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 reader)该文档。
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桌面应用程序。
5 个解决方案
#1
5
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终止时删除该文件。
#2
7
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.
这对你没有帮助,因为在你把它写在某处Acrobat可以访问它之前,Reader无法访问它。如果您不希望它在磁盘上,则在内存磁盘中安装虚拟磁盘并在那里写入文件。如何执行此操作取决于您的操作系统。
#3
5
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"'
#4
2
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。那将是新的ByteArrayOutputStream()我想,在JAVA中,因为我正在使用ColdFusion。
For me, it works.
对我来说,它有效。
#5
0
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 @behe suggested and finally write ByteArrayOutputStream
object 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> + "\"");
#1
5
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终止时删除该文件。
#2
7
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.
这对你没有帮助,因为在你把它写在某处Acrobat可以访问它之前,Reader无法访问它。如果您不希望它在磁盘上,则在内存磁盘中安装虚拟磁盘并在那里写入文件。如何执行此操作取决于您的操作系统。
#3
5
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"'
#4
2
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。那将是新的ByteArrayOutputStream()我想,在JAVA中,因为我正在使用ColdFusion。
For me, it works.
对我来说,它有效。
#5
0
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 @behe suggested and finally write ByteArrayOutputStream
object 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> + "\"");