Convert HTML to PDF with plugin in C#/VB.NET

 

C#/VB.NETConvert HTML to PDF

An HTML file is a web page document commonly saved with a filename extension .html or .htm. Because of PDF portability and compatibility, it offers many advantages to convert HTML content into PDF. Since PDF supports a wide range of image compression, it will give you the benefit of taking little storage on your hard drive even with so much data held on one file. Don’t worry because even with compressed data, your converted HTML documents to PDF still retains the original text and layouts of your web page. You can save and share your PDFs on any device or operating system while maintaining document content, layout, and quality. HTML can be converted to PDF in using QT Web plugin. This article will show you how to convert HTML to PDF with plugin.

 

Install Free Spire.PDF for .NET

In this test, Free Spire.PDF for .NET is introduced into the program. The Spire.PDF.dll file can be referenced by:

 

Method 1: Download Free Spire.PDF for .NET locally, unzip it, and install it. After the installation is complete, find Spire.PDF.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

 

Method 2: Install via NuGet. It can be installed by the following 2 methods:

 

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.PDF", and click "Install". Wait for the program installation to complete.

 

(2) Copy the following content to the PM console installation.

 

Install-Package FreeSpire.PDF -Version 8.2.0

 

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: E:\\case\\plugins. And you need to set the "Platform target" of your project to x64 or x86 accordingly.

Specific Steps:

l  Get the HTML string from a .htm file.

l  Specify the output file path.

l  Specify the plugin path.

l  Through the HtmlConverter.PluginPath Property to set plugin path.

l  Convert HTML string to PDF using HtmlConverter.Convert()method.

Full Code:

C#

using System.IO;

using Spire.Pdf.HtmlConverter.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("True love knows no obstacles or distance.htm");

 

            //Specify the output file path

            string fileName = "HtmlStringToPdf.pdf";

 

            //Specify the plugin path

            string pluginPath = "E:\\case\\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), Spire.Pdf.HtmlConverter.LoadHtmlType.SourceCode);

        }

    }

}

VB.NET

Imports System.IO
Imports Spire.Pdf.HtmlConverter.Qt
Imports System.Drawing
Imports Spire.Pdf.Graphics

Namespace ConvertHtmlStringToPdfWithPlugin
    
    
Class Program
        
        
Private Shared Sub Main(ByVal args() As String)
            
'Get the HTML string from a .html file
            
Dim htmlString As String = File.ReadAllText("True love knows no obstacles or distance.htm")
            
'Specify the output file path
            
Dim fileName As String = "HtmlStringToPdf.pdf"
            
'Specify the plugin path
            
Dim pluginPath As String = "E:\case\plugins"
            
'Set plugin path
            
HtmlConverter.PluginPath pluginPath
            
'Convert HTML string to PDF
            
HtmlConverter.Convert(htmlString, fileName, true100000New Size(10801000), New PdfMargins(0), Spire.Pdf.HtmlConverter.LoadHtmlType.SourceCode)
        
End Sub
    End Class
End Namespace

 

Effective shot

 


Conclusion:

In this post, you have learned how to convert HTML to from PDF in C#/VB.NET and have learned the advantages of two different formats: PDF and HTML. Not only that, we also have other functions, such as converting Images to PDF, Multipage Image to PDF, PDF to Word and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.PDF for .NET.


Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application