Hi,
Below is the snippet used to generate a pdf document from buffered image but it isnt working.Generated pdf is completely blank
BufferedImage originalImage = ImageIO.read(new File(“/Users/dev/image.png”));
Document pdfDocument = new Document();
Page page = pdfDocument.getPages().add();
page.getResources().getImages().add(originalImage);
pdfDocument.save(“/Users/dev/mmmm.pdf”);
pdfDocument.close();
@maheibm
Could you please share your sample image and the generated output PDF as well? We will test the scenario in our environment and address it accordingly.
files.zip (40.8 KB)
Please find the attached file containing image and generated pdf file. Also to bring to your notice, it has been tried with many conversions (doc to png and then to pdf) that the pdf generated (with just images, each image in a page) is more than the overall images size(way more). Even in this example you can see that image is 27kb and pdf is 92kb and is blank too. I did try below mentioned code to compress but no changes at all. In the other scenario, overall 287 png size was 56mb but the pdf generated was 101.5mb. (It was same even after converting buffered image to com.aspose.pdf.Image and then inserted to pdf) but we wanted the direct insertion of buffered image to work and ensure the size isnt double.
OptimizationOptions a = new OptimizationOptions();
a.setCompressObjects(true);
ImageCompressionOptions ds = new ImageCompressionOptions();
ds.setCompressImages(true);
ds.setResizeImages(true);
a.setImageCompressionOptions(ds);
a.setRemoveUnusedObjects(true);
a.setRemoveUnusedStreams(true);
a.setUnembedFonts(true);
pdfDocument.optimizeResources(a);
pdfDocument.optimize();
Please note that the heap memory spiked to 5gb for the operation of creating 300page pdf with 300 pngs. Would request you to let us know a better way as this cant be considered.
@maheibm
Please try to use the code snippet like below to add an image inside PDF:
BufferedImage originalImage = ImageIO.read(new File(dataDir + "image.png"));
Document doc = new Document();
Page page = doc.getPages().add();
com.aspose.pdf.Image image = new Image();
image.setBufferedImage(originalImage);
//image.setFile(dataDir + "ActualPNG.png");
// Set margins so image will fit, etc.
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);
// Get the width of the image
int width = originalImage.getWidth();
// Get the height of the image
int height = originalImage.getHeight();
page.getPageInfo().setWidth(originalImage.getWidth());
page.getPageInfo().setHeight(originalImage.getHeight());
page.getParagraphs().add(image);
doc.save(dataDir + "output.pdf");
Related to the file size and memory spike issue, can you please try using 24.5 version of the API and if issue still persists, please share your system specifications as well as JDK version which you are using with Java Heap Size. We will log an investigation ticket and share the ID with you.
@asad.ali Thanks for the response. Like i mentioned in earlier comment, i did try adding buffered image to com.aspose.pdf.Image and then adding it to pdf page which worked but wanted it to work by directly taking bufferedimage and not another intermediate conversion(since it would be efficient, may be). Also i did try on 24.5 and memory issue with pdf size is still the same. i’m using Apple M1 Pro macbook with 32gb of RAM and 10 cores, using jdk 1.8.0_144. Attached is a snapshot of heap memory trying to create a pdf from png images (399 images added to pdf) and then performed GC (as you can notice in image the reduced the heap memory significantly)
Screenshot 2024-06-15 at 12.23.05 PM.png (79.0 KB)
@maheibm
We are checking it and will get back to you shortly.
@maheibm
We have logged below two tickets in our issue tracking system for the scenarios:
- PDFJAVA-44031 - Memory spike issues
- PDFJAVA-44030 - Add buffered image directly to PDF
Related to the PDF file size, can you please share sample image with the generated PDF file that can showcase the irregular file size even after optimizing the PDF document so that we can further proceed with logging a dedicated ticket for this scenario as well?
@asad.ali Thanks for your prompt action on this. In the second comment of mine, i have uploaded the image and generated pdf ( files.zip 40.8 KB) which shows significant file size changes from png to pdf even after optimisation being set.
@maheibm
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFJAVA-44034
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@asad.ali Could you please let us know the link for paid support as we now have the license purchase. Also wanted you to consider these tickets raised under priority .
@maheibm
Have you subscribed to the paid support already? If so, you will be able to login there using same email address that was used to purchase the subscription. Please note that paid support service need to be subscribed separately. It does not come along normal licensing.
@asad.ali thanks for the response. I could login there. Shall i create a ticket to ask for priority on these three tickets raised?
@maheibm
You can simply create a post there while mentioning the ticket ID logged here and your issue will be escalated accordingly.
The issues you have found earlier (filed as PDFJAVA-44034) have been fixed in Aspose.PDF for Java 24.9.