This project has moved. For the latest updates, please go here.

Free HTML to PDF Converter for .NET Documentation

Using Free HTML to PDF Library for .NET, with just a few lines of code, you can convert an URL or a HTML code to a PDF buffer in memory or to a PDF file on disk.

This document is a reference material for the software developers using our component to create or edit PDF documents. It contains a detailed description of the software capabilities, code samples and a complete API reference.

Using HiQPdf software component does not require any knowledge of PDF format. The PDF objects are encapsulated in a high level representation, the API is simple and well structured.

Convert URL to a PDF Buffer Example

// create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// convert URL to a PDF buffer in memory
// the buffer can be saved to a file, a stream or a HTTP response
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory("http://www.google.com");

The Document property of the HiQPdf.HtmlToPdf class having the PdfDocumentControl type is used to control most of the properties of the generated PDF document like page size, page orientation, page margins, header and footer, outlines, font embedding, the document standard and the document security.

The PageSize property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page size. The default value of this property is A4. The final width and height of the PDF pages is also influenced by the PageOrientation property. For example, if the initial page orientation was portrait and then it is set to landscape then the width of and height sizes are swapped, the width becoming bigger than the height.

The PageOrientation property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page orientation. The default value of this property is Portrait.

The Margins property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page margins. By default the generated document margins are all 0.

The FitPageWidth property of the HiQPdf.PdfDocumentControl class controls if the content is scaled down to fit the PDF page width. This property is true by default.

The HTML width is given by the BrowserWidth property of the HiQPdf.HtmlToPdf class which is 1200 pixels by default. At a default DPI of 96 the HTML width is 12.5 inch which is larger than the default A4 portrait page width and the HTML is scaled down to fit the PDF page which means that the text and images might appear smaller than they are in HTML.

If the FitPageWidth property is set to false the HTML is not scaled down and by default only a part of the 1200 pixels of HTML page would be displayed in a A4 portrait page. But when FitPageWidth is false another property ResizePageWidth comes into play. By default the ResizePageWidth is true and the PDF page width can be resized to display the whole HTML without scaling.

The FitPageHeight property of the HiQPdf.PdfDocumentControl class controls if the content is scaled down to fit the PDF page height. This property is false by default.

If the FitPageWidth property is false, the PostCardMode property of the HiQPdf.PdfDocumentControl class controls if the content will be rendered in one PDF page resized to display the entire HTML. This property is false by default.

There are also a few properties of the HTML converters controlling the internal browser behavior. The most important are described in the sections below.

The BrowserWidth property of the HiQPdf.HtmlToPdf class is a property having a very high influence against the HTML content rendering. Changing this property is basically the equivalent of resizing a web browser window when viewing a web page. By default the Browser Width is 1200 pixels and this is suitable for displaying most of the web pages.

The TrimToBrowserWidth controls if the browser window width is forced to have the width given by the BrowserWidth property. If the HTML document cannot be entirely displayed in BrowserWidth pixels and the TrimToBrowserWidth is false then the browser window width will be automatically set to display the entire HTML document. If the HTML document cannot be entirely displayed in BrowserWidth pixels and the TrimToBrowserWidth is true then the browser window width will not be automatically set to display the entire HTML document and the HTML document will be trimmed. The default value of this property is false.

Control Page Breaks in PDF with CSS Attributes

The page breaks in the generated PDF document can be controlled using the following CSS properties:

  • page-break-before : always style forces a page break in PDF right before the box where the element is rendered
  • page-break-after : always style forces a page break in PDF right after the box where the element is rendered
  • page-break-inside : avoid style will make the converter to generate the PDF such that the element with this style is not cut between PDF pages if possible.

Below there is a HTML document which demonstrates the usage of the three styles controlling the page breaks in the generated PDF document and the code of a demo application to convert that HTML document to PDF.

Select Media Type to Render a Different Layout for Screen or for Print

Using CSS media types a HTML document can have one layout for screen, one for print , 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 this can be changed when another media type is assigned to MediaType property. For example, when this property is set to 'print' the CSS properties defined by the '@media print' rule will be used when the HTML is rendered instead of the CSS properties defined by the '@media screen' rule.

Below there is a HTML document which demonstrates how to define different styles for 'screen' and for 'print' media types. 

<html>
<head>
<title>
    HTML to PDF Rendering Changes Based on Selected Media Type
</title>
<style type="text/css">
    body {
        font-family: 'Arial';
        font-size: 16px;
    }

    @media screen
    {
        p
        {
            font-family: Verdana;
            font-size: 14px;
            font-style: italic;
            color: Green;
        }
    }
    @media print
    {
        p
        {
            font-family: 'Courier New';
            font-size: 12px;
            color: Black;
        }
    }
    @media screen,print
    {
        p
        {
            font-weight: bold;
        }
    }
</style>
</head>
<body>
    <br /><br />
    The style of the paragraph below is changed based on
    the selected media type:
    <br /><br />
    <p>
    This is a media type selection test. When viewing on screen
    the text is bigger, italic and green. When printing the
    text is smaller, normal and black.
    </p>
</body>
</html>

You can find detailed documentation about all converter features on HiQPdf Website in HiQPdf HTML to PDF for .NET Blog .

Last edited Sep 15, 2016 at 10:23 AM by hiqpdf, version 4