如何将html字符串转换为PDF InputStream?(How to convert an html String to a PDF InputStream?)

编程入门 行业动态 更新时间:2024-10-26 22:27:11
如何将html字符串转换为PDF InputStream?(How to convert an html String to a PDF InputStream?)

如果我们有一个html页面内容的字符串,我们如何将它转换为将此字符串转换为pdf文档后生成的InputStream

我正在尝试将iText与XMLWorkerHelper一起使用,以下代码可以正常工作,但问题是我不想在文件上输出。 我尝试了几种变体,以便在InputStream上获得结果,我可以将其转换为Primefaces StreamedContent,但没有成功。 我们怎么做?

我们可以使用另一种技术来解决这个问题吗? 这样做的动机是使用已经呈现的xhtml文件并将其输出为pdf以供用户下载。

Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("results/loremipsum.pdf")); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("/html/loremipsum.html")); document.close();

If we have string with a content of a html page, how can we convert it to a InputStream made after transform this string to a pdf document?

I'm trying to use iText with XMLWorkerHelper, and this following code works, but the problem is I don't want the output on a file. I have tried several variations in order to get the result on a InputStream that I could convert to a Primefaces StreamedContent but no success. How we can do it?

Is there another technique that we can use to solve this problem? The motivation to this is use xhtml files wich is already rendered and output it as a pdf to be downloaded by the user.

Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("results/loremipsum.pdf")); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("/html/loremipsum.html")); document.close();

最满意答案

如果您需要一个InputStream ,其他一些代码可以读取您的代码生成的PDF,您只需使用字节数组输出流创建PDF,然后将字节数组从该流包装在字节数组输入流中:

ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("/html/loremipsum.html")); document.close(); ByteArrayInputStream pdfInputStream = new ByteArrayInputStream(baos.toByteArray());

您可以通过在不同的线程中创建和处理PDF并使用PipedOutputStream和PipedInputStream来优化这一点。

If you need an InputStream from which some other code can read the PDF your code produces, you can simply create the PDF using a byte array output stream and thereafter wrap the byte array from that stream in a byte array input stream:

ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("/html/loremipsum.html")); document.close(); ByteArrayInputStream pdfInputStream = new ByteArrayInputStream(baos.toByteArray());

You can optimize this a bit by creating and processing the PDF in different threads and using a PipedOutputStream and a PipedInputStream instead.

更多推荐

本文发布于:2023-07-05 07:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   转换为   如何将   html   String

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!