0

I am using PDFStamper to generate a PDF file and then I want to pass it to be opened in a Browser. My code is in a JSP file. My code to actually generate a PDF to Desktop works but not to route to a browser. Below is my code.

PdfReader reader = new PdfReader("/path/pdfs/raw.pdf");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, baos);
        PdfContentByte canvas = stamper.getOverContent(1);
        BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
        canvas.setFontAndSize(font, 12);
        canvas.beginText();
        canvas.showTextAligned(Element.ALIGN_LEFT, "TEST! TEST! TEST! TEST! ", 80, 713, 0);
        canvas.endText();
        stamper.close();
        reader.close();
        String filename="test.pdf";
        response.setContentType("application/pdf");
        response.setHeader( "Content-Disposition", "filename=" + filename );
        response.setContentType("application/pdf");
        OutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();     

This currently opens a blank page - I am not sure what exactly I am doing wrong.

I can make this work using iText Document but since I am opening an existing document and adding stuff to it I have to use PDFStamper and that is where the issue comes. I've confirmed the PDF file in reader exists and can be accessed via a browser by directly going to the location.

Any help would be appreciated! Using, Struts2, Tile2, Weblogic, Java, iText

SamiFekadu
  • 3
  • 1
  • 4
  • [Return NONE or use Stream Result](http://stackoverflow.com/a/30240478/1654265). The latter, the better. – Andrea Ligios Nov 13 '15 at 09:04
  • Possible duplicate of [Getting the jsp source code appended with the file content when I download files](http://stackoverflow.com/questions/30240284/getting-the-jsp-source-code-appended-with-the-file-content-when-i-download-files) – Andrea Ligios Nov 13 '15 at 09:04
  • No that is not it @AndreaLigios - this is specific to PDFStamper. I have it working with things like Document and other libraries. PDFStamper seems to be not be working the normal way. – SamiFekadu Nov 17 '15 at 18:10
  • Have you tried saving the contents of `baos` to a file, too, and inspecting that file? – mkl Nov 21 '15 at 07:48

0 Answers0