简体   繁体   中英

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

I'm generating a PDF from a Java application. (And works great) the problem is that the PDF is generated 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, 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.

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.

If you don't want iText to generate your document to disk, then just 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 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.

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

'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"'

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

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.

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.

For me, it works.

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.

In such cases it will be wise to implement what @behe suggested and finally write ByteArrayOutputStream object to 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> + "\"");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM