Convert HTML to PDF Using DynamicPDF HTML Converter

by James B

Tue, August 18 2020, 12:50 PM (US Eastern Time)

Introduction

The DynamicPDF HTML Converter product is available and included with any of our subscription plans (subscription pricing). You can also use DynamicPDF HTML Converter free if you accept a small footer stating "Created with the DynamicPDF Essentials Edition" and link to the DynamicPDF website from a public area of your website. DynamicPDF HTML Converter supports the following features,

  • quickly converts HTML to PDF,
  • supports the latest HTML, CSS, and JavaScript,
  • works with files, byte arrays, and URLs,
  • supports custom headers and footers,
  • supports .NET Framework and .NET Core (including Linux),
  • and supports a transparent pricing model.

As this tutorial will demonstrate, DynamicPDF HTML Converter is an intuitive yet powerful library for converting HTML files, URLs, and webpages into PDF documents. A video tutorial is also included if you prefer watching the video rather than reading this tutorial. Or better yet, watch the video and work through the written tutorial. All source is available on Github, and the video is available here or on our YouTube channel.

The Converter and ConversionOptions Classes

The DynamicPDF HTML Converter namespace (ceTe.DynamicPDF.HtmlConverter documentation), consists of four classes,

and two enumerations,

Of these, the two classes most important for understanding this tutorial are the Converter and ConversionOptions classes.  The Converter class is how we convert the document from HTML to PDF using the static Convert or AsyncConvert methods. Both methods are overloaded to support different ways of processing HTML conversion. Although these two methods are all that is required, if you wish to perform any formatting of a PDF document, then you must use the ConversionOptions class to format that PDF. The ConversionOptions class supports specifying a PDF document's metadata, such as author, subject, and name. It also supports formating a PDF document by specifying page size, page orientation, headers, footers, and margins.

Now that we discussed the Converter and ConversionOptions classes let's explore using these classes to convert and format HTML to PDF. 

Creating the Project

Start by creating or downloading the project. You can choose to follow the instructions here or download the project from GitHub (link).

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ceTe.DynamicPDF.HtmlConverter;

namespace ConsoleAppHtmlToPdf
{
    class Program
    {
       static void Main(string[] args)
       {
        Console.WriteLine("Hello World!");
       }
    }
}

Convert Dynamically Generate PDF

After creating the project, in this first example, we dynamically generate a PDF using an HTML table generated by sample form data.

  • Create a new method named ConvertDynamicHtmlToPdf and add a multidimensional array as a string parameter.
  • Create sample data to load and display as an HTML table.
static string[,] companyData = {{ "Grey Fox Brewing", "Mark Smith", "Canada" },
{"Deutsche Ingolstadt","Elias Schneider","Germany"},
{"Centro comercial Moctezuma","Alejandra Silva","Columbia"},
{"West Indies Trading Company","Helen Moore","UK"},
{"Bharat of India","Aarnav Chanda","India"},
{"Magazzini Alimentari Riuniti","Giovanni Esposito","Italy"},
{"Joyas de Cristal","Helena Garcia","Spain"},
{"Telemar Brasil","Elias Martinez","Brazil"},
{"Joe's Pizzaria","Joe Bowman","United States"} };
The data consists of an array containing each entry which consists of the company name, contact name, and the country the company is located.
static void ConvertDynamicHtmlToPdf(string[,] data)
{
     string head = "<html><head><style>"
         + "table {font-family:arial, sans-serif;border-collapse:collapse;width:100%;}"
         + "td,th {border:1px solid #dddddd;text-align:left;padding:8px;}"
         + "tr:nth-child(even){background-color:#dddddd;}"
         + "</style></head><body>";
     string title = "<h2>Company Contacts Listing</h2>";
     string rowHeader = "<tr><th>Company</th><th>Contact</th>"
         + "<th>Country</th></tr>";
     string[] tableTag = { "<table>", "</table>" };
     string close = "</body></html>";

     StringBuilder output = new StringBuilder(head).Append(title);
     output.Append(tableTag[0]);
     output.Append(rowHeader);

     int i = 0;
     foreach (string rec in companyData)
     {
          if (i++ % 3 == 0) output.Append("<tr>");
          output.Append("<td>").Append(rec).Append("</td>");
           if (i % 3 == 0) output.Append("</tr>");
      }

      output.Append(tableTag[1]).Append(close);
      Converter.Convert(output.ToString(), "example5.pdf");
}

After creating our newly created method, add it to the project's Main method.

  • Modify Main to call the newly created method.
  • Run the program and review the created PDF document.
static void Main(string[] args)
{
     ConvertSaveToFileNoInputHtml();
}

Example illustrating HTML with base tag.

As this first example illustrated, creating a PDF document from dynamically generated HTML data is straightforward and only required a document path as a URL and the PDF output.

Convert HTML to PDF Using URI

As the previous example illustrated, generating PDF from HTML is easy when using DynamicPDF HTML Converter. Now let's convert a PDF using a URI to an HTML document, only this time we use a large HTML document combined with ConversionOptions to format the PDF and then save it to a local file.  

  • Create a new static method named ConvertSaveToFileNoInputHtml.
  • Add the URL to the Project Guttenberg's "A Tale of Two Cities."
  • Create a ConversionOptions instance and add the margins, author, creator, title, subject, and the header and footer to the ConversionOptions properties.
  • Call the Converter.Convert method, passing the URL document to convert, the output path for the PDF document, and the conversion options.
  • The code for the ConvertUriToPdf method should appear as follows. 
static void ConvertUriToPdf()
{
     // method signature: public static void Convert(Uri uri, string outputPath, 
    // ConversionOptions conversionOptions = null);

    string outputDocumentPath = "./tale-two-cities--document.pdf";
    string taleOfTwoCities = "https://www.gutenberg.org/files/98/98-h/98-h.htm";
    Uri resolvePath = new Uri(taleOfTwoCities);
    double leftRightMarginsPts = 36;
    double topBottomMarginsPts = 144;

    ConversionOptions conversionOptions = new ConversionOptions(PageSize.Letter, 
           PageOrientation.Portrait, leftRightMarginsPts, topBottomMarginsPts);
           conversionOptions.Author = "Charles Dickens";
           conversionOptions.Creator = "James B";
           conversionOptions.Title = "A Tale of Two Cities";
           conversionOptions.Subject = "Guttenberg press version of Charles Dickens\'s"
               + " A Tale of Two Cities.";
           conversionOptions.Header = "<div style = 'text-align:center;width:100%"
               +";font-size:15em;'>A Tale of Two Cities</div>";
           conversionOptions.Footer = "<div style='text-align:left;"
               +"text-indent:10px;display:inline-block;"
               + "font-size:6em;'><span class=url></span></div>"
               + "<div style = 'text-align:center; display:inline-block; width:60%'></div>"
               + "<div style = 'text-align:right; display:inline-block;"
               +"font-size:6em;'>Page <span class=\"pageNumber\">"
               + "</span> of <span class=\"totalPages\"></span></div>";

     Converter.Convert(resolvePath, outputDocumentPath, conversionOptions);
}

Example illustraing HTML to PDF conversion.

As this second example illustrated, the conversion from HTML to PDF in this example was also straightforward. The ConversionOptions class, although optional, provides powerful options to format a PDF.  For example, in the code above, we created a header from CSS that wrote the title at the top of each page. We also created a footer from CSS that wrote the path and page number at the bottom of each page.

Convert with Base Path and URI

The Convert method consists of an overloaded method that takes an input HTML string, a URL, and an optional ConversionOptions parameter. The following is the method's signature.

public static void Convert(string inputHtml, string outputPath, Uri basePath = null,
     ConversionOptions conversionOptions = null);

