Winnovative HTML to PDF Converter

HTML to PDF Converter Overview

Winnovative HTML to PDF Converter for .NET Documentation

The main functionality of the library is to convert HTML documents to PDF. But the library does much more than this. You can also convert HTML to raster images or HTML to SVG using the appropriate interfaces and you can edit, merge or split existing PDF documents.

HTML to PDF Conversion

There are two approaches to convert HTML to PDF. The first is to use the HtmlToPdfConverter class. The second approach is to create a Document object and to add a HtmlToPdfElement to it. Below are described in detail both methods.

Convert HTML to PDF using the HtmlToPdfConverter Class

The easiest approach is to use one of the HtmlToPdfConverter class methods to convert an URL or a HTML string to a PDF document. The resulted PDF document can be:

Code Sample - Convert HTML to PDF with HtmlToPdfConverter Class

// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

// Set HTML Viewer width in pixels which is the equivalent in converter of the browser window width
htmlToPdfConverter.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text);

// Set HTML viewer height in pixels to convert the top part of a HTML page 
// Leave it not set to convert the entire HTML
if (htmlViewerHeightTextBox.Text.Length > 0)
    htmlToPdfConverter.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text);

// Set PDF page size which can be a predefined size like A4 or a custom size in points 
// Leave it not set to have a default A4 PDF page
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize();

// Set PDF page orientation to Portrait or Landscape
// Leave it not set to have a default Portrait orientation for PDF page
htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation();

// Set the maximum time in seconds to wait for HTML page to be loaded 
// Leave it not set for a default 60 seconds maximum wait time
htmlToPdfConverter.NavigationTimeout = int.Parse(navigationTimeoutTextBox.Text);

// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
if (conversionDelayTextBox.Text.Length > 0)
    htmlToPdfConverter.ConversionDelay = int.Parse(conversionDelayTextBox.Text);

// The buffer to receive the generated PDF document
byte[] outPdfBuffer = null;

if (convertUrlRadioButton.Checked)
{
    string url = urlTextBox.Text;

    // Convert the HTML page given by an URL to a PDF document in a memory buffer
    outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
}
else
{
    string htmlString = htmlStringTextBox.Text;
    string baseUrl = baseUrlTextBox.Text;

    // Convert a HTML string with a base URL to a PDF document in a memory buffer
    outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlString, baseUrl);
}

// Send the PDF as response to browser

// Set response content type
Response.AddHeader("Content-Type", "application/pdf");

// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("{0}; filename=Getting_Started.pdf; size={1}",
    openInlineCheckBox.Checked ? "inline" : "attachment", outPdfBuffer.Length.ToString()));

// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);

// End the HTTP response and stop the current page processing
Response.End();

Another approach is to add HTML content to a PDF document using HtmlToPdfElement objects. The initial Document to which the HTML elements are added are either new documents created using one of the class constructor or documents initialized with the result of another HTML to PDF conversion using the HtmlToPdfConverterConvertUrlToPdfDocumentObject(String) and HtmlToPdfConverterConvertHtmlToPdfDocumentObject(String, String) methods.

Code Sample - Convert HTML to PDF with HtmlToPdfElement Class

// Create a PDF document
Document pdfDocument = new Document();

// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
pdfDocument.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

// Create a PDF page where to add the first HTML
PdfPage firstPdfPage = pdfDocument.AddPage();

try
{
    // The element location in PDF
    float xLocation = float.Parse(xLocationTextBox.Text);
    float yLocation = float.Parse(yLocationTextBox.Text);

    // The URL of the HTML page to convert to PDF
    string urlToConvert = urlTextBox.Text;

    // Create the HTML to PDF element
    HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, urlToConvert);

    // Optionally set the HTML viewer width
    htmlToPdfElement.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text);

    // Optionally set the HTML viewer height
    if (htmlViewerHeightTextBox.Text.Length > 0)
        htmlToPdfElement.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text);

    // Optionally set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels
    htmlToPdfElement.ClipHtmlView = clipContentCheckBox.Checked;

    // Optionally set the destination width in PDF
    if (contentWidthTextBox.Text.Length > 0)
        htmlToPdfElement.Width = float.Parse(contentWidthTextBox.Text);

    // Optionally set the destination height in PDF
    if (contentHeightTextBox.Text.Length > 0)
        htmlToPdfElement.Height = float.Parse(contentHeightTextBox.Text);

    // Optionally set a delay before conversion to allow asynchonous scripts to finish
    htmlToPdfElement.ConversionDelay = 2;

    // Add the HTML to PDF element to PDF document
    // The AddElementResult contains the bounds of the HTML to PDF Element in last rendered PDF page
    // such that you can start a new PDF element right under it
    AddElementResult result = firstPdfPage.AddElement(htmlToPdfElement);

    // Save the PDF document in a memory buffer
    byte[] outPdfBuffer = pdfDocument.Save();

    // Send the PDF as response to browser

    // Set response content type
    Response.AddHeader("Content-Type", "application/pdf");

    // Instruct the browser to open the PDF file as an attachment or inline
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Add_HTML_to_PDF_Elements_to_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));

    // Write the PDF document buffer to HTTP response
    Response.BinaryWrite(outPdfBuffer);

    // End the HTTP response and stop the current page processing
    Response.End();
}
finally
{
    // Close the PDF document
    pdfDocument.Close();
}
See Also

Reference

Other Resources