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

How to preserve resources during HTML string to PDF conversion

Platform: WinForms |
Control: PDF |
Published Date: November 24, 2017 |
Last Revised Date: December 7, 2018
Tags: htmltopdf, webkit

Syncfusion HTML to PDF for .NET is used to convert webpages, SVG, MHTML, and HTML to PDF. Using this library, you can preserve resources such as image, scripts, and CSS during HTML text to PDF conversion. While converting HTML string to PDF, you have the option to specify the base URL. The base URL is a path of the resources used in the HTML string.

Assemblies required

  • Syncfusion.Compression.Base.dll
  • Syncfusion.HtmlConverter.Base.dll
  • Syncfusion.Pdf.Base.dll
  • QtBinaries folder

You can get all the previously mentioned assemblies and QtBinaries by installing the HTML converter installer available in the following link.

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

NuGet

  1. Install the Syncfusion.HtmlToPdfConverter.QtWebKit.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

NuGet Installed location: C:\Users\<<user name>> \.nuget\packages\syncfusion.htmltopdfconverter.qtwebkit.winforms\XX.X.X.XX\lib.

  1. Copy the QtBinaries folder from the NuGet package installed location and paste it into your application folder to use it for conversion.

  1. Use the following namespaces and code snippet to preserve resources during HTML string to PDF conversion.

 

 

 

 

HTML string

<html>

<body Align=’Center’><br>

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

<p> Hello World</p>

</body>

</html>

 

For the previous HTML string, 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 follows,

Example baseURL: D:/temp/Resources/

 

Find the possible causes and solutions for missing resources while converting HTML string to PDF:

  1. Open the base URL in web browser and check the resources such as styles, scripts, and images are accessible in the machine, where the conversion takes place. If the resources are accessible in the web browser, then the converter can access the resources when converting the HTML string to PDF.  
  2. When you combine the Base URL with href link (images, styles, and scripts used in the HTML string), it should be a valid path. Otherwise, the browser/converter unable to access the resources from the base URL.
  3. Regarding OPENSSL

To convert or access the HTTTS sites, the HTML converter requires OPENSSL assemblies. If the base URL is HTTPS, make sure that your machine/server has the OPENSSL assemblies.  Refer to the prerequisites section for more information.

The following assemblies are placed in the windows system folder (for 64-bit machine, it should be placed in $SystemDrive\Windows\SysWOW64 and for 32-bit machine, it should be placed in $SystemDrive\Windows\System32),

  • libeay32.dll
  • libssl32.dll
  • ssleay32.dll
  1. Temporary HTML file 

While converting HTML string to PDF with base URL, the HTML string with Base URL is internally saved into temporary HTML file and use this file for further conversion. So, that the browser/converter can access the resources such as styles, images, and scripts from the provided base URL. 

The HTML converter preserves the output PDF document as the temporary HTML file that is displayed in web browser. So, create a temporary HTML file with HTML string and base URL, and check the contents in the web browser.

Refer to the following steps to create a temporary HTML file.   

1.      Copy the input HTML string and save it as a HTML file.   

2.      Open the HTML file in text editor and include the Base URL as base href in header tag as follows.  

<head><BASE HREF="https://mydomain.com/"></head>

3.      Save and view the HTML file in browser and check the contents. 

A complete working sample can be downloaded from HTMLStringtoPDF_Resource.zip

Take a moment to peruse the documentation for Converting HTML string to PDF, where you will find other options for URL to PDF, bookmarks, and hyperlinks.

An online sample link for Converting HTML to PDF.

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the 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