Aspose HTML to PDF creating multi page PDF

Hi Team,

We had a requirement of converting HTML to PDF in one page PDF in java, where all the content of HTML should adjust/fit in one Single Page irrespective of height and width of the content.

Whatever I tried, resulting PDF was still in multiple pages and distorted. Could you help with proper solution for this issue ASAP.

Attaching HTML for reference.

@shipragupta0755 You can check page count in the resulting document before conversion and then increase page height so the content fit one page:

Document doc = new Document("C:\\Temp\\in.html");

PageSetup pageSetup = doc.getFirstSection().getPageSetup();
double pageContentHeight = pageSetup.getPageHeight() - pageSetup.getTopMargin() - pageSetup.getBottomMargin();

// Check whether document fits one page. If not increase page height.
while (doc.getPageCount() > 1)
{
    double addition = doc.getPageCount() > 2 ? (doc.getPageCount() - 1) * pageContentHeight : 10;
    pageSetup.setPageHeight(pageSetup.getPageHeight() + addition);
    doc.updatePageLayout();
}

doc.save("C:\\Temp\\out.pdf");

Please note, the maximum height of page in MS Word is 1584 pt, so you cannot set page height beyond this value using Aspose.Words.

Hi Alexey,

Thank you for the solution, it worked properly. Whole resultant PDF was on single page. But there is 1 more issue somehow placement of images are changed in final PDF, i.e., resultant PDF is distorted. Could you help with that too?

Regards,
Shipra Gupta

@shipragupta0755 Could you please attach the problematic input and output documents here for testing? We will check the issue and provide you more information.

Also, please note, Aspose.Words is designed to work with MS Word documents at first. While loading HTML document, it is converted to Aspose.Words DOM and due to differences in HTML documents and MS Word documents object models it is not always possible to provide 100% fidelity after processing HTML document.

Hi @alexey.noskov,

Please find attached input filepdf_sample.zip (12.0 KB)
. Thank you for your guidance.

@shipragupta0755
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): WORDSNET-25956

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.