Free Support Forum - aspose.com

Fitting images to page height/adjusting page width when converting from html to pdf

Hi,
I have several questions on the topic.

When using conversion of html to pdf (with java), is it possible to make images to always fit into a single page, and not to split an image into multiple pages?
Currently, I am able to do that by applying css styles that make height equal to page height without margins, something like this:

<img … style=“height: ${maxPageHeight}” />

Is there any other way, preferably without specifying the exact page height? Maybe by setting some options in Java code? The relevant piece of code how we are currently saving documents in Java:

InputStream targetStream = new 
ByteArrayInputStream(htmlTemplate.getBytes(StandardCharsets.UTF_8));
HtmlLoadOptions options = new HtmlLoadOptions();
options.getPageInfo().setMargin(new MarginInfo(10, 10, 10, 10));
Document document = new Document(targetStream, options);
document.setFitWindow(true);
document.save(outputStream);

Also, at the moment, if the image fits within a page by height but is to wide for a page, the page is made wider to display the whole image. That is the preferred behavior by us. But if the image is too high and very wide, and I set the image height to be equal to the page height using img style element or img height, then the page is not made wider and is cut at the right to fit page width. Is there a way both to make the page wider, and to fit an image in a single page?

After you have opened the file, click on the “File” tab on the menu bar at the top and then select the "Optimize PDF " option. There, you will be able to resize your PDF image without quality loss .

I meant, is there a way to make the output PDF page adapt its height/width/orientation automatically, based on the image actual size within that page? The output PDF would be generated by the end users, so we need to ensure the PDF looks well without a need to do manual changes.

@arjana3

Thanks for contacting support.

Would you kindly share your sample HTML document with us in a ZIP Archive. We will test the scenario in our environment and address it accordingly.

Here is a zip with an html ‘various_diagrams.html’ which references 3 different sizes of images:
sample.zip (1.7 MB)

The output pdf result.pdf is included as well. When preparing a sample, I noticed that in a case of an image that is short but is wider than a page, the page is made wider only if there are no other images in the document.

@arjana3

We were able to observe similar behavior of the API in our environment while using Aspose.PDF for Java 20.4. We have logged an issue as PDFJAVA-39425 in our issue tracking system for the sake of further investigation. We will look into these details and keep you posted with the status of ticket resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.