The inputHtml is the string content containing HTML, while the basePath is the URL to the document's root path. This overloaded Convert method is how we can process an HTML document using a baseTag. Refer to HTML baseTag on W3Schools for more details on HTML base paths.

  • Create a static method named ConvertSaveToFileInputHtml.
  • Add an array with two relative paths.
  • Create a string containing links to the relative paths in the array.
  • Create a new URI containing the path listed below to Wikimedia Commons.
  • Call the Convert method, passing the input HTML, the output path, and the URI.
  • Create a new ConversionOptions instance and set the page size, page orientation, and the left/right and bottom/top margins.
  • Reduce the zoom to 25% by setting the Zoom property in ConversionOptions.
  • Add the ConvertSaveToFileInputHtml method call to the Main method.
static void ConvertSaveToFileInputHtml()
{
        // method signature: public static void Convert(string inputHtml, string outputPath,
        // Uri basePath = null, ConversionOptions conversionOptions = null);

       string[] filePaths = new string[]{ "./e/ea/Doel_-_Water_pump_1.jpg", 
           "./3/3a/The_Soviet_Union_1939_CPA_690_stamp_%28Plane%29_cancelled.jpg" 
        };
        
        string tempHtml = "<html><body><img src=\"" + filePaths[0] + "\">" 
             + "<img src=\"" + filePaths[1] + "\">" + "</body></html>";

        Uri resolvePath = new Uri("https://upload.wikimedia.org/wikipedia/commons/");
        Converter.Convert(tempHtml, "example1.pdf", resolvePath);

        ConversionOptions conversionOptions = new ConversionOptions(PageSize.Tabloid, 
                PageOrientation.Landscape, 1, 1);
        conversionOptions.Zoom = 0.25F;

        Converter.Convert(tempHtml, "secondTry.pdf", resolvePath,conversionOptions);
      
}
 
  • Run the program and review the PDF output.

Example illustrating HTML with base tag.

