16-09-2021

For web developers, it's a lot easier to create a PDF from the HTML format, basically because it provides a better option to manage the PDF. For this task, the web developers use libraries like wkhtmltopdf. wkhtmltopdf is a command line tools to render HTML into PDF using the QT Webkit rendering engine. These run entirely 'headless' and do not require a display or display service.

In this article, you will learn how to use wkhtmltopdf easily with C# in your Windows Forms application.

Pdf

Requirements

The HTML document file, converted by C#.NET PDF to HTML converter toolkit SDK, preserves all the original anchors, links, bookmarks and font style that are included in target PDF document file. Besides, the converted HTML webpage will have original formatting and interrelation of text and graphical elements of the PDF. In another post I will go into some of the details particular to PDF to EPUB conversion, but everything in today’s post applies to both HTML and EPUB output. Furthermore, while PDFNet is available in C/C, Java, Objective-C, Python, Ruby, PHP, VB and C#, due to its popularity I decided to do this demo in C#. C# HTML to PDF – How to generate PDF files from HTML in C#.Net Applications and websites. This developer software download contains a C# PDF generation and editing library in addition to a full tutorial on how it may be used. Do you need to convert PDF files to HTML in large numbers or regularly? Download our PDF to HTML SDK and use its Action Center to generate an APP, sample code in C#, Java, PHP or Python, and include it in your workflow. Jan 27, 2020 PDF Online's free PDF-to-HTML tool extracts the images to a separate directory, writes the HTML, and keeps the hyperlinks that you already have in your PDF file. Links are the essential ingredient of the web, so the fact that this tool maintains them is critical to the functionality of the resulting web pages it creates.

  • You will need Visual Studio with NuGet Package Manager (>=2010).

In our case, we are going to use Visual Studio Community, so let's get started !

1. Install Pechkin

To generate a PDF from a HTML file with C# in Windows Forms, we are going to use Pechkin, the .NET Wrapper for wkhtmltopdf DLL, library that uses Webkit engine to convert HTML pages to PDF. Pechkin is available as a NuGet package, in most cases you should use SynchronizedPechkin as it protects multithreaded code from crashing the lib. But for simple usage from one thread, you can use SimplePechkin directly.

Note

Pechkin includes the latest version of wkhtmltox DLL in the project (and in NuGet package) along with its dependencies, and copied into build folder on project build. So there's no need to install any prerequisites before using the library on the computer.

Install the package with Visual Studio, by accessing the NuGET package manager. Do right click in your project in the Solution Explorer at the right top of Visual Studio and select Manage NuGet packages in the dropdown menu:

Now from the emergent window search for the Pechkin.Synchronized package, select it and install it in your project:

Once the setup finishes, you will be able to use the wrapper in your project.

2. Generate a PDF

The Convert method converts a HTML string (or a configuration object) to PDF with the given settings and returns a byte arrays that store the binary data of the PDF.

Note

With all the examples, the pdfContent variable will generate only the content of the PDF and you won't see anything, therefore don't forget to write the created content into a file by following the step #3.

A. Generate simple PDF from a HTML string

You can create a PDF quickly with an empty global configuration object. Import the Pechkin type in your class:

And then create the content of the PDF with the following snippet:

B. Generate PDF from a Website

Instead of a plain HTML string, you can render a website through it's URL with the SetPageUri method in the configuration. Import the Pechkin.Synchronized type at the top of your class:

Pdf To Html5 C# Free

Pdf to html5 c# youtube

And then use the following code to create a PDF from a website:

Pdf to html5 c# download

C. Generate PDF from a local HTML file

To create a PDF from a local HTML file, you can do it by providing the local filepath of the file with the SetPageUri method (in the same way that you do with a website) but at the same time, allow the usage of local resources with the SetAllowLocalContent method (note that you need to append as prefix file:/// to the html filepath). Import the Pechkin.Synchronized type at the top of your class:

And use the following code to create a PDF from a local HTML file:

Now that you know how to generate the content of a PDF, you only need to learn how to save it.

3. Save the PDF

As mentioned previously, Pechkin generates a PDF and return the data as a byte array, that means that you will need to write that data somehow by yourself. We will write the PDF into a file using the FileStream class included in the assembly System.IO. With FileStream you can read from, write to, open, and close files on a file system, as well as to manipulate other file related operating system handles including pipes, standard input, and standard output. Before using it, import the following type with the using directive in the top of your class:

And then, use the class to create a file with the returned ByteArray as content. You can use the following method that expects as first parameter the absolute path of the file and as second parameter the data to write (pdf content):

Then it can be used as shown in the following example:

Happy coding !

Pdf To Html5 C 230

Using GroupDocs.Conversion for .NET, it is possible to convert any PDF document to HTML file in C#. Conversion process or feature is not limited to PDF files only. You can convert any supported file format to HTML.
GroupDocs.Conversion for .NET is a back-end API that can be integrated in any (new or existing) .NET application.

C# Example: How to Convert a PDF to HTML

This API can convert any source document to a markup format which in general is an HTML conformable format.

View the code on Gist.

You can see that sample PDF has some visuals and text. And the resultant HTML covers everything.

Pdf To Html5 C# File

You can download our GitHub open-source example project in order to evaluate the API. In case of any issue, you can raise it on forum.