What is best possible way to convert html(string) to pdf

Hi, I need to convert html string to pdf with following additional requirements:-

  1. Add header and footer image(.svg - vector image) in first page only
  2. Add page no. in top left from second page onwords.

I have seen many link on your site but did not found any spefific standard and efficent way for that, please assist.

Hi Jasbir,

Thanks for your inquiry. Please use following code example to achieve your requirements. Hope this helps you.

string inputSring = "Html string";
byte[] bArray = Encoding.UTF8.GetBytes(inputSring);
Document doc = new Document(new MemoryStream(bArray));
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup ps = builder.PageSetup;
ps.DifferentFirstPageHeaderFooter = true;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.InsertImage(MyDir + "in.svg");
builder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);
builder.InsertImage(MyDir + "in.svg");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Page ");
builder.InsertField("PAGE", "");
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
doc.Save(MyDir + "Out.pdf");

Thanks Tahir,

What a neat soltuion, this works like a charm, great help.

Any idea what others image format we can support, i am specifically looking for vector images.

And how can we inlcude external css in html string e.g. "
+ htmlstring

Hi Jasbir,

Thanks for your inquiry.

jasbirmaan067:
Any idea what others image format we can support, i am specifically looking for vector images.

Please note that Aspose.Words mimics the same behavior as MS Word does. Aspose.Words supports many image file format e.g. Svg, Emf, Jpeg, Bmp, Png, Tiff etc. Please try latest version of Aspose.Words for .NET 16.12.0 and let us know if you face any issue.

*jasbirmaan067:
And how can we inlcude external css in html string e.g. "

  • htmlstring
    You can use the same code shared in my previous post to load this html into Aspose.Words’ DOM.*