Vamsi Vegesna
Vamsi Vegesna

Reputation: 107

Dont want to save created PDF file in server side while using PDFBox

I am using PDFBox in my project. I currently save the PDF that is created before sending it to client. Instead the requirement is to send the ByteArrayStream to client side without saving file. How to do this with PDFBox?

I know this possible with iText. But I am restricted to use iText in my current project.

Below is the code that is used.

    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.beginText();
    contentStream.showText("PDF created");
    contentStream.endText();
    contentStream.close();
    document.save(outputFilePath);// don't want to do this
    document.close();

Upvotes: 0

Views: 807

Answers (1)

Gerben Jongerius
Gerben Jongerius

Reputation: 729

You could use the overloaded method PDDocument.save with the outputstream. This would result into something similar to this:

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
document.save(outStream);
byte[] pdfData = outStream.toByteArray();

This would allow you to get the PDF directly and use it.

Upvotes: 2

Related Questions