Login

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):

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:

<?php

if (is_front_page()) {

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

} elseif (is_single()) {

echo "<title>";

wp_title(”,true);

echo "</title>";

} elseif (is_page()) {

echo "<title>";

wp_title(”,true);

echo "</title>";

} elseif (is_archive()) {

echo "<title>";

wp_title(”,true);

echo "</title>";

} elseif (is_404()) {

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

} else {

echo "<title>";

bloginfo(‘name’);

echo "</title>";

}

?>

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

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: Yourdomain.com,” replace it with your correct domain. So if your domain is “thisismydomain.com”, it will be:

Page not found: Thisismydomain.com

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)

$titletag=trim(wp_title(”,false));

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 POSTlink.

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:

<?php

$titletag=trim(wp_title(”,false));

echo ‘<a rel="nofollow" href="http://www.php-developer.org/postpdfcreator.php?title=’.$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.

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:

<?php

$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”:

$posttitle=trim($_GET['title']);

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.

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: http://www.php-developer.org. 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.