We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

how to convert HTML code to pdf ?

Hie.

I would like to know if it's possible to send the HTML source code of a web page for conversion to PDF instead of the URL ?

thanks.


1 Reply

TE Thiruvenkadam E Syncfusion Team August 27, 2009 12:21 PM UTC

Hi Iulian,

Thank you for your interest in Syncfusion products.

Convert HTML code to pdf

We can able to convert HTML code to pdf using FromString(string html, ImageType type, int width, int height) method.

Kindly refer the below code snippet which helps to convert HTML code to pdf.


string htmlCode = "<html> <body> <font color='#FF0000F'>PDF stands for Portable Document Format </font> </body> </html>";
//Create a PDF document
PdfDocument doc = new PdfDocument();
//Add a page
PdfPage page = doc.Pages.Add();
PdfUnitConvertor convertor = new PdfUnitConvertor();
float width = -1;
float height = -1;
//Calculates the height and width of the pdf image
AspectRatio dimension = AspectRatio.None;
//Create a html converter.
HtmlConverter html = new HtmlConverter();
//Convert html to image
Image image = html.FromString(htmlCode, Path.GetFullPath(txtUrl.Text), ImageType.Metafile, (int)width, (int)height, dimension);
//Create a pdf metafile.
PdfMetafile mf = new PdfMetafile(image as Metafile);
mf.Quality = 100;
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
//Draw the metafile
mf.Draw(page, new Point(0, 0), format);
//Save the document.
doc.Save("Sample.pdf");


Please do find the sample from the below specified location which demonstrates how to convert HTML code to pdf and let me know if this helps,

http://files.syncfusion.com/support/PDF.Windows/F89490/HtmltoPdf.zip

Regards,
Thiru

Loader.
Live Chat Icon For mobile
Up arrow icon