How to convert HTML to PDF in .NET dynamically from URL for an IIS-hosted Application

asp-net-generate-pdf-html-itextsharp

When it comes to working with .NET, there aren’t too many open source libraries that can really help you create a solid product. But TuesPechkin is one of those fine crafted libraries that is both open source and easy to work with. TuesPechkin, is a derivative of Pechkin library which was used to convert HTML pages to PDF on the code behind. But the development of Pechkin was stopped and the library had unanswered and open issues. TuesPechkin comes with all the neat features of the pechkin library. And also comes with support for any issues.

Using TuesPechkin is pretty straight forward and you will find it really easy to implement it in your solution and get up and running in seconds. To start, install the (https://www.nuget.org/packages/TuesPechkin/) TuesPechkin nuget package along with the TuesPechkin.Wkhtmltox nuget package. TuesPechkin.Wkhtmltox comes with both (https://www.nuget.org/packages/TuesPechkin.Wkhtmltox.Win32/)32-bit and (https://www.nuget.org/packages/TuesPechkin.Wkhtmltox.Win64/0.12.2.1)64-bit support.

Once you have installed the nuget packages, prepare your document

var document = new HtmlToPdfDocument
{
GlobalSettings =
{
ProduceOutline = true,
DocumentTitle = “Pretty Websites”,
PaperSize = PaperKind.A4, // Implicit conversion to PechkinPaperSize
Margins =
{
All = 1.375,
Unit = Unit.Centimeters
}
},
Objects = {
new ObjectSettings { HtmlText = “<h1>Pretty Websites</h1><p>This might take a bit to convert!</p>” },
new ObjectSettings { PageUrl = “www.google.com” },
new ObjectSettings { PageUrl = “www.microsoft.com” },
new ObjectSettings { PageUrl = “www.github.com” }
}
};

GlobalSettings defines the settings for the PDF document e.g. Margins, PaperSize etc. Objects contains the number of pages that your PDF will contain. You can either just provide HtmlText or a PageUrl in the ObjectSettings instance. Once you have prepared the document, you need to prepare a converter. Now if you are planning to deploy your solution on an IIS-hosted application, you need to keep the converter somewhere static, or as a singleton instance. Making sure that the converter code only runs once during the application cycle.

Now, to do as explained. Create a service like below for your solution

public static class TuesPechkinInitializerService
{
private static string staticDeploymentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “wkhtmltopdf”);

public static void CreateWkhtmltopdfPath()
{
if (Directory.Exists(staticDeploymentPath) == false)
{
Directory.CreateDirectory(staticDeploymentPath);
}
}

public static IConverter converter =
new ThreadSafeConverter(
new RemotingToolset<PdfToolset>(
new Win32EmbeddedDeployment(
new StaticDeployment(staticDeploymentPath)
)
)
);
}

Now, if you find the error Win32EmbeddedDeployment not resolved in the above. You need to install the TuesPechkin.Wkhtmltox.Win32 nuget package. After you have create your service, initialize your service in your Global file when the application starts.

TuesPechkinInitializerService.CreateWkhtmltopdfPath();

Once you have created and initialized the service, you just need to call the converter whenever you want to convert your document like below

TuesPechkinInitializerService.converter.Convert(document);

If you followed the steps correctly, you would find no problem in converting your HTML to PDF. If the system hangs on coversion, you might want to try to switch from Win32EmbeddedDeployment to Win64EmbeddedDeployment.

Let us know in the comments section below how TuesPechkin helped you achieve your product. Follow us on all social channels for more tutorials like this.

, ,
Previous Post
Database Testing Checklist: All You Need to Know
Next Post
Creating a custom Dashboard or using Power BI or Tableau

3 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu