'Java PDFBox merge heic/dmg files to PDF
I have Spring boot application and i'm using PDFBox in order to merge multiple pdf and images to one pdf file.
Is it possible to merge heic or dmg file to pdf using PDFBox?
The attached code failed for heic and dmg files in line "float width = bimg.getWidth();"
merge code:
public static ByteArrayOutputStream mergeFiles(Map<InputStream, String> sourcesMap) throws IOException {
Path mergeDirectory = Files.createTempDirectory("merge-" + System.currentTimeMillis());
ByteArrayOutputStream mergedPDFOutputStream = null;
try {
mergedPDFOutputStream = new ByteArrayOutputStream();
PDFMergerUtility mixedPdfMerger = createMixedPdfMerger(sourcesMap, mergedPDFOutputStream, mergeDirectory);
mergeFileStreams(mergedPDFOutputStream, mixedPdfMerger);
return mergedPDFOutputStream;
} catch (Exception e) {
log.error("Failed to merge PFD", e);
if (!(e instanceof IOException)) {
throw new IOException("PDF merge problem", e);
}
throw (IOException) e;
} finally {
FileUtils.deleteDirectory(mergeDirectory.toFile());
sourcesMap.keySet().forEach(IOUtils::closeQuietly);
if (mergedPDFOutputStream != null) {
mergedPDFOutputStream.close();
}
}
}
private static PDFMergerUtility createMixedPdfMerger(Map<InputStream, String> sourcesMap,
ByteArrayOutputStream mergedPDFOutputStream,
Path mergeDirectory) throws IOException {
PDFMergerUtility pdfMerger = new PDFMergerUtility();
for (Map.Entry<InputStream, String> source : sourcesMap.entrySet()) {
if ("pdf".equalsIgnoreCase(source.getValue())) {
pdfMerger.addSource(source.getKey());
} else {
File file = streamToFile(mergeDirectory, source.getKey());
pdfMerger.addSource(imageToPDDocument(mergeDirectory, file, source.getValue()));
}
}
pdfMerger.setDestinationStream(mergedPDFOutputStream);
return pdfMerger;
}
private static File imageToPDDocument(Path mergeDirectory, File file, String fileType) throws IOException {
PDDocument doc = null;
InputStream in = null;
try {
log.info("*** imageToPDDocument ***");
MemoryUtil.logMemoryInfo();
doc = new PDDocument();
in = new FileInputStream(file);
BufferedImage bimg = ImageIO.read(in);
float width = bimg.getWidth();
float height = bimg.getHeight();
PDPage page = new PDPage(new PDRectangle(width, height));
doc.addPage(page);
doc.setResourceCache(new DefaultResourceCache() {
@Override
public void put(final COSObject indirect, final PDXObject xobject) {
}
});
PDImageXObject img;
if ("tiff".equalsIgnoreCase(fileType)) {
BufferedImage bim = ImageIO.read(file);
img = LosslessFactory.createFromImage(doc, bim);
} else {
img = PDImageXObject.createFromFileByContent(file, doc);
}
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.drawImage(img, 0, 0);
contentStream.close();
File pdfFile = Files.createTempFile(mergeDirectory, String.valueOf(System.currentTimeMillis()), ".tmp").toFile();
doc.save(pdfFile);
return pdfFile;
} finally {
if (in != null) {
in.close();
}
if (doc != null) {
doc.close();
}
log.info("*** imageToPDDocument - finally ***");
MemoryUtil.logMemoryInfo();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
