Could you please provide suggestion how preview pdf file before downloading? Thanks.
Hi,
by preview you probably mean, showing the PDF File within your application. Since the PDF download, can also be shown just in another browser window. This is completely dependent on your Browser and personal system.
I’ve implemented a PDF preview within CUBA Platform version 6.10 by rendering the PDF File (each page) as a picture, and dynamically setting this as the source for an image component in the window. For converting the PDF to an image I am using Apache PDFBox.
Thats how it looks - Image component in a Group Box, at the top two buttons to page up and down, and close the pdf preview:
Relevant Code:
private void showPDFPreviewBtnClick () {
pdfAsImagePreview.setSource(StreamResource.class)
.setStreamSupplier(() -> {
try {
InputStream pdfAsImage = convertPDFPageToImage(desiredPageToShow, YourEntity.getDocument());
return pdfAsImage;
} catch (IOException | FileStorageException e) {
e.printStackTrace();
return null;
}
});
}
private InputStream convertPDFPageToImage(int page, FileDescriptor PdfDatei ) throws IOException, FileStorageException {
byte[] dokumentBytes;
dokumentBytes = fileStorageService.loadFile(PdfDatei);
PDDocument doc;
doc = PDDocument.load(dokumentBytes);
PDFRenderer renderer = new PDFRenderer(doc);
BufferedImage image = null;
for (int i = 0; i < doc.getNumberOfPages(); i++) {
if (i == page) {
image = renderer.renderImageWithDPI(i, 200);
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpeg", baos);
byte[] imageBytes = baos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
return bis;
}
hope this helps.
Best regards
Daniel
Thank you both!