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

Converting an HTML file to PDF

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

12 Replies

TE Thiruvenkadam E Syncfusion Team 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.

[c#]

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.
pdf.Save(path);

// Close pdfdocument
pdf.Close(true);
}

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,


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


Regards,
Thiru


NM Naresh Macha 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=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'."

Please advice.

Thank you much.
-Naresh.


NM Naresh Macha 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,
-Naresh.


SS Sri Subhashini M Syncfusion Team 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,

http://files.syncfusion.com/samples/PDF.Windows/SampleImage_88814.zip

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?

http://files.syncfusion.com/samples/PDF.Windows/HtmlToPDF_Sample.zip

Regards,
Suba


NM Naresh Macha 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,
-Naresh.



Samples_515a9c4c.zip


SS Sri Subhashini M Syncfusion Team 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?

Regards,
Suba


NM Naresh Macha 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.

-Naresh.


CH Chernsah 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?
thx.

Regards,
Feng


SS Sri Subhashini M Syncfusion Team 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?

Regards,
Suba


DN Dnyani 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.



AS Abirami Selvan Syncfusion Team 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.


AS Abirami Selvan Syncfusion Team 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 :

http://www.syncfusion.com/support/directtrac/features/XAMARIN-388

Please let us know if you need any further assistance.

 

Regards,

Abirami.


Loader.
Live Chat Icon For mobile
Up arrow icon