This site is now AnswerTips enabled. Double-click any word for its definition.

Produce PDFs from your web pages on-the-fly

by Bill Hely on December 3, 2009 · 2 comments

PDF_createThis post is a bit different to my norm and is aimed primarily at those readers with some web building experience.

It’s got nothing to do with security but it is productivity-related if you’ve ever needed something like this on your website. Regardless, it’s such a cool facility I just had to pass it on.

More likely you haven’t needed it because you never thought of it, so maybe I can sow a seed…

Immersed neck deep as I am in constructing the new membership website, I keep coming up with new features I’d like to make available to members.

One idea I had a couple of weeks ago was to provide members with a one-click way to send one of the many articles and tutorials to a friend who may be in desperate need of some assistance.

No, I don’t mean the traditional tell-a-friend feature — there’s one of those as well.

This is different. It goes like this…

As you would expect, all membership site content is presented in your browser as a formatted HTML page, and most pages have some server-side programming code contributing to their functionality and appearance.

The usual way to refer someone to a webpage is to simply send them a link to that page, but there are a couple of negatives to that approach. For one thing they can’t view the page if they’re off-line, like say sitting on an aeroplane (a popular place to catch up on reading). Also, in the case of a member sending an article link to a nonmember, the latter won’t get anything at all unless the member sends his login details as well (thus risking excommunication!).

So I thought that the obvious solution was to find a way to create a PDF of any webpage on-the-fly, and automatically attach the PDF to an e-mail addressed to the friend.

Now, there are currently scores of articles in the membership site, a number that will increase to hundreds over time, so retrospectively and manually creating PDFs of every page and storing them for download was out of the question. Even when that massive task was completed it would be an ongoing headache because PDFs would have to be created for all new articles and recreated when pages were modified/updated.

That’s certainly not an extra responsibility I want to take on, so emphasis had to be on automatic, on-the-fly creation of PDFs for individual pages as and when required.

Now coming up with an idea is one thing, but finding the necessary tools to implement it can be frustrating.

The new membership site is hosted on a Windows 2008 Server and for various reasons I’m writing all server-side code in classic ASP, so I needed something compatible with that environment.

I soon discovered that there are very few programs available that could do exactly what I wanted, and of those I could find the majority were very expensive (many hundreds of dollars up into the thousands). But most discouraging of all, flexibility and ease of implementation were often less than one would hope for.

Then I found a website with the strange name of WebSupergoo — and their product called ABCpdf.

Now I’m not going to get into a lot of technical jiggery-pokery. Even on a subject such as this I want to keep it as plain language as possible. Still, some mild tech-speak can’t be avoided, but if you’re in a position to use this solution you’ll be able to follow the language.

ABCpdf is a .NET product that includes a COM interface for compatibility with classic ASP. It is available for C#, VB.NET, ASPX and other .NET environments. In fact you can use it from any scripting language that supports Microsoft ActiveX Scripting interfaces. It is fully multi-threaded and ideal for use under IIS.

In other words it is designed to be installed on the web server and called from server-side code such as, in my case, ASP written on the individual webpages.

So the first step after downloading was installation, which wasn’t a problem at all. After taking a quick look at it my host technicians had no objection to installing ABCpdf on the web server that hosts my site — a 64-bit machine running Windows Server 2008 and IIS7. If your host objects, maybe you should be giving your business to someone who allows more flexibility.

Before giving an example of ABCpdf in use, there is one other slightly techo thing that it is appropriate to mention at this point.

In a previous blog post I explained why it’s important to keep Internet Explorer patched and up-to-date even if you use some other browser on a day-to-day basis. Also the article “Which Browser?” in the Special Topics Module of the membership site discusses this topic in more detail.

I’m taking this opportunity to reinforce that recommendation because ABCpdf is one of those many applications that make use of an existing Internet Explorer component — specifically the “mshtml library”. So please do make a point of ALWAYS keeping your IE up-to-date even if you don’t use it as a browser. OK, back to…

Creating PDF files on-the-fly…

Documentation is extensive and there are many examples in various programming languages. Here is the (almost) exact classic ASP code I use to create PDFs from the click of a link on any membership page.

Sub Create_PDF()

