How to use ABCPDF to convert HTML page to PDF
Posted on Mar 7, 2013 by
Paul WhiteEventually developers have the need to generate a PDF based on an webpage. In my case it was for generating invoices for my client. However going from
HTML webpage to PDF is no simple task. But thanks to the
ABCPDF.net component from websupergoo anyone with some basic coding skills can do this.
For my example I am using version 8 of the ABCPDF.net component.
You will need to put the following files in your bin directory
ABCpdf.dll
ABCpdf8-32.dll
ABCpdf8-64.dll
Note that we are using the professional version of the component, hence the reason for both the 32bit and 64bit dlls
Next at the top of your webpage you will need the following IMPORTS
<%@ Assembly Name="ABCpdf" %>
<%@ import Namespace="WebSupergoo.ABCpdf8" %>
<%@ import Namespace="WebSupergoo.ABCpdf8.Objects" %>
<%@ import Namespace="WebSupergoo.ABCpdf8.Atoms" %>
Then the following code will generate a PDF from a webpage
string dpi="150";
int compression = 90;
// make PDF and images
Doc theDoc = new Doc();
theDoc.Rendering.DotsPerInch = 150;
// apply a rotation transform
double w = theDoc.MediaBox.Width;
double h = theDoc.MediaBox.Height;
double l = theDoc.MediaBox.Left;
double b = theDoc.MediaBox.Bottom;
theDoc.Rect.Inset(0, 0);
theDoc.HtmlOptions.ImageQuality = compression;
theDoc.HtmlOptions.FontEmbed = true;
theDoc.HtmlOptions.FontSubstitute = false;
theDoc.HtmlOptions.FontProtection = false;
int theID = theDoc.AddImageUrl("http://mywebsite.com/mypage.htm");
savetheDoc.Save(Server.MapPath("/files/myfile.pdf"));
The previous code takes the webpage located at http://mywebsite.com/mypage.htm, and converts it into a PDF document and saves it in your website file directory with the name myfile.pdf.
Summary
ABCPDF gets the job done with very few lines of code. I highly recommend it!
Discussion
No Comments have been submitted