Using iText, create in memory a PDF that is created on disk instead of

I am creating a PDF file from a Java application. (And it works fine) the problem is that the PDF is created on disk as:

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 that the user will search for the PDF file and print it, I do not need to store them on disk. I need (if possible) to generate them in memory and use the command to open (using acrobat reader) this document.

Is it possible? Any idea.

If not, what suggestions (in your experience) are there.

Thank you in advance.

EDIT:

Used for the standard Java Desktop application.

+4
source share
5 answers

To do this, Acrobat must have access to the memory of another process (Java). It's impossible.

You can simply write files to the system temporary directory.

If your application remains open after opening the PDF file in Acrobat, you may need to use a combination of File.createTempFile() and File.deleteOnExit() to delete the file after the JVM completes.

+5
source

If you do not want iText to create your document to disk, simply do this:

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

This will not help you, because Reader cannot access it until you write it somewhere where Acrobat can access it. If you do not want this to be on disk, connect a virtual disk with memory and write your files there. How you do this depends on your operating system.

+7
source

Yes ... it's pretty easy. You just need to pass the content back to the requestor (i.e. via the Response object in the Servlet). You also need to set a title.

 'Content-type: application/pdf' 

You can also set this to prevent it from opening in the browser

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

I am not a JAVA programmer, but I am working a bit with iText at this point, and I had the same question. I realized that if pdfWriter only needs outputStream, it can also use java.io.ByteArrayOutputStream. This will be the new ByteArrayOutputStream () I think in JAVA since I use ColdFusion.

It works for me.

+2
source

The requirement may be for a web application where users can download a PDF file that is created at runtime. File.createTempFile() can create a large number of temporary files, and File.deleteOnExit() will only be called when the JVM is output - not an ideal scenario.

In such cases, it would be wise to implement what @behe suggested and finally write the ByteArrayOutputStream object to ServletOutputStream .

 ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream(); //get ByteArrayOutputStream from behe code snippet ByteArrayOutputStream bout = (...) bout.writeTo(servletOutputStream); httpServletResponse.setContentType("application/octet-stream"); httpServletResponse.setHeader("Content-Disposition", "attachment;filename=\"" + <fileName> + "\""); 
0
source

Source: https://habr.com/ru/post/1299609/


All Articles