Dim theDoc, theURL, theID, count

Set theDoc = CreateObject("ABCpdf7.XSettings")

' The next line is the new code they introduced for
' on-the-fly provision of your own personal license key.
theDoc.InstallRedistributionLicense "your-license-key-goes-here"

Set theDoc = Server.CreateObject("ABCpdf7.Doc")

theDoc.Page = theDoc.AddPage()
theURL = "http://The-URL-of-the-web-page-to-be-rendered.asp"
theID = theDoc.AddImageUrl(theURL)

Do
 theDoc.FrameRect
 If Not theDoc.Chainable(theID) Then Exit Do
 theDoc.Page = theDoc.AddPage()
 theID = theDoc.AddImageToChain(theID)
Loop

For count = 1 To theDoc.PageCount
 theDoc.PageNumber = count
 theDoc.Flatten
Next

theDoc.Save "C:\Path-to-a-folder-on-the-server\filename.pdf"
theDoc.Clear

End Sub

It really is as simple as that, but ABCpdf is capable of much more than is illustrated by that simple example. Apart from being able to create PDFs from existing web documents, which was my prime requirement, you can actually create PDFs programmatically — word-by-word, line-by-line or object-by-object.

For example, I could easily prepend or append a customized block of text that read something like:

This document courtesy of John Smith for the exclusive use of Marjorie Brown. Join me as a member of  CAOS and get access to a wide range of very useful articles.

Watermarks and extra images are also easily included. The possibilities are limited only by your imagination.

Licensing

There is a trial license built-in to the trial download that will let you use all features of the product for 30 days. If you find it even half as useful as I do you can then purchase a full license for your own continued use.

And that’s where I discovered a shortcoming.

A license key for ABCpdf is a long string of alphanumeric characters, and this key must be plugged into the server installation of the product. If your website runs on a dedicated server, no problem. But if you are one of the many millions of Webmasters whose website is hosted on a shared server, there is a problem.

You see, once the license key is installed it becomes available to all other websites hosted on that same server, thus violating the terms of use. Hence it was always WebSupergoo’s recommendation (insistence?) that ABCpdf only be installed on dedicated servers where the license key would be kept private to the legal licensee.

Now I can’t be sure of the reasoning, but it seems to me that the developers must have originally seen the market for ABCpdf as big-time websites which are invariably hosted on private or dedicated servers. But as we all know there are now millions upon millions of websites whose owners cannot justify the extra cost of a dedicated server.

I mentioned to my contact at ABCpdf that I thought they were depriving a lot of people of a very useful product and unnecessarily limiting their potential market. Well, talk about…

Going the extra yard!

I think it was something like within 48 hours I received a reply saying they had altered the code to allow the private license key to be sent from within the webpage code. I have emphasised that line of code in green in the sample above.

In summary…

ABCpdf is a first class product that works well pretty much straight out of the box. Anyone who is reasonably competent in any of the supported languages should have no problem getting it up and running and pushing out customized PDFs in no time.

And then you might actually start reading the user guide and begin to realise just how powerful this application is.

Highly recommended. Click the link for more information…

ABCpdf from WebSupergoo

If you found this article of use please share it
on your favorite social media network
(see buttons below)

Post to Twitter Post to Digg Post to Facebook Post to StumbleUpon

{ 2 comments… read them below or add one }

1 Andreas Krueger February 1, 2010 at 4:10 pm

Dear Bill,

since two weeks i try to generate and stream an pdf on the fly.
i will implement an button to generate from a shown site, an pdf.
but each time it’s empty or i got an error.
i can’t save in local filesystem !

do you have an sample source for me ?

Reply

2 Bill Hely February 4, 2010 at 12:47 pm

Andreas, I gave a code example in the article above. Also you should check out this webpage:
http://www.websupergoo.com/helppdf7/default.html

See the links on the left hand side of that page. Start with the “Simple Example” under the “Getting Started” item, get it working, then expand it with extra functions bit by bit. That’s basically where I started.

If you encounter problems you really need to consult WebSupergoo support (after checking out the FAQs, of course ). There may be issues such as DLL registration that I couldn’t possibly help you with.

Reply

Leave a Comment

You can add images to your comment by clicking here.