Adding PDF Conversion to Your WordPress Website

This is the second part of a two-part series that shows you how to convert WordPress posts into PDFs without a plug-in or module. In the first part, you learned the important concepts for implementation, the required PHP class and a little about formulating the GET request which serves as an input to postcreator.php to grab the correct content from the MySQL database. In this final part, we will discuss how to completely integrate this application into your WordPress blogging/CMS platform. It is highly recommended that you read the first part to avoid confusion.

Importance of using proper title tag

In the first part, you learned how to formulate the GET request by extracting ID parameters in the URL that consist of the post ID, which will then be used by the PDF creator script to grab the necessary content from the MySQL database.

However, since most blogs use different permalink structures, the above method cannot be used. This leads us to the second and most important method of distinguishing post content; namely, the Title Tag.

In WordPress, you should have unique title tags. This helps not only your SEO (Google, for example recommends the use of unique title tags), but your website readers/users as well.

When every post title is unique, you can use this input to your PDF creator script to properly grab the correct post content from the MySQL database. But first, you need to have a correct title tag in your WordPress blog. Some plug-ins can do this job, but the recommended way is by using a PHP script (no plug-ins for simplicity).

The default title tag in WordPress (no plug-in enabled) is this (can be found in header.php):

<title><?php wp_title(‘&laquo;’, true, ‘right’); ?> <?php bloginfo(‘name’); ?></title>

Other related templates may use a slight variation of this. But the main problem is that the above title is not optimized for use. For example, if the actual title of the post is “Hello World!” using the above script, the title that can be viewed in the browser is this:

Test  >> Blog Archive >> Hello World!

For this PDF creator project, you need a post title that exactly corresponds to the post title stored in the WordPress MySQL database. The strategy is to use the SELECT MySQL command in the PDF creator PHP script to get the post content for the corresponding unique post title. Below is the screen shot of the WordPress MySQL database (wp_post table) for the most basic/default installation:

It shows that the post title is only “Hello World!” and in the same row, you can see the post content. So how can we formulate a correct title tag in WordPress to exactly respond to the same post title stored in the MySQL database?

The following is the recommended PHP script that will provide the desired title tag output:


if (is_front_page()) {

        echo "<title>This is your homepage title tag</title>";

} elseif (is_single()) {

        echo "<title>";


        echo "</title>";


} elseif (is_page()) {

        echo "<title>";


        echo "</title>";

} elseif (is_archive()) {

        echo "<title>";


        echo "</title>";

} elseif (is_404()) {

        echo ‘<title>Page not found:</title>’;

} else {

        echo "<title>";


        echo "</title>";



Copy the above code and replace the default PHP code for the title tag, which is:

<title><?php wp_title(‘&laquo;’, true, ‘right’); ?> <?php bloginfo(‘name’); ?></title>

After that, set the correct title tag for your home page. You should edit “This is your homepage title” and replace it with your home page title tag. Also, in the 404 page “Page not found:,” replace it with your correct domain. So if your domain is “”, it will be:

Page not found:

In WordPress documentation, you can use wp_title () as input to PDF creator script in order to grab the post title. To make this happen, you will not display it to the browser, but just use it as a variable in the single.php (post template, discussed later)



We can use this as input to the PDF creator script, but first you need to create your CLICK TO SAVE THE PDF VERSION OF THIS POST link.

{mospagebreak title=Inserting the link in your WordPress post template}

Ideally, you can insert your "create a PDF" link anywhere in the post. It depends on your own template and preferences. However, for the purpose of this article and providing a standard method, we may wish to insert it at the bottom of the post.

Open single.php, and then find the code that looks similar to this:

<?php the_content( __(‘<p>Read the rest of this entry &raquo;</p>


Below the code above, insert this code:



echo ‘<a rel="nofollow" href="’.$titletag.’"><b>CLICK TO SAVE THE PDF VERSION OF THIS POST</b></a>’;


Replace the red font with your own domain name.

IMPORTANT: Do not forget to place a rel=nofollow to prevent search engines from crawling the content; otherwise, you could end up with duplicate content issues.

{mospagebreak title=PostPDFCreator.php Script Source Code}

Now that you have created the PDF link, it is time to create your PHP PDF Creator script. The governing flowchart for the programming is this:

First, you need to connect to your WordPress MySQL database:


$username = "xxx";

$password = "xxx";

$hostname = "xxx";

$database = "xxx";

$dbhandle = mysql_connect($hostname, $username, $password)

 or die("Unable to connect to MySQL");

//select a database to work with

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");


For the second step, you need to receive the GET request when users click on the “Click to save the PDF version of this post”:



It is important to sanitize user inputs to avoid any possibility of MySQL injection:

$posttitle = mysql_real_escape_string(stripslashes($posttitle));


The third step is to query MySQL to extract the “published” post corresponding to the requested title.

$result = mysql_query("SELECT `post_content` FROM `wp_posts` WHERE `post_title`=’$posttitle’ AND `post_status`=’publish’")

or die(mysql_error());

$row = mysql_fetch_array($result)

or die("Invalid query: " . mysql_error());

$content = $row[‘post_content’];

The fourth step is to assign the post content to a PHP variable as well as to strip HTML tags.

$content= strip_tags($content);


$message = "Post title: <b>$posttitle</b>





Copyright © 2009";


Edit items in a red font and replace with your own domain.

The fifth step is to integrate the R & OS PDF Creator class


include (‘class.ezpdf.php’);


If you upload this to the root directory of a hosting server, the above path will change. It depends on your hosting configuration; some may use a path like this:

include (‘/home/content/phpdeveloper/html/class.ezpdf.php’);  

If you are confused in determining the exact path of your root directory, upload this test script (save as rootpath.php) to your root directory and execute it in the browser to see the results:





For example, when uploaded this script to the root directory, of the website , you get the following result:


So when the class.ezpdf.php is uploaded to the root directory, the path will be:


So the correct PHP include syntax to include this PDF creator class will be:

include (‘/home/www/’);

The sixth step is to customize fonts:





Using an image is optional.

Finally, to stream the PDF to the browser, execute this function and close the MySQL:





{mospagebreak title=Implementation steps}

Now that you understand the concept and scripts behind the creation of PDF for your WordPress post, it is time to implement it in a real website:

Step 1: Upload the following file to your website/WordPress root directory:

a  Font (folder)

b. class.ezpdf.php

c. class.pdf

You can download the raw source files here:, or at the author’s website:

Step 2. Customize your postpdfcreator.php (you can download the complete script here:

Put in the correct database information, path of the files, customize the PDF output, etc. And then upload postpdfcreator.php to the root directory of your WordPress website.

Step 3. Put a hyperlink in your WordPress template:

Insert this code below <?php the_content( __(‘<p>Read the rest of this entry &raquo;</p>



echo ‘<a rel="nofollow" href="’.$titletag.’"><b>CLICK TO SAVE THE PDF VERSION OF THIS POST</b></a>’;


Step 4. Block postpdfcreator.php in the robots.txt to block duplicate content. Add this in your robots.txt syntax:

Disallow: /postpdfcreator.php

Step 5. Test your WordPress and see if the output corresponds to your expectations; you can tweak it further.

You can see a sample implementation in this website: Look at any post and you will see the link on the bottom. This PDF integration is very basic and only prints the text content of the post; it does not include images. 

[gp-comments width="770" linklove="off" ]