如果我们有一个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.
更多推荐
发布评论