Taming PDF support

Introduction:

It happens about twice each month. Out of the blue, a random stranger will get in touch with me, curious about how I seem to have PDF support for my journal working “so seamlessly.” A few of these people have also offered me money to help them out, which made me realise this information is of use to somebody; and so exists this tutorial. Digg it if you find it useful!

At the outset, I’d like to let you know that this feature is still rather buggy, but, for the most part it does what is supposed to.

I presume you’re getting in touch with me because a popular plugin that converts posts into PDF, WP2PDF, often misbehaves, and is non-trivial to install and configure in the first place. This page details some of the things I have done to make it work. I have made some changes to the core WP2PDF code, which I detail below. Such is the joy of Free Software!

This guide is not detailed at all, and generally assumes you know what you are doing. If there are many repeated questions about something, I will augment this page to make that bit more clear. The instructions work the same for both WordPress 1.5x, and 2.x—one of which you obviously must have installed; otherwise you wouldn’t be here.

The Rough Steps:

These steps assume your blog’s files are installed at /blog, and it can be accessed via http://your-blog.org/.

  1. Fetch an archive of WP2PDF 0.4.2 from SourceForge.
  2. Copy it over to your server, and unzip it; creating the folder /blog/wp2pdf. “Install” it by opening http://your-blog.org/wp2pdf/install.php in your web browser.
  3. Fetch my modified wp2pdf.php (you need to rename the file from .php.txt to .php) and overwrite the original file in the WP2PDF archive.
  4. Configure it to your heart’s content, (including things like fonts and so on) by visiting http://your-blog.org/wp2pdf/admin/setup.php. This mostly works as advertised. Make sure you set the “saving behaviour” in the general preferences pane to “Save files on the webserver, then relocate.”
    WP2PDF Configuration
  5. Open the file add-to-template-functions-links.php and add its contents to the end of your existing /blog/wp-includes/template-functions-links.php (or, on newer WordPress versions, to /blog/wp-includes/link-template.php). Really, just copy and paste.
  6. Now you’re technically done. To check it out working, try loading http://your-blog.org/wp2pdf/wp2pdf.php. You should get a PDF with recent posts.

More info:

There are quite a few interesting and powerful things you can do from this point on.

  1. Since we’re greedy, we’re still not completely satisfied. We also want cool “permalinks” to work well. If you note, on my journal, the PDF for a post is linked to as post-permalink/pdf/. You definitely want that. For this, open your .htaccess and add the following block (anywhere for WordPress 1.5x, and RIGHT AT THE TOP for WordPress 2.x):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^earlier/([^/]+)/?([0-9]+)?/pdf/?$ /wp2pdf/wp2pdf.php?name=$1&page=$2 [QSA,L]
</IfModule>
  1. You will need to modify this slightly, because my permalinks are of the form /earlier/post-name/ and I want the PDF to show up at /earlier/post-name/pdf/. You will need to tweak the word “earlier” with whatever you use. And finally, you can just use <?php get_wp2pdf_permalink();?> where you need a link to PDFs on your template!
  2. You can have more than one post in the PDF. You can have a certain number of posts, posts by category, … and so much more. Again, you will have to work out your .htacess similar to the above to make your URIs look good. Enjoy!

Known Issues:

  1. Some “special symbols,” such as π, don’t render properly on the PDF document.
  2. Images don’t render on the PDF document.
  3. Some pages will randomly break and fail to render completely as a PDF document.

You need to sacrifice your first born to the gods and hope for the best.