Wednesday, 22 June 2011 08:53

Spire.PDF for .NET Program Guide Content

Spire.PDF for .NET is a professional PDF API applied to creating, writing, editing, handling and reading PDF files without any external dependencies within .NET (C#, VB.NET, ASP.NET, .NET Core, .NET 5.0, .NET 6.0, .NET 7.0, MonoAndroid and Xamarin.iOS) application. Using this .NET PDF library, you can implement rich capabilities to create PDF files from scratch or process existing PDF documents entirely through C#/VB.NET without installing Adobe Acrobat.

Many rich features can be supported by the .NET PDF API, such as adding digital signature, including timestamp in signature, adding dynamic/image stamp, adding text/image watermark, creating PDF Portfolio, extracting text/attachment/images, PDF merging/spliting, metadata updating, section, graph/image drawing and inserting, table creation and processing, cropping pages, copying pages, and importing data etc.

Monday, 21 February 2022 08:45

C#/VB.NET: Convert HTML to PDF

Converting HTML content into PDF offers many advantages, including the ability to read it offline, as well as preserving the content and formatting with high fidelity. Spire.PDF provides two methods to convert HTML to PDF, one is to use QT Web plugin, the other is not to use plugin. We recommend that you use QT plugin to do the conversion.

The following sections demonstrate how to render an HTML webpage (URL) or an HTML string to a PDF document using Spire.PDF for .NET with or without QT plugin.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF 

Download Plugin

If you choose the plugin method, please download the plugin that fits in with your operating system from the following link.

Unzip the package somewhere on your disk to get the "plugins" folder. In this example, we saved the plugin under the path "F:\Libraries\Plugin\plugins-windows-x64\plugins‪‪".‬‬

C#/VB.NET: Convert HTML to PDF

Also, we recommend that you set the "Platform target" of your project to x64 or x86 accordingly.

C#/VB.NET: Convert HTML to PDF

Convert a URL to PDF with QT Plugin

The following are the steps to convert a URL to PDF using Spire.PDF with QT plugin.

  • Specify the URL path to convert.
  • Specify the path of the generated PDF file.
  • Specify the plugin path, and assign it as the value of the HtmlConverter.PluginPath property.
  • Call HtmlConverter.Convert(string url, string fileName, bool enableJavaScript, int timeout, SizeF pageSize, PdfMargins margins) method to convert a URL to a PDF document.
  • C#
  • VB.NET
using Spire.Pdf.Graphics;
using Spire.Additions.Qt; 
using System.Drawing;

namespace ConvertUrlToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Specify the URL path
            string url = "https://www.wikipedia.org/";

            //Specify the output file path
            string fileName = "UrlToPdf.pdf";

            //Specify the plugin path
             string pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";

            //Set the plugin path
             HtmlConverter.PluginPath = pluginPath;

            //Convert URL to PDF
            HtmlConverter.Convert(url, fileName, true, 100000, new Size(1080, 1000), new PdfMargins(0));
        }
    }
}

Convert an HTML String to PDF with QT Plugin

The following are the steps to convert an HTML string to PDF using Spire.PDF with QT plugin.

  • Get the HTML string from a .html file.
  • Specify the path of the generated PDF file.
  • Specify the plugin path, and assign it as the value of the HtmlConverter.PluginPath property.
  • Call HtmlConverter.Convert(string htmlString, string fileName, bool enableJavaScript, int timeout, SizeF pageSize, PdfMargins margins, Spire.Pdf.HtmlConverter.LoadHtmlType htmlType) method to convert HTML string to a PDF document.

Note: Only inline CSS style and internal CSS style can be rendered correctly on PDF. If you have an external CSS style sheet, please convert it to inline or internal CSS style.

  • C#
  • VB.NET
using System.IO;
using Spire.Additions.Qt; 
using System.Drawing;
using Spire.Pdf.Graphics;

namespace ConvertHtmlStringToPdfWithPlugin
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get the HTML string from a .html file
            string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html");

            //Specify the output file path
            string fileName = "HtmlStringToPdf.pdf";

            //Specify the plugin path
            string pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";

            //Set plugin path
            HtmlConverter.PluginPath = pluginPath;

            //Convert HTML string to PDF
            HtmlConverter.Convert(htmlString, fileName, true, 100000, new Size(1080, 1000), new PdfMargins(0), LoadHtmlType.SourceCode);       
        }
    }
}

Convert a URL to PDF Without Plugin

The following are the steps to convert a URL to PDF using Spire.PDF without plugin.

  • Create a PdfDocument object.
  • Create a PdfPageSettings object, and set the page size and margins through it.
  • Create a PdfHtmlLayoutFormat object, and set the IsWaiting property of it to true.
  • Specify the URL path to convert.
  • Load HTML from the URL path using PdfDocument.LoadFromHTML() method.
  • Save the document to a PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using System;
using Spire.Pdf;
using System.Threading;
using Spire.Pdf.HtmlConverter;
using System.Drawing;

namespace ConverUrlToPdfWithoutPlugin
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Create a PdfPageSettings object
            PdfPageSettings setting = new PdfPageSettings();

            //Save page size and margins through the object
            setting.Size = new SizeF(1000, 1000);
            setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);

            //Create a PdfHtmlLayoutFormat object
            PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();

            //Set IsWaiting property to true
            htmlLayoutFormat.IsWaiting = true;

            //Specific the URL path to convert
            String url = "https://www.wikipedia.org/";

            //Load HTML from a URL path using LoadFromHTML method
            Thread thread = new Thread(() =>
            { doc.LoadFromHTML(url, true, true, false, setting, htmlLayoutFormat); });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

            //Save the document to a PDF file
            doc.SaveToFile("UrlToPdf.pdf");
            doc.Close();
        }
    }
}

Convert an HTML String to PDF Without Plugin

The following are the steps to convert an HTML string to PDF using Spire.PDF without plugin.

  • Create a PdfDocument object.
  • Create a PdfPageSettings object, and set the page size and margins through it.
  • Create a PdfHtmlLayoutFormat object, and set the IsWaiting property of it to true.
  • Read the HTML string from a .html file.
  • Load HTML from the HTML string using PdfDocument.LoadFromHTML() method.
  • Save the document to a PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.HtmlConverter;
using System.IO;
using System.Threading;
using System.Drawing;

namespace ConvertHtmlStringToPdfWithoutPlugin
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Create a PdfPageSettings object
            PdfPageSettings setting = new PdfPageSettings();

            //Save page size and margins through the object
            setting.Size = new SizeF(1000, 1000);
            setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);

            //Create a PdfHtmlLayoutFormat object
            PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();

            //Set IsWaiting property to true
            htmlLayoutFormat.IsWaiting = true;

            //Read html string from a .html file
            string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html");

            //Load HTML from html string using LoadFromHTML method
            Thread thread = new Thread(() =>
            { doc.LoadFromHTML(htmlString, true, setting, htmlLayoutFormat); });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

            //Save to a PDF file
            doc.SaveToFile("HtmlStringToPdf.pdf");
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Page 245 of 245