2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
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
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
NuGet Installed location: C:\Users\<<user name>> \.nuget\packages\syncfusion.htmltopdfconverter.qtwebkit.winforms\XX.X.X.XX\lib.
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:
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),
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.
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.
|
2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.
This is an excellent resource.
How do you handle a URL that requires interactive authentication?