Convert HTML to Single Page PDF using Aspose.PDF for .NET

Hi Team,


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

We tried with MoveNP in PdfFileEditor, it is merging the All the Pages to One page along with Margin and other page settings. The expected result we need is the content should be continued as it is. PFA we tried to implement this feature.

Please find us a good approach on this asap.

We are having a Site OEM license with our organization Factset Research Systems.
Kindly prioritize the response and resolution as we are in production environment.

Thanks,
Shanthi

Hi Shanthi,

Thanks for your inquriy. You may set PageInfo Width and Height property to convert HTML to a single Page PDF document as following. However, if later you want to resize the PDF page size then you can use ResizeContents() method of PdfFileEditor class.

HtmlLoadOptions options = new HtmlLoadOptions();

options.PageInfo.Margin.Left = 0;
options.PageInfo.Margin.Right = 0;
options.PageInfo.Margin.Top = 0;
options.PageInfo.Margin.Bottom = 0;
options.PageInfo.Width = 800;
options.PageInfo.Height = 3000;

var pdfDocument = new Aspose.Pdf.Document(@"D:\Downloads\StoFit\stofit.html", options);

/*

// Resize contents of resultant PDF

pdfDocument.ProcessParagraphs();

int[] page_cnt1 = new int[pdfDocument.Pages.Count];

for (int i = 0; i < pdfDocument.Pages.Count; i++)
    page_cnt1[i] = i + 1;

PdfFileEditor pfe = new PdfFileEditor();

pfe.ResizeContents(pdfDocument, page_cnt1, PdfFileEditor.ContentsResizeParameters.PageResize(Aspose.Pdf.PageSize.A4.Width, Aspose.Pdf.PageSize.A4.Height));

*/

// Save output as PDF format

pdfDocument.Save(myDir + "stofit.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

How can I be sure of my content height.


My expectation is page height should be of content height

Please find the output as attachment

Hi Shanthi,


Thanks for your feedback. In reference to your requirements, currently we set page width/height manually. However, I have logged an enhancement ticket PDFNET-41253 in our issue tracking system for the requirement and will notify you as soon as it is resolved.

We are sorry for the inconvenience caused.

Best Regards,

@Shanthi555k

We would like to share with you that you can now convert HTML to Single Page PDF document using Aspose.PDF for .NET. In other words, you can now specify IsRenderToSinglePage property of HtmlLoadOptions Class in order to render HTML content on one page of PDF. Please consider using following code snippet:

HTML to PDF - Render all contents in single page

string outFile = "out.pdf";
HtmlLoadOptions options = new HtmlLoadOptions();
options.IsRenderToSinglePage = true;
Document doc = new Document("HTML/input.htm", options);
doc.Save(outFile);
1 Like

The issues you have found earlier (filed as PDFNET-41253) have been fixed in Aspose.PDF for .NET 23.3.