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.
Unfortunately, activation email could not send to your email. Please try again.

Converting an HTML file to PDF

Thread ID:

Created:

Updated:

Platform:

Replies:

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

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 07:52 AM

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

Naresh Macha
Replied On August 13, 2009 10:22 AM

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.

Naresh Macha
Replied On August 13, 2009 10:39 AM

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.

Sri Subhashini M [Syncfusion]
Replied On August 14, 2009 08:09 AM

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

Naresh Macha
Replied On August 14, 2009 10:08 AM

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

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

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

Naresh Macha
Replied On August 17, 2009 09:55 AM

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.

Chernsah
Replied On August 24, 2009 11:42 AM

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

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

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

Dnyani
Replied On May 26, 2015 06:43 AM

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 01:34 AM

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.

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

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.


CONFIRMATION

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.

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.

;