Free Support Forum - aspose.com

Converting html to pdf with IsRenderToSinglePage=true (Aspose.PDF for .NET)

Hello support team,

Converting html to pdf with IsRenderToSinglePage=true (it renders whole html document to the single pdf page).
It is working well, but noticed issue that there are always blank area at the bottom of the document. The blank area depends of the content height.

I suspect that converting algorithm is to convert html to pdf by splitting them into pages and combining them together.

See the input (there are html file in zip package)
Input document.zip (1.6 KB)

See the result:
Result document.pdf (103.3 KB)

The expectation:
Untitled.png (84.8 KB)

The conversion code:

        MemoryStream resultStream = new MemoryStream();
        using (MemoryStream htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html)))
        {
            HtmlLoadOptions options = new HtmlLoadOptions();
            if (contentWidth != null)
            {
                options.PageInfo.Width = contentWidth.Value;
            }
            
            options.IsRenderToSinglePage = true;
            options.IsEmbedFonts = true;
            options.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

            Document pdfDocument = new Document(htmlStream, options);               
            pdfDocument.Save(resultStream);
        }
        return resultStream;

How we can avoid the blank space/area at the bottom of the pdf document?
We tried to set options.PageInfo.Height property and it’s working, but the problem is that we don’t know the exact height of the html content.

P.S. Using the latest Aspose.PDF for .NET (21.4.0.0).

Thank you!
Martin

@martin27

I request you to try trimming the white space of the page with below code and then share your feedback.

Document document = new Document("Result document.pdf");
Page page = document.Pages[1];
Rectangle rectangle = page.CalculateContentBBox();
rectangle.LLX = 0;
rectangle.URX = page.Rect.URX;
page.CropBox = rectangle;
page.MediaBox = rectangle;
document.Save("Trimmed.pdf");

I have attached output PDF file. Trimmed.pdf (103.4 KB)

This code solved my issue.
Thank you for your help!