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.

How to preserve resources during HTML string to PDF conversion

The WebKit HTML converter allows you to convert the HTML string to PDF. While converting HTML string to PDF, you have the option to specify the base URL. The base URL is nothing but, the path of the resources (Images, scripts, CSS, etc.,) used in the HTML string.

 

Assemblies Required

  1. Syncfusion.Compression.Base.dll
  2. Syncfusion.HtmlConverter.Base.dll
  3. Syncfusion.Pdf.Base.dll
  4. QtBinaries folder

All the above mentioned assemblies can be get by installing the WebKit installer available in the below link.

WebKit HTML Converter: https://www.syncfusion.com/downloads/latest-version

HTML String:

<html>

<body>

<img src="syncfusion_logo.gif" alt="Syncfusion_logo" width="200" height="70">

<p> Hello World</p>

</body>

</html>

 

For above HTML string, we need to provide the path of syncfusion_logo.gif as baseURL.

 

For example, if the above image is in “D:/temp/Resources/syncfusion_logo.gif” location then the baseURL will be as below,

Example baseURL: D:/temp/Resources/

Code snippet:

C#

//Initialize HTML to PDF converter

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

WebKitConverterSettings settings = new WebKitConverterSettings();

 

//Set WebKit path

settings.WebKitPath = @"/QtBinaries/";

 

//Assign WebKit settings to HTML converter

htmlConverter.ConverterSettings = settings;

 

//html string with image

string htmlString = "<html><body><img src=\"syncfusion_logo.gif\" alt=\"Syncfusion_logo\" width=\"200\" height=\"70\"><p> Hello World</p></body></html>";

            

//Path to the resources; Path of syncfusion_logo.gif

string baseUrl = Path.GetFullPath(@"../../Resources/");

 

//Convert HTML string to PDF

PdfDocument document = htmlConverter.Convert(htmlString, baseUrl);

 

//Save and close the PDF document

document.Save("Output.pdf");

 

document.Close(true);

 

Working Sample can be downloaded from the below link.

http://www.syncfusion.com/downloads/support/directtrac/general/ze/HTMLStringToPDF1113182506

 

Article ID: Published Date: Last Revised Date: Platform: Control:
8177 11/24/2017 11/27/2017 ASP.NET MVC (Classic) PDF
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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.