Size issue after file storage by server. Your guide failed

It didn’t work out the way you told me.

File size different.same save results

We tested it in two ways, but all failed

#. Test 1

Document doc = new Document();

Page page = doc.getPages().add();
PageInfo pageInfo = new PageInfo();
pageInfo.setMargin(new MarginInfo(10, 10, 10, 10));
page.setPageInfo(pageInfo);

HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
HtmlFragment htmlFragment = new HtmlFragment(“…”);
htmlFragment.setHtmlLoadOptions(htmlLoadOptions);

// Set the font from the project
TextState textState = new TextState();
Font font = FontRepository.openFont(“font file”);
textState.setFont(font); // Use the font name if it’s in the project resources
htmlFragment.setTextState(textState);

page.getParagraphs().add(htmlFragment);

/*

  • I configured the page to put html of PDF 1 page content,
  • I’m going to generate a total of three page html
    */

PdfSaveOptions options = new PdfSaveOptions();

doc.optimize();
doc.save(“C:/A.pdf”, options);

#. Test 2

Document doc = new Document();

Page page = doc.getPages().add();
PageInfo pageInfo = new PageInfo();
pageInfo.setMargin(new MarginInfo(10, 10, 10, 10));
page.setPageInfo(pageInfo);

HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
HtmlFragment htmlFragment = new HtmlFragment(“…”);
htmlFragment.setHtmlLoadOptions(htmlLoadOptions);

// Set the font from the project
TextState textState = new TextState();
Font font = FontRepository.openFont(“font file”);
textState.setFont(font); // Use the font name if it’s in the project resources
htmlFragment.setTextState(textState);

page.getParagraphs().add(htmlFragment);

/*

  • I configured the page to put html of PDF 1 page content,
  • I’m going to generate a total of three page html
    */

PdfSaveOptions options = new PdfSaveOptions();

OptimizationOptions optimizationOptions = new OptimizationOptions();
optimizationOptions.setAllowReusePageContent(true);
optimizationOptions.setLinkDuplcateStreams(true);
optimizationOptions.setRemoveUnusedObjects(true);
optimizationOptions.setRemoveUnusedStreams(true);

ImageCompressionOptions imageCompressionOptions = new ImageCompressionOptions();
imageCompressionOptions.setCompressImages(true);
imageCompressionOptions.setImageQuality(10);
optimizationOptions.setImageCompressionOptions(imageCompressionOptions);

doc.optimizeResources(optimizationOptions);
doc.save(“C:/A.pdf”, options);

@OH_JUNG_EUN

It seems you’re experiencing issues with the file size of the PDF generated by your code. Here are some suggestions to help you troubleshoot and optimize the file size effectively.

Suggestions for Optimizing PDF File Size

  1. Ensure Proper Optimization Settings: In your second test, you are using OptimizationOptions to optimize the PDF. Make sure that you are setting all relevant options correctly. Here’s a brief overview of the options you can use:

    • AllowReusePageContent: This allows the reuse of page content, which can help reduce file size.
    • LinkDuplicateStreams: This links duplicate streams to avoid redundancy.
    • RemoveUnusedObjects: This removes any objects that are not used in the document.
    • RemoveUnusedStreams: This removes any streams that are not used.

    Here’s an example of how to set these options:

    OptimizationOptions optimizationOptions = new OptimizationOptions();
    optimizationOptions.setAllowReusePageContent(true);
    optimizationOptions.setLinkDuplcateStreams(true);
    optimizationOptions.setRemoveUnusedObjects(true);
    optimizationOptions.setRemoveUnusedStreams(true);
    
  2. Image Compression: You are already compressing images in your second test. Ensure that the ImageQuality is set appropriately. A lower quality (e.g., 10) will significantly reduce the file size but may affect the visual quality. Adjust this value based on your requirements.

    ImageCompressionOptions imageCompressionOptions = new ImageCompressionOptions();
    imageCompressionOptions.setCompressImages(true);
    imageCompressionOptions.setImageQuality(10); // Adjust as needed
    optimizationOptions.setImageCompressionOptions(imageCompressionOptions);
    
  3. Check for Redundant Content: If your HTML content includes large images or unnecessary elements, consider optimizing the HTML before converting it to PDF. This can help reduce the overall size of the generated PDF.

  4. Testing Different Approaches: Since you mentioned that both tests failed, try simplifying your approach. Start with a basic document creation and optimization process, then gradually add complexity. This can help identify which part of the process is causing the increase in file size.

  5. Review Output: After saving the PDF, check the output file size and compare it with the expected size. If the size is still larger than anticipated, consider reviewing the content being added to the PDF for any large images or unnecessary data.

Example Code for Optimization

Here’s a simplified example of how to optimize a PDF document:

Document pdfDocument = new Document();
Page page = pdfDocument.getPages().add();
HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
HtmlFragment htmlFragment = new HtmlFragment("<html>...</html>");
htmlFragment.setHtmlLoadOptions(htmlLoadOptions);
page.getParagraphs().add(htmlFragment);

OptimizationOptions optimizationOptions = new OptimizationOptions();
optimizationOptions.setAllowReusePageContent(true);
optimizationOptions.setLinkDuplcateStreams(true);
optimizationOptions.setRemoveUnusedObjects(true);
optimizationOptions.setRemoveUnusedStreams(true);

ImageCompressionOptions imageCompressionOptions = new ImageCompressionOptions();
imageCompressionOptions.setCompressImages(true);
imageCompressionOptions.setImageQuality(10);
optimizationOptions.setImageCompressionOptions(imageCompressionOptions);

pdfDocument.optimizeResources(optimizationOptions);
pdfDocument.save("C:/A.pdf");

By following these suggestions and using the example code, you should be able to better manage the file size of your generated PDFs. If you continue to face issues, consider checking the content being added to the PDF for any large or unnecessary elements.