使用iText从多个HTML页面生成PDF [英] Generate PDF from multiple HTML pages using iText

查看:190
本文介绍了使用iText从多个HTML页面生成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何生成单个HTML页面。我想知道如何从pdf生成从多个HTML页面生成的单个pdf页面。

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.

例如,有 HTMLX1.html 还有另一个文件 HTMLX2.html
我可以生成单独的pdf文件 PDFX1.pdf PDFX2.pdf 。我可以将它们写入文件系统,然后将它们连接起来,如 iTextConcatenate示例

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

推荐答案

创建PDFX1.pdf时,请使用 PdfWriter 带有 ByteArrayOutputStream 而不是 FileOutputStream

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);

连接PDF文档时,请使用 PdfReader 使用 ByteArrayInputStream

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());

(对于PDFX2.pdf也这样做。)

(Do this similarly for PDFX2.pdf.)

这篇关于使用iText从多个HTML页面生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