Acewin
Acewin

Reputation: 1727

Generate PDF from multiple HTML pages using iText

I know how to generate a single HTML page. I want to know how to generate a single pdf page from pdfs that are generated from multiple HTML pages.

For example There is HTMLX1.html and there is another file HTMLX2.html I can generate the individual pdf files PDFX1.pdf and PDFX2.pdf respectively from the html. I can write them to the file system and then concatenate them as in iTextConcatenate Example.

I simply want to know if I can combine this action on the fly without writing them to the file system. I have not been able to identify the missing link

Upvotes: 2

Views: 1805

Answers (2)

Sinan Gedik
Sinan Gedik

Reputation: 92

You can use StringReader and StringWriter if you want to avoid writing to a file.

Oracle docs:

  1. StringReader

  2. StringWriter

It will still be a I/O process but you won't write to or read from an actual file. Instead, you will use String buffers.

Upvotes: 0

rhens
rhens

Reputation: 4871

When you create PDFX1.pdf, use a PdfWriter with a ByteArrayOutputStream instead of a FileOutputStream:

ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos1);

When concatenating the PDF documents, use a PdfReader with a ByteArrayInputStream:

PdfReader reader = new PdfReader(new ByteArrayInputStream(baos1.toByteArray()));

Or you can also use the byte array directly:

PdfReader reader = new PdfReader(baos1.toByteArray());

(Do this similarly for PDFX2.pdf.)

Upvotes: 2

Related Questions