0

I'm trying to use an existing PDF template and iText to fill in the document, then send the file to our database.

However, I cannot figure out how to convert the finished iText PDF into a usable form - I can display it to the user easily enough, but I cannot get it into a File, InputStream, or even byte[] format to upload to our Database.

public ActionForward doIt(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws java.lang.Exception
{   


 int docid = Integer.parseInt(form.getDocumentTemplateId());
 byte[] byteTemplate = TemplateDb.getTemplate(docId);


    PdfReader pdfReader = new PdfReader(byteTemplate);
    PdfStamper pdfStamper = new PdfStamper(pdfReader, response.getOutputStream());
    AcroFields acroFields = pdfStamper.getAcroFields();         

    acroFields.setField(//And then I set my acro fields, which works fine);

 ByteArrayInputStream inByteStream = new ByteArrayInputStream(byteTemplate );

// This is me calling a separate function to upload the Input Stream - but all that the inByteStream object contains is a blank template
    DocumentManager.uploadDocument(inByteStream);        

    pdfStamper.close();

    pdfReader.close();              

}
Zibbobz
  • 725
  • 1
  • 15
  • 41
  • 2
    `response.getOutputStream()` in your `PdfStamper` constructor holds the final PDF after the call to `pdfStamper.close();`. You can either dump `response.getOutputStream()` to a byte array, or replace it with a `ByteArrayOutputStream` and pass that to both `uploadDocument` and the resonse's output stream – Jon Reilly Oct 09 '18 at 21:03
  • @JonReilly Yes, that is ideally what I would like to do - but I don't know *how* to do either of those things - I don't know how to convert response.getOutputStream() to a Byte Array(Ideal) or a ByteArrayOutputStream - and I have done a lot of google searching trying to find an explanation on how to do that. I didn't want to lead the question by just asking for that specifically - but if you could explain how, that would be a complete answer for this question. – Zibbobz Oct 10 '18 at 13:10
  • Since `response.getOutputStream()` subclasses `java.io.OutputStream` there should simply be a `toByteArray()` method. `response.getOutputStream().toByteArray()` will be a byte[] of your filled in PDF **after** the call to `pdfStamper.close()`. I'm not 100% familiar with the `HttpServletResponse` class though- it may be possible there are headers in that stream as well. – Jon Reilly Oct 10 '18 at 13:35
  • Whoops, looks to `toByteArray()` is part of the `ByteArrayOutputStream` class. You can convert a generic `OutputStream` to a `ByteArrayOutputStream` like in [this example](https://stackoverflow.com/a/26961084/7407793) – Jon Reilly Oct 10 '18 at 13:42
  • @JonReilly I saw that example and tried it - unfortunately, what I have is a ServletOutputStream - which cannot be converted like this. – Zibbobz Oct 10 '18 at 14:24
  • I'm not familiar enough with ServletOutputStream, though I'd be very surprised if you couldn't copy to some other form of Outputstream. Regardless you can use the second approach. `ByteArrayOutputStream boas = new ByteArrayOutputstream()` and then `PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);` After `pdfStamper.close()` you can now call `boas.toByteArray()` and pass that to your `uploadDocument( ... )` method and `response.getOutputStream().write( ... )`. – Jon Reilly Oct 10 '18 at 14:38

0 Answers0