java 使用 iText,在内存上生成一个在磁盘上生成的 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2151867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:43:21  来源:igfitidea点击:

Using iText, generate on memory a PDF that is generated on disk instead

javapdf-generationitext

提问by Sheldon

I'm generating a PDF from a Java application. (And works great) the problem is that the PDF is generated on disk as:

我正在从 Java 应用程序生成 PDF。(并且效果很好)问题是 PDF 在磁盘上生成为:

        Document documento = new Document(PageSize.A4, 25, 25, 25, 25);
        PdfWriter writer = PdfWriter.getInstance(documento, new FileOutputStream("/Users/sheldon/Desktop/Registry.pdf"));
        documento.open();

        // Put some images on the PDF
        for( byte[] imagen : imagenes )
        {
            Image hoja = Image.getInstance(imagen);
            hoja.scaleToFit(documento.getPageSize().getHeight(), documento.getPageSize().getWidth());
            documento.add(hoja);
        }

        documento.addTitle("Generated Registry!");

        documento.close();

Now, as the user will search for the PDF and print them I don't need to store them on disk. I need (if possible) to generate them in memory and use a command to open (with acrobat reader) that document.

现在,由于用户将搜索 PDF 并打印它们,我不需要将它们存储在磁盘上。我需要(如果可能)在内存中生成它们并使用命令打开(使用 acrobat 阅读器)该文档。

Is that possible? Any idea.

那可能吗?任何的想法。

If not, what suggestions (on your experience) have.

如果没有,有什么建议(根据您的经验)。

Thank you on advance.

提前谢谢你。

EDIT:

编辑:

Is for an standard Java Desktop Application.

用于标准 Java 桌面应用程序。

回答by behe

If you don't want iText to generate your document to disk, then just do this:

如果您不希望 iText 将文档生成到磁盘,则只需执行以下操作:

Document documento = new Document(PageSize.A4, 25, 25, 25, 25);
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(documento, out);
(...)
return out.getBytes();

This won't help you though, since Reader can't access it until you written it somewhere Acrobat can access it. If you don't want that to be on disk, then mount a virtual in memory disk and write your files there. How you do this, depends upon your operating system.

但是,这对您没有帮助,因为 Reader 无法访问它,除非您将它写在 Acrobat 可以访问的地方。如果你不希望它在磁盘上,那么在内存磁盘中安装一个虚拟并将你的文件写入那里。您如何执行此操作取决于您的操作系统。

回答by mlathe

Yes... it's pretty easy. You just have to stream the content back to the requester (ie via the Response object in a Servlet). You also need to set the header

是的……这很容易。您只需要将内容流式传输回请求者(即通过 Servlet 中的 Response 对象)。您还需要设置标题

'Content-type: application/pdf'

You might also want to set this to get it to not open in the browser

您可能还想设置它以使其不在浏览器中打开

'Content-Disposition: attachment; filename="downloaded.pdf"'

回答by matt b

For this to work, Acrobat would need to be able to access the memory of another process (Java). This is not possible.

为此,Acrobat 需要能够访问另一个进程 (Java) 的内存。这不可能。

You might just want to write the files to the system's temporary directory.

您可能只想将文件写入系统的临时目录。

If your application stays open after opening the PDF in Acrobat, you might want to look into using a combination of File.createTempFile()and File.deleteOnExit(), to have the file deleted upon termination of the JVM.

如果在Acrobat打开PDF后,您的应用程序保持打开状态,你可能要考虑使用的组合File.createTempFile()File.deleteOnExit(),有文件于JVM终止删除。

回答by Dominique

I'm not a JAVA-programmer, but I'm working with iText a little bit at this moment and I had the same question. I figured that if pdfWriter only needs an outputStream it might as well use java.io.ByteArrayOutputStream. That would be new ByteArrayOutputStream() I guess, in JAVA, as I am using ColdFusion.

我不是 JAVA 程序员,但此时我正在使用 iText,并且我有同样的问题。我认为如果 pdfWriter 只需要一个 outputStream,它也可以使用 java.io.ByteArrayOutputStream。那将是 new ByteArrayOutputStream() 我猜,在 JAVA 中,因为我使用的是 ColdFusion。

For me, it works.

对我来说,它有效。

回答by deb_

The requirement can be for a web application where users can download a PDF which is generated at runtime. File.createTempFile()might create a large number for temporary files and File.deleteOnExit()will only be called on JVM exit - not an ideal scenario.

要求可能是针对用户可以下载在运行时生成的 PDF 的 Web 应用程序。File.createTempFile()可能会为临时文件创建大量文件,并且 File.deleteOnExit()只会在 JVM 退出时调用 - 不是理想的情况。

In such cases it will be wise to implement what @behesuggested and finally write ByteArrayOutputStreamobject to ServletOutputStream.

在这种情况下,明智的做法是实现@behe 的建议并最终将ByteArrayOutputStream对象写入ServletOutputStream.

ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();

//get ByteArrayOutputStream from behe's code snippet
ByteArrayOutputStream bout = (...)
bout.writeTo(servletOutputStream);

httpServletResponse.setContentType("application/octet-stream");
httpServletResponse.setHeader("Content-Disposition", "attachment;filename=\"" + <fileName> + "\"");