The majority of the websites are already able to produce reports or to present various results in HTML pages. While the HTML content is simple generate and edit it is not suitable for printing or for transmission by email. The de facto standard for printing is the PDF format. The HiQPdf HTML to PDF Converter for .NET can be used in your .NET applications to transform any HTML page into a PDF document preserving the original aspect of the HTML document.

Building the Sample

The demo is an ASP.NET website in C# offering sample code for the most important features of the software. To build the sample you have to open the Visual Studio solution and build the project. All the necessary files are referenced from NuGet and will be automatically copied from there. After build you can run the demo application in a browser. The same demo application is also available online at HiQPdf HTML to PDF Demo .

Description

This sample shows you how to quickly create PDF reports from existing HTML reports. The HTML report can be any HTML page containing HTML5 code, CSS3, JavaScript or SVG. Additionally you can convert the HTML pages to raster images (JPEG, PNG or BMP) and to SVG vector images.

An useful feature for creating reports is the support for @media rules in CSS to have different layouts for Screen and Print. Using CSS media types in a HTML document you can have one layout for screen, one for print and one for handheld devices. The @media rule allows different style rules for different media in the same style sheet in a HTML document. By default the HTML to PDF converter will render the HTML document for 'screen', but can layout the document for another media type by simply setting a property in your code.

The support for repeating HTML table headers and footers in each PDF page helps you easily create good looking printable tables. The page breaks control using API properties or CSS styles is another key feature when designing your reports.

The HTML to PDF conversion engine is the most important feature, but the HiQPdf software is a complete PDF Library for .NET. You can create new PDF documents and add text and images at the desired positions in pages in a traditional manner, open and edit external PDF documents by adding new objects, create PDF documents with interactive forms, fill and submit interactive forms, merge and split PDF documents, extract text and images from PDF documents, search text in PDF documents, rasterize PDF pages to images.

The HiQPdf Library for .NET offers you the fastest and the most precise HTML to PDF conversion technology to use in your Web and Desktop applications. The HTML to PDF Converter can convert to PDF any HTML document or URL that a modern browser can display, preserving all the CSS styles and executing all the JavaScript scripts found in the HTML document.

Besides the common features a HTML to PDF Converter must have, the HiQPdf component has some unique features on the market like the support for web fonts in Web Open Font Format (WOFF), the ability to convert only a selected region of the HTML document or the possibility to overlap multiple HTML documents in the same PDF while preserving the transparent backgrounds and images from the HTML document
Convert Modern HTML5 Documents with CSS3, SVG, Canvas and JavaScript.

The HTML documents can contain last generation content like Scalar Vector Graphics, Canvas and CSS3 styles. The JavaScript engine is fast, can handle complex scripts and exposes in HTML page objects you can use to manually trigger conversion or to determine various information about converter during conversion

The HiQPdf HTML to PDF Converter can render HTML documents using Web Fonts in formats like Web Open Font Format (WOFF), TrueType or OpenType with TrueType Outlines. The Web Fonts offer a great flexibility to web designers to create special effects on text in a HTML document because they are not limited anymore to a small set of fonts installed on the client computers displaying the HTML document. The Web Fonts are downloaded on the fly by converter and used to render the HTML document to PDF without installing those fonts on the local machine. The location from where the fonts can be downloaded is given in a CSS3 @font-face rule.

The HiQPdf Software does not depend on installed browsers, printer drivers, viewers or any other third party software. The HiQPdf Library for .NET can be deployed by simply copying it on the server. It is also possible to install the library in the .NET Framework GAC

The code below shows how configure the basic options of the converter when converting a web page or a HTML code to PDF.

C#
Edit|Remove
// create the HTML to PDF converter 
HtmlToPdf htmlToPdfConverter = new HtmlToPdf(); 
 
// set browser width 
htmlToPdfConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text); 
 
// set browser height if specified, otherwise use the default 
if (textBoxBrowserHeight.Text.Length > 0) 
    htmlToPdfConverter.BrowserHeight = int.Parse(textBoxBrowserHeight.Text); 
 
// set HTML Load timeout 
htmlToPdfConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text); 
 
// set PDF page size and orientation 
htmlToPdfConverter.Document.PageSize = GetSelectedPageSize(); 
htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation(); 
 
// set the PDF standard used by the document 
htmlToPdfConverter.Document.PdfStandard = checkBoxPdfA.Checked ? PdfStandard.PdfA : PdfStandard.Pdf; 
 
// set PDF page margins 
htmlToPdfConverter.Document.Margins = new PdfMargins(5); 
 
// set triggering mode; for WaitTime mode set the wait time before convert 
switch (dropDownListTriggeringMode.SelectedValue) 
{ 
    case "Auto": 
        htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto; 
        break; 
    case "WaitTime": 
        htmlToPdfConverter.TriggerMode = ConversionTriggerMode.WaitTime; 
        htmlToPdfConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text); 
        break; 
    case "Manual": 
        htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Manual; 
        break; 
    default: 
        htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto; 
        break; 
} 
 
// set header and footer 
SetHeader(htmlToPdfConverter.Document); 
SetFooter(htmlToPdfConverter.Document); 
 
// convert HTML to PDF 
byte[] pdfBuffer = null; 
 
if (radioButtonConvertUrl.Checked) 
{ 
    // convert URL to a PDF memory buffer 
    string url = textBoxUrl.Text; 
 
    pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url); 
} 
else 
{ 
    // convert HTML code 
    string htmlCode = textBoxHtmlCode.Text; 
    string baseUrl = textBoxBaseUrl.Text; 
 
    // convert HTML code to a PDF memory buffer 
    pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl); 
} 
 
// inform the browser about the binary data format 
HttpContext.Current.Response.AddHeader("Content-Type""application/pdf"); 
 
// let the browser know how to open the PDF document, attachment or inline, and the file name 
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=HtmlToPdf.pdf; size={1}", 
    checkBoxOpenInline.Checked ? "inline" : "attachment", pdfBuffer.Length.ToString())); 
 
// write the PDF buffer to HTTP response 
HttpContext.Current.Response.BinaryWrite(pdfBuffer); 
 
// call End() method of HTTP response to stop ASP.NET page processing 
HttpContext.Current.Response.End();

Source Code Files

More Information

For more details see a complete list of the HiQPdf HTML to PDF Features and the Online HTML to PDF Converter Documentation