ExpertPDF HTML to PDF Version 2.2

Create PDF Files Programmatically

asp:review

 

ExpertPDF HTML to PDF Version 2.2

Create PDF Files Programmatically

 

By Steve C. Orr

 

Adobe Acrobat is widely considered to be a staple of the Internet. Its ubiquitous PDF file format enables a variety of print formatting options that otherwise wouldn t be feasible. Also beloved is its typically read-only, tamper-resistant data format.

 

The accolades of PDFs are espoused by virtually everyone, but if you ask the best way to create a PDF document you ll find the response isn t nearly so universal. There are an overwhelming number of software applications available that enable end users to create PDF documents, so determining which one is best for a given scenario can be a daunting task. But what if you need to create a PDF file programmatically? Thankfully, the answer to that question is much simpler.

 

Create PDF Files Programmatically

HTML to PDF is the primary component of Outside Software s ExpertPDF library. The ExpertPDF library provides programmatic PDF creation and conversion functionality. This review focuses on ExpertPDF s main HTML to PDF component, although you should be aware that there are several optional add-on components that can be purchased, depending on your needs.

 

To begin using HTML to PDF, simply copy the control (ephtmltopdf.dll) into your Web application s bin folder and add a reference to it.

 

From a programming standpoint, HTML to PDF could hardly be any easier to use. If you know how to make Web pages, you essentially know how to create PDF documents, as well as long as you have a copy of HTML to PDF. There s no need to memorize complex object models or wallow in data binding muck. Instead, all it takes is a couple lines of code to take any existing HTML page and convert it into PDF format. All page formatting is fully preserved (including CSS), so you don t have to redesign the output at all. To illustrate this point, the sample code shown in Figure 1 takes Microsoft s richly formatted MSN home page and generates an identical PDF file from it.

 

'Imports System.Drawing.Imaging.ImageFormat

'Imports ExpertPdf.HtmlToPdf

'Imports ExpertPdf.HtmlToPdf.ImgConverter

Dim url As String = "http://www.msn.com/"

Dim cvtr As PdfConverter = New PdfConverter

Dim b() As Byte = cvtr.GetPdfFromUrlBytes(url)

Response.Clear()

Response.AddHeader("Content-Type", "binary/octet-stream")

Response.AddHeader("Content-Disposition", ("inline; " _

        + " filename=my.pdf; size=" + b.Length.ToString))

Response.BinaryWrite(b)

Response.End()

Figure 1: HTML to PDF s consistent object model makes it easy to convert Web pages to PDF files.

 

You might notice that Figure 1 consists primarily of boilerplate ASP.NET code. There are really only two lines that deal directly with HTML to PDF. Other than instantiating the PdfConverter component and calling its GetPdfFromUrlBytes method, the rest of the code does ordinary things like setting page headers (so Web browsers can recognize easily the content as a PDF file instead of the usual HTML output). Most of that code becomes unnecessary in cases where you don t need to display the PDF (perhaps saving the file to a network share instead).

 

I ve included the necessary Imports statements (aka Using statements) as comments at the top of Figure 1 for your reference. I caution you to avoid importing Microsoft s System.Drawing namespace in your HTML to PDF pages. I did, and it resulted in a Visual Studio namespace conflict that perplexed me for a while.

 

Take a Picture

As if the flawless PDF generation functionality weren t enough, HTML to PDF can also take complete screen shots of any Web page and output the results into any standard image file format (such as JPG, BMP, PNG, GIF, etc.). Even better, the code for rendering an image file is almost identical to the code for rendering a PDF, so you don t have to memorize different syntaxes. The sample code shown in Figure 2 again takes Microsoft s MSN home page as input, but this time outputs a JPG image instead of a PDF.

 

Dim url As String = "http://www.msn.com/"

Dim cvtr As ImgConverter = New ImgConverter()

Dim b() As Byte = cvtr.GetImageFromUrlBytes(url, Jpeg)

Response.Clear()

Response.AddHeader("Content-Type", "image/jpeg")

Response.BinaryWrite(b)

Response.End()

Figure 2: HTML to PDF can take any Web page as input and generate a PDF or image file as the output.

 

The image file displayed in Figure 3 resulted from the code in Figure 2. It is a perfect representation of the MSN home page.

 


Figure 3: HTML to PDF can take a picture-perfect screen shot of virtually any Web page even Microsoft s intricately formatted MSN home page.

 

Don t let HTML to PDF s simplicity fool you into thinking that it has a limited feature set. There s a thorough set of classes and methods included that allow you to handle more complex tasks when necessary. For example, you get full control over margins, colors, fonts, headers and footers, orientation, and most other typical document options. Additionally, there are methods for controlling compression, authentication, encryption, and security. You also can lock down your resulting PDF files pretty tight if needed, so others won t be tempted to alter your work.

 

Support System

As for server-side support, HTML to PDF works on Windows 2000 machines and above. It s currently compiled with version 2.0 of Microsoft s .NET Framework, although you can certainly feel free to use a newer version of the framework for your own development.

 

The free evaluation version of HTML to PDF is less than 2 MB, making it a speedy download. There is no automated setup included, but it doesn t feel very necessary either. The zip file contains the control itself (ephtmltopdf.dll), along with some decent sample code and a smidgen of documentation. More thorough documentation is available online at http://www.html-to-pdf.net/, where you ll also find a full feature list, FAQ, and sample code in both VB.NET and C#.

 

The first thing on their Web site you ll probably want to tinker with is the online demo of HTML to PDF. With it, you can generate a watermarked PDF file from any public Web page on the entire Internet. This lets you discover right now how well HTML to PDF will work with your Web site, with no download necessary.

 

Conclusion

HTML to PDF is a quality component that is clearly valuable to any Web developer in need of an easy and flexible way to generate PDF files. An added bonus is the ability to grab complete screen shots of Web pages even long ones that scroll off screen. I ve heard many developers from around the world beg and plead for such abilities, and now a good and simple solution is finally at our disposal. Let us rejoice!

 

Steve C. Orr is an ASP Insider, MCSD, Certified ScrumMaster, Microsoft MVP in ASP.NET, and author of the book Beginning ASP.NET 2.0 AJAX by Wrox. He s been developing software solutions for leading companies in the Seattle area for more than a decade. When he s not busy designing software systems or writing about them, he can often be found loitering at local user groups and habitually lurking in the ASP.NET newsgroup. Find out more about him at http://SteveOrr.net or e-mail him at mailto:[email protected].

 

Rating:

Web Site: http://www.html-to-pdf.net/

Price: Starts around US$250

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish