0
votes


I am using following snippet code for Generate PDF as Output Stream in HTTP request in java
Code is:-

        webAppAccess.getHttpServletResponse().setContentType("application/pdf");

        try {
            // step 1
            Document document = new Document();
            // step 2
            PdfWriter.getInstance(document, webAppAccess
                    .getHttpServletResponse().getOutputStream());
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph("Hello World"));
            document.add(new Paragraph(new Date().toString()));
            // step 5
            document.close();
        } catch (DocumentException de) {
            throw new IOException(de.getMessage());
        }

I am getting pdf on screen

%PDF-1.4 %���� 2 0 obj <>stream x�+�r �26S�00SI�2P�5�1���BҸ4>>>/MediaBox[0 0 595 842]/Parent 3 0 R/Contents 2 0 R/Type/Page>> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000331 00000 n 0000000015 00000 n 0000000419 00000 n 0000000174 00000 n 0000000470 00000 n 0000000515 00000 n trailer <]/Info 6 0 R>> %iText-5.4.0 startxref 668 %%EOF


Please help me out for the same

3
you need to set up the content-type header. The data you're getting back is the PDF, but the browser doesn't know it's a PDF.HocusPocus
what piece of code should i add for that? could you please give me..eagle

3 Answers

2
votes

You might want to add one header where you mention response is an attachment ->

response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);
1
votes
ByteArrayOutputStream baos = // make a bytearray output from the document
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setHeader("Content-Disposition", "attachment; filename=filename.pdf");
// setting the content type
response.setContentType("application/pdf");
response.setContentLength(baos.size());
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());

baos.writeTo(bos);
bos.flush(); 
bos.close();
-1
votes

Thank God finally it worked!!!,I sucessed by opening .PDF file in new tab,From developemnt perspective ON CLICK either button or link target should be balnk e.g(target=_blank)