ExpertPDF HTML to PDF Version 2.2
Create PDF Files Programmatically
October 30, 2009
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 theInternet. Its ubiquitous PDF file format enables a variety of print formattingoptions that otherwise wouldn t be feasible. Also beloved is its typicallyread-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 responseisn t nearly so universal. There are an overwhelming number of softwareapplications available that enable end users to create PDF documents, sodetermining which one is best for a given scenario can be a daunting task. Butwhat if you need to create a PDF file programmatically? Thankfully, the answerto that question is much simpler.
Create PDF Files Programmatically
HTML to PDF is the primary component of Outside Software sExpertPDF library. The ExpertPDF library provides programmatic PDF creation andconversion functionality. This review focuses on ExpertPDF s main HTML to PDFcomponent, although you should be aware that there are several optional add-oncomponents 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 toit.
From a programming standpoint, HTML to PDF could hardly beany easier to use. If you know how to make Web pages, you essentially know howto create PDF documents, as well as long as you have a copy of HTML to PDF. There sno 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 andconvert it into PDF format. All page formatting is fully preserved (includingCSS), 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 homepage 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 toPDF s consistent object model makes it easy to convert Web pages to PDF files.
You might notice that Figure 1 consists primarily ofboilerplate ASP.NET code. There are really only two lines that deal directlywith HTML to PDF. Other than instantiating the PdfConverter component andcalling its GetPdfFromUrlBytes method, the rest of the code does ordinarythings like setting page headers (so Web browsers can recognize easily thecontent as a PDF file instead of the usual HTML output). Most of that codebecomes unnecessary in cases where you don t need to display the PDF (perhapssaving 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 cautionyou to avoid importing Microsoft s System.Drawing namespace in your HTML to PDFpages. I did, and it resulted in a Visual Studio namespace conflict thatperplexed me for a while.
Take a Picture
As if the flawless PDF generation functionality weren tenough, HTML to PDF can also take complete screen shots of any Web page andoutput 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 almostidentical to the code for rendering a PDF, so you don t have to memorize differentsyntaxes. The sample code shown in Figure 2 again takes Microsoft s MSN homepage 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 toPDF can take any Web page as input and generate a PDF or image file as theoutput.
The image file displayed in Figure 3 resulted from thecode in Figure 2. It is a perfect representation of the MSN home page.
Figure 3: HTML to PDF can take apicture-perfect screen shot of virtually any Web page even Microsoft sintricately formatted MSN home page.
Don t let HTML to PDF s simplicity fool you into thinkingthat it has a limited feature set. There s a thorough set of classes andmethods included that allow you to handle more complex tasks when necessary. Forexample, you get full control over margins, colors, fonts, headers and footers,orientation, and most other typical document options. Additionally, there aremethods for controlling compression, authentication, encryption, and security. Youalso can lock down your resulting PDF files pretty tight if needed, so otherswon t be tempted to alter your work.
Support System
As for server-side support, HTML to PDF works on Windows2000 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 ofthe framework for your own development.
The free evaluation version of HTML to PDF is less than 2MB, making it a speedy download. There is no automated setup included, but itdoesn t feel very necessary either. The zip file contains the control itself(ephtmltopdf.dll), along with some decent sample code and a smidgen ofdocumentation. 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 totinker with is the online demo of HTML to PDF. With it, you can generate awatermarked PDF file from any public Web page on the entire Internet. This letsyou discover right now how well HTML to PDF will work with your Web site, with nodownload necessary.
Conclusion
HTML to PDF is a quality component that is clearlyvaluable to any Web developer in need of an easy and flexible way to generatePDF files. An added bonus is the ability to grab complete screen shots of Webpages even long ones that scroll off screen. I ve heard many developers fromaround the world beg and plead for such abilities, and now a good and simplesolution is finally at our disposal. Let us rejoice!
Steve C. Orr is anASP Insider, MCSD, Certified ScrumMaster, Microsoft MVP in ASP.NET, and authorof the book Beginning ASP.NET 2.0 AJAXby Wrox. He s been developing software solutions for leading companies in the Seattlearea for more than a decade. When he s not busy designing software systems orwriting about them, he can often be found loitering at local user groups andhabitually 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: Startsaround US$250
About the Author
You May Also Like