I've seen many answers here, I copied some examples and tried applying it, but I don't know how to make this work. I'm trying to create a file with PDFBox and sending it with a Response so the user can download it. So far, I'm able to download the file, but it is blank. I already tried just loading an example file from my computer with PDFBox and downloading it, but it comes the same way, blank. The code I am working with now is:
@GET
@Path("/dataPDF")
@Produces("application/pdf")
public Response retrievePDF(){
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
output = createPDF();
ResponseBuilder response = Response.ok(output.toByteArray(), "application/pdf");
response.header("Content-Disposition","attachment; filename=file.pdf");
return response.build();
}
catch (Exception ex) {
ex.printStackTrace();
return Response.status(Response.Status.NOT_FOUND).build();
}
public ByteArrayOutputStream createPDF() throws IOException {
PDFont font = PDType1Font.HELVETICA;
PDPageContentStream contentStream;
ByteArrayOutputStream output =new ByteArrayOutputStream();
PDDocument document =new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(font, 20);
contentStream.newLineAtOffset(10, 770);
contentStream.showText("Amount: $1.00");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(font, 20);
contentStream.newLineAtOffset(200, 880);
contentStream.showText("Sequence Number: 123456789");
contentStream.endText();
contentStream.close();
document.save(output);
document.close();
return output;
}
UPDATE 1: So the file is being created, now I'm justing having trouble sending it to the web, I'm using ReactJS. I tried adapting the structure I used to download a csv file, here it is:
const handleExportPDF= fileName => {
FileController.retrievePDF().then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
});
};
static retrievePDF() {
const { method, url } = endpoints.retrievePDF();
return api[method](url,{
responseType: "application/pdf"
});
}
export const fileEndpoints = {
retrievePDF: () => ({
method: "get",
url: `/export/dataPDF`
})
};
UPDATE 2: If someone ever stumbles here, I was able to solve the problem with this answers here: PDF Blob - Pop up window not showing content. The point was changing
responseType: "application/pdf"
to responseType: 'arraybuffer'
And even though it already works just changing this, I also changed
window.URL.createObjectURL(new Blob([response.data]));
to window.URL.createObjectURL(new Blob([response.data]), {type: 'application/pdf'});