In this example, we created a PDF document by combining an HTML string with its base path URI (https://upload.wikimedia.org/wikipedia/commons/). The complete HTML resolved as follows.

<html><body>
<img src="https://upload.wikimedia.org/wikipedia/commons/./e/ea/Doel_-
_Water_pump_1.jpg">
<img src="https://upload.wikimedia.org/wikipedia/commons/./3/3a/The_Soviet_Union_1939_
CPA_690_stamp_%28Plane%29_cancelled.jpg">
</body></html>

We then added document formatting, followed by calling the  Convert method to write the PDF document. Note that the conversion options included formatting the output page as tabloid, the orientation as landscape, and the margins as 1 for the left/right and top/bottom margins.

Convert with CSS and JavaScript

Now let's create the method ConvertHtmlWithCssJavaScriptToPdf to illustrate converting embedded CSS and embedded JavaScript.

  • Add the samplePageWithCss and samplePageWithJavaScript variables to the Program class.
static string samplePageWithCss = "<!DOCTYPE html><html><head ><style>" 
    + "body {background-color: lightblue;}"
    + "h1 {color: white; text-align: center;} p " 
    + "{font-family: verdana;font - size: 20px;}</style>" 
    + "</head><body><h1>My First CSS Example</h1>"
    + "<p>This is a paragraph.</p></body></html>";

static string samplePageWithJavaScript = "<!DOCTYPE html><html><body>"
     + "<h2>My First Web Page</h2>"
     + "<p>My First Paragraph.</p><p id=\"demo\"></p>"
     + "<script>document.getElementById(\"demo\")"
     + ".innerHTML = 5 + 6;</script></body></html>";
  • Create a new method named ConvertHtmlWithCssJavaScriptPdf.
  • Convert the embedded CSS and embedded JavaScript to PDFs.
static void ConvertHtmlWithCssJavaScriptPdf()
{
    // method signature: public static void Convert(string inputHtml, string outputPath, 
    // Uri basePath = null, ConversionOptions conversionOptions = null);

     Converter.Convert(samplePageWithCss, "cssExample.pdf");
     Converter.Convert(samplePageWithJavaScript, "jscriptExample.pdf");
 }

After running the method, both PDF documents output formatted CSS and JavaScript. As the two examples illustrate, DynamicPDF HTML Converter seamlessly supports CSS and JavaScript embedded in HTML documents.

Example illustrating converting embedded CSS and JavaScript into a PDF.

Convert to Byte Array

You can also convert a PDF to a byte array rather than saving it to a file. In this example, we convert an HTML document into a byte array. 

  • Create a new static method named ConvertWriteToByteArrayNoInputHtml.
  • Create a new ConversionOptions instance and pass the PageSize.Tabloid, PageOrientation.Landscape, and the left/right margin and top/bottom margins as 28 points.
  • Create a new URI object instance and pass the CNN URL to the constructor.
  • Call the Convert method, passing the URI and ConversionOptions as parameters and return a byte array as the result.
  • Save the byte array to a file. 
static void ConvertWriteToByteArrayNoInputHtml()
{
   // public static byte[] Convert(Uri uri, ConversionOptions conversionOptions = null);

   ConversionOptions conversionOptions = new ConversionOptions(PageSize.Tabloid, PageOrientation.Landscape, 28, 28);
   Uri document = new Uri("https://cnn.com");
   byte[] output = Converter.Convert(document,conversionOptions);
   File.WriteAllBytes("./cnn-printiout.pdf", output);

This simple example illustrated converting an HTML document to a PDF as a byte array. For simplicity, we saved the byte array to a file to demonstrate that the PDF was printed and formatted correctly.

Convert to Byte Array Asynchronous

For the final example, we convert an HTML document to a byte array asynchronously. If you ran the application in the first example, where it converted A Tale of Two Cities from HTML to PDF, then you probably noticed it took several seconds for the conversion to complete, as the document consists of 330 pages. When processing large documents, it makes sense to convert the document asynchronously so it processes without blocking the main thread. In this example, we convert HTML to PDF asynchronously.

  • Create a new static method named ConvertAsyncReturnByteArray that specifies async and returns a Task.
  • Create strings that specify the output document's path and the input document's path.
  • Create a URI from the input document's path.
  • Create a ConversionOptions instance, passing the page size as letter, the page orientation as portrait, and the margins as 28 points.
  • Add a title and footer to the ConversionOptions instance.
  • Call the ConvertAsync method and pass the input URI document and the ConversionOptions to the method as parameters. Return the output as a byte array.
static async Task ConvertAsyncReturnByteArray()
{
     // public static Task<byte[]> ConvertAsync(Uri uri, ConversionOptions
     // conversionOptions = null);

     string outputDocumentPath = "./gibbons-document.pdf";
     string gibbons = "https://www.gutenberg.org/files/731/731-h/731-h.htm";
     Uri resolvePath = new Uri(gibbons);
     double leftRightMarginsPts = 28;
     double topBottomMarginsPts = 28;

     ConversionOptions conversionOptions = new ConversionOptions(PageSize.Letter, 
     PageOrientation.Portrait, leftRightMarginsPts, topBottomMarginsPts);
     conversionOptions.Title = "HISTORY OF THE DECLINE AND FALL OF THE ROMAN" 
         + " EMPIRE";
      conversionOptions.Footer = "<div style='text-align:left;text-indent:10px;"
          +" display:inline-block; font-size:6em;'><span class=url></span></div>"
          + "<div style = 'text-align:center; display:inline-block; width:60%'></div>"
          + "<div style = 'text-align:right; display:inline-block;font-size:6em;'>"
          + "Page <span class=\"pageNumber\"></span> of <span"
          + "class=\"totalPages\"></span></div>";

      byte[] vals = await Converter.ConvertAsync(resolvePath, conversionOptions); 
      File.WriteAllBytes(outputDocumentPath, vals);
}
  • Modify the Main method to call the ConvertAsyncReturnByteArray method as an asynchronous task. 
static void Main(string[] args)
{
     var task = ConvertAsyncReturnByteArray();
      int i = 0;
      while(task.IsCompleted == false) { Console.Write("\r" + i++); }
      task.Wait();
}

When we execute the program, it takes several seconds to build the PDF document. However, as the simple code in Main illustrates, it does not block the thread while making the PDF.

GitHub Project 

The GitHub project is available here at DynamicPDF/blog-convert-html-to-pdf (project file).

Summary

In this tutorial, we explored using DynamicPDF HTML Converter. DynamicPDF HTML Converter supports synchronous and asynchronous document conversion. It also supports HTML strings with Base URLs, embedded CSS, and embedded JavaScript. And, as the examples illustrated, DynamicPDF HTML Converter supports formatting PDF with many formatting options. As the examples shown, creating a PDF from HTML is straightforward. Get started using Dynamic HTML Converter today by using the free version or by purchasing a subscription today.

Tags: , , , , ,

Version 10 Release of Dynamic PDF for .NET Core Suite

by Anil S

Fri, August 3 2018, 2:32 PM (US Eastern Time)

We are happy to announce the release of DynamicPDF for .NET Core Suite v10 (this includes our Merger, Generator and ReportWriter products).

v10 now supports .NET Standard 2.0 for seamless cross-platform deployment.  

Download it Today!

Version 10 adds several useful and exciting new features:

  • Deployment to any .NET implementation including .NET Core, .NET Framework, Mono, UWP, Xamarin.iOS and Xamarin.Android.
  • HTML layout engine for rapid PDF development using HTML and CSS
  • All new report layout engine (DLEX templates)
  • Take a look at a full list of All Version 10 Full Features

Free upgrades for Maintenance customers, discounted upgrades for eligible existing customers, eligible upgrades can be accessed via our Customer Area, www.dynamicpdf.com/customerarea or we can be contacted directly, sales@dynamicpdf.com or +1 410.772.8620.

Version 9 Release of Dynamic PDF for .NET Core Suite

Tue, December 12 2017, 2:43 PM (US Eastern Time)

DynamicPDF for .NET Core Suite v9 (this includes our Merger, Generator and ReportWriter products) has now been fully released.  Download it Today!

Version 9 adds tons of useful and exciting new features including:

  • HTML Rendering (convert HTML pages including CSS)
  • Disk Buffering (incremental PDF rendering decreases memory usage)
  • Character Shaping (for fonts of complex scripts)
  • Over 20 New Barcodes (Aztec, Code 11, Code 93, GS1 Databar, Australia Post, etc.)
  • Package PDFs
  • XFA Static Form Filling
  • Document, Page and Field Actions
  • Take a look at a full list of All Version 9 Full Features

What might be our best feature...Lower Prices across the board (all editions, all license models).

Free upgrades for Maintenance customers, discounted upgrades for eligible existing customers, eligible upgrades can be accessed via our Customer Area, www.dynamicpdf.com/customerarea or we can be contacted directly, sales@cete.com or +1 410.772.8620.

Tags: , , , , , , , , , , , , , ,

DynamicPDF for Java Version 8 BETA Release

by Anil S

Fri, July 31 2015, 1:05 PM (US Eastern Time)

The DynamicPDF for Java Version 8.0 BETA has just been posted to our site for download and testing, DynamicPDF for Java v8 Beta Download.

This version 8 BETA includes DynamicPDF Generator & Merger. The BETA download includes the updated documentation and examples (Java source code, Servlets, JSPs).

Version 8 will include a bunch of new features including two new barcodes (MSI and RM4SCC), PDF/A-2 and PDF/A-3 support, duplicate image removal, individual form field flattening and more. Take a look at the full list of new features.

Email support@cete.com with any questions or issues and thanks for taking part in our Version 8 BETA.

Tags: , , , , ,

Dynamic PDF for COM/ActiveX Core Suite Version 8 Release

Fri, February 20 2015, 4:25 PM (US Eastern Time)

Our DynamicPDF Core Suite (including Merger, Generator and ReportWriter) version 8 has now been fully released.  Download it Today!

Version 8 adds lots of new features including additional support for PDF/A-2, PDF/A-3, MSI (Modified Plessey) & RM4SCC (Royal Mail) barcodes, Strikethrough Text, Document Optimization (of Images), Enhanced Font Rendering, Field level Flattening as well as overall efficiency improvements and stabilizations. Take a look at our Version 8 Full Feature List.

For existing customers, eligible upgrades (free with Maintenance, discounted with previous versions) can be accessed via our Customer Area, www.dynamicpdf.com\customerarea or anyone can contact us directly, sales@cete.com or +1 410.772.8620.

Tags: , , , , , , ,

DynamicPDF for COM/ActiveX Core Suite Version 8 BETA Release

Fri, December 5 2014, 4:42 PM (US Eastern Time)

The DynamicPDF Core Suite for COM/ActiveX Version 8.0 BETA has just been posted to our site for download and testing, DynamicPDF Core Suite for COM/ActiveX v8 Beta Download.

This version 8 BETA includes DynamicPDF Generator, Merger and ReportWriter (all included in the Core Suite) and the installer includes the updated documentation and all our ASP, VB6 and VBScript examples.

Version 8 will include a bunch of new features including two new barcodes (MSI and RM4SCC), PDF/A-2 and PDF/A-3 support, duplicate image removal and more. Take a look at the full list of new features.

Email support@cete.com with any questions or issues and thanks for taking part in our Version 8 BETA.

Tags: , , , , , , , ,

Dynamic PDF for .NET Core Suite Version 8 Release

Mon, September 29 2014, 5:12 PM (US Eastern Time)

DynamicPDF Core Suite for .NET v8 has been released!  Thanks to everyone who participated in the beta testing and helped make this release a success.  Please review our full DynamicPDF Core Suite v8 Press Release.
 
Version 8 adds lots of new features including additional support for PDF/A-2, PDF/A-3, MSI (Modified Plessey) & RM4SCC (Royal Mail) barcodes, Strikethrough Text, Document Optimization (of Images), Enhanced Font Rendering, Field level Flattening as well as overall efficiency improvements and stabilizations. Take a look at our Version 8 Full Feature List.
 
For any upgrade options, existing customers can log into our Customer Area, or anyone can contact us directly, sales@cete.com or +1 410.772.8620.

Tags: , , , , , ,

DynamicPDF for .NET Core Suite Version 8 BETA Release

Wed, June 11 2014, 4:58 PM (US Eastern Time)

We have just posted the DynamicPDF Core Suite for .NET version 8 BETA on our site for download and testing, DynamicPDF Core Suite for .NET v8 Beta Download.

This version 8 BETA includes DynamicPDF Generator, Merger and ReportWriter (all included in the Core Suite) and the installer includes the updated documentation and all our C# and VB.NET Visual Studio examples.

Version 8 will include a bunch of new features including two new barcodes (MSI and RM4SCC), PDF/A-2 and PDF/A-3 support, duplicate image removal and more. Take a look at the full list of new features.

Email support@cete.com with any questions or issues and thanks for taking part in our version 8 BETA.

Tags: , , , , , , ,

.NET PDF Viewer - Beta Release

Thu, March 7 2013, 4:41 PM (US Eastern Time)

Now ready for Beta testing is our DynamicPDF Viewer 1.0 for .NET windows control.  This is an all new PDF Viewer designed specifically for .NET developers who want to add seamless, efficient, customizable PDF viewing to their .NET windows application.

No COM Interops, no problem!  DynamicPDF Viewer for .NET is an actual .NET Windows Form Control.  The API is easy to use and the PDF Viewing window is easy to customize.

Display a PDF from a file or byte array, open encrypted PDF files, turn toolbars on or off and so much more.

Read more and begin beta testing today, http://www.dynamicpdf.com/Products/DynamicPDFViewerForNET/Beta.aspx.

Any questions, comments or issues should be sent to support@cete.com.

Thanks for everyone's participation.

Tags: , , , ,

Java Release of DynamicPDF Merger and Generator Version 7

Tue, December 4 2012, 4:45 PM (US Eastern Time)

We are proud to announce the version 7 release of our DynamicPDF Merger and Generator for Java, our easy to use components for creating and interacting with any PDF documents from within any Java application.  Thanks to everyone who participated in the Beta testing process and contributed to a successful release.

Version 7 for Java includes new PDF encryption algorithm support (AES 128 and 256, RC4 128 and crypt filter support), adding QR codes to PDF, true PDF form flattening and lots more.  Check out our full list of new PDF features here.

We always love your feedback; let us know what features or functionality you want to see in the future.

Tags: , , , , , ,

Month List

RecentPosts