How to preserve resources during HTML string to PDF conversion
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
- 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.
- Copy the QtBinaries folder from the NuGet package installed location and paste it into your application folder to use it for conversion.
- Use the following namespaces and code snippet to preserve resources during HTML string to PDF conversion.
C#
using Syncfusion.HtmlConverter; using Syncfusion.Pdf; using System.IO;
VB.NET
Imports Syncfusion.HtmlConverter Imports Syncfusion.Pdf Imports System.IO
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 Align='Center'><br><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);
VB.NET
'Initialize HTML to PDF converter Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter(HtmlRenderingEngine.WebKit)Dim settings As WebKitConverterSettings = New WebKitConverterSettings() 'Set WebKit path settings.WebKitPath = "../../QtBinaries/" 'Assign WebKit settings to HTML converter htmlConverter.ConverterSettings = settings 'HTML string with image Dim htmlString As String = "<html><body Align='Center'><br> <img src=""syncfusion_logo.gif"" alt=""Syncfusion_logo"" width=""200"" height=""70""> <p> Hello World</p></body></html>" Dim baseUrl As String = Path.GetFullPath("../../Resources/") Dim document As PdfDocument = htmlConverter.Convert(htmlString, baseUrl) 'Save and close the PDF document document.Save("Output.pdf") document.Close(true)
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:
- 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.
- 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.
- 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
- 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.
- Copy the input HTML string and save it as a HTML file.
- 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>
- 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.