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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Converting an HTML file to PDF

Thread ID:





88814 Aug 12,2009 05:47 PM UTC May 29,2015 10:45 AM UTC ASP.NET Web Forms (Classic) 12
Tags: PDF
Naresh Macha
Asked On August 12, 2009 05:47 PM UTC

Examples shown takes a url and converts it into a PDF. is there ans example of converting an HTML file to PDF?

Thiruvenkadam E [Syncfusion]
Replied On August 13, 2009 11:52 AM UTC

Hi Naresh,

Thank you for your interest in Syncfusion products.

Converting an HTML file to PDF

We can able to converting an HTML file to PDF by using FromString(string html, string baseUrl, ImageType type, int width, int height, AspectRatio aspectRatio) method.

Kindly refer the below code snippet which helps to convert an HTML file to PDF.


StreamReader reader = new StreamReader(@"..\..\Template.html");
string html = reader.ReadToEnd();
//Create a new pdf document
PdfDocument pdf = new PdfDocument();
//Adds a new pdf page.
PdfPage page = pdf.Pages.Add();
PdfUnitConvertor converter = new PdfUnitConvertor();
float width = converter.ConvertToPixels(page.GetClientSize().Width, PdfGraphicsUnit.Point);

using (HtmlConverter html = new HtmlConverter())
//Renders html from the string to image.
PdfMetafile mf = new PdfMetafile(html.FromString(htmlContent,Path.GetFullPath(@"..\..\Template.html"),ImageType.Metafile, (int)width, -1, AspectRatio.KeepWidth) as Metafile);
mf.Quality = 100;

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
//Draws the image.
mf.Draw(page, new PointF(0, 0), format);

// Save the pdf document.

// Close pdfdocument

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



Naresh Macha
Replied On August 13, 2009 02:22 PM UTC

Hi Thiru,

Thank you very much for the fast response, I am having an issue with the code sample @ line

using (HtmlConverter html = new HtmlConverter())

The code does not compile it has a curly line underneath it with following description" 'Sync.HtmlConverter.HtmlConverter': type used in a using statement must be implicitly convertible to 'System.IDisposable' " and "The type 'System.Windows.Forms.UserControl' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'."

Please advice.

Thank you much.

Naresh Macha
Replied On August 13, 2009 02:39 PM UTC

Hi Thiru,

Thank very much for all your help. I have opened the solution but it is a windows form and i am trying to do it in web application is it possible to achive the same using web form?

Thank you,

Sri Subhashini M [Syncfusion]
Replied On August 14, 2009 12:09 PM UTC

Hi Naresh,

Sorry for the delay in getting back with you.

Here is the answers for your questions,

'System.Windows.Forms' Needed Assembly
We can resolve the issue by adding the 'System.Windows.Form' assemblies in your application. Kindly refer the below sample image for more information,


HTMLtoPDF Web Sample

Yes, we can convert an HTML file as a PDF document using Web application. Could you please do find the sample from the below specified location and let me know if this helps you?



Naresh Macha
Replied On August 14, 2009 02:08 PM UTC

Hi Suba,

I have added both the file you have sent me HtmlToPDF.aspx and Template.html(App_data folder), The program was looking for a reference to System.Windows.Forms i have added it. But i have a run time error doing so. I am attaching a samples.Zip file which has the entire solution and a word document showing the error i am getting.

Please adice.

Thank you,


Sri Subhashini M [Syncfusion]
Replied On August 17, 2009 04:53 AM UTC

Hi Naresh,

ActiveX control cannot be instantiated Exception

We were able to reproduce the issue which you mentioned here. We can resolve the problem by adding the @Page directive AspCompat="true" attribute to the page where we want to perform HTML to PDF conversions.

<%@ Page Language="C#" AspCompat="true" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" % >

Could you please try the code and let me know if this helps?


Naresh Macha
Replied On August 17, 2009 01:55 PM UTC

Hi Suba,

Thank you very much, your suggestion did work. I really appreciate all the effort you guys have taken to solve the issue.


Replied On August 24, 2009 03:42 PM UTC

Hi Thiru,

I try the sample that you have posted,

The problem was that I have a utf-8 html that contain

chinese font and try to convert it to the pdf document.

The result is that the chinese characters in the pdf document become garbage characters.

Could you please help me to solve the issue?


Sri Subhashini M [Syncfusion]
Replied On August 25, 2009 05:43 AM UTC

Hi Feng,

I fear that I was not able to reproduce the issue which you mentioned here.

Could you please send us the HTML page so that we could sort out the cause of the issue in depth and provide you the solution?


Replied On May 26, 2015 10:43 AM UTC

Hi Sri,

Can I have html to pdf converter for xamarin forms? If available then please send me sample.

Abirami Selvan [Syncfusion]
Replied On May 28, 2015 05:34 AM UTC

Hi Dnyani,

Thanks for using Syncfusion product.
Currently , We don’t have the support for html to pdf converter in Xamarin forms.
Please let us know if you have any queries.

Thanks ,

Abirami Selvan [Syncfusion]
Replied On May 29, 2015 10:45 AM UTC

Hi Dnyani,

Please ignore the previous update.

We would like to inform you that we have logged this as a feature request in our database. We will implement this feature in any of our upcoming releases. We usually have an interval of at least three months between releases. The feature implementation would also greatly depend on the factors such as product design, code compatibility and complexity. Please log on to our support website to check for further updates :


Please let us know if you need any further assistance.





This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon