If you have an educational or information-based website, you can expect that a lot of readers will spend a lot of time reading your content. In addition, these readers may intend to use your content for personal and educational purposes. A typical example involves sharing your content with fellow classmates or readers.
When content or information is shared, one of the most effective and most popular file types for sharing is the PDF format. PDF is also known as an Acrobat Reader document. Documents shared using PDF are efficient; the format reduces the file size as compared to common document-related file types such as MS Word, which can be heavy to share online. One of the great benefits of using PDF is to preserve the formatting of the document, and making it a read-only file that makes the document very readable.
On the other hand, the Internet presents the modern medium of exchanging information. One of the most popular blog publishing platforms is WordPress. This open source blogging and CMS solution is very popular because it is user friendly, easy to install and very convenient to readers, because it is very content-oriented.
Thus, when you have a content-based website or blog, it is very convenient for your readers to download your posts as a PDF document. In this way readers can easily share your content online or offline, which will help in increasing awareness of the website and its overall popularity.
Now that I’ve covered why you might want to do this, let’s begin.
WordPress is powered by the PHP server side scripting language. Therefore it makes sense to use PHP in generating PDF documents. Using PHP, it is possible to customize your PDF applications to have a slightly or even completely different look from your website image. This means you can design your PDF template by incorporating your customized logo, website name, contact address, etc. To have a better glimpse of this implementation, see the flowchart:
We can present a link at the bottom of your WordPress post which users can click to read the PDF version of the post. Then this link will use a query which the PHP PDF generator script can process as a GET request. The PHP script will then communicate with the WordPress MySQL database. The objective of this communication is to grab associated post content stored in MySQL and then send it back to the PHP script.
The PHP script can then convert the WordPress post content to PDF, using a PDF class which will be discussed later. Also, in the PHP script, any developer can embed the PDF template that includes the contact address, website logo, etc. The PDF output can then be shown in the browser. It is essential that the PDF output be compatible with most browsers to make the integration user-friendly.
IMPORTANT: Search engines like Google will index PDF documents; therefore, these documents should not be crawled by Googlebot to avoid duplicate content issues with your canonical WordPress post.
Also, when the PDF output is displayed in the browser, it should provide a way for a user to save the PDF to the desktop (users’ local computer).
PDF PHP Class by R and OS
In this WordPress PDF integration using PHP, you are going to use the PHP PDF class by R&OS (http://www.ros.co.nz/pdf/ and http://sourceforge.net/projects/pdf-php/ ). This will do the job of converting the post content to PDF. This article will discuss the details of the integration process, since they are not covered thoroughly by the developers.
Discussion of the details of the PHP class itself is out of the scope of this article, and it is highly recommended that you refer to the authors’ website. The advantage of using the above PHP class is that it does not require installation of PHP modules, which is too technical for the average WordPress blogger. This application does not even require installation by way of a WordPress plug-in.
Below are the most important customizable features of this PHP class (this can be controlled by you):
- Font type of your PDF document (common types are available, such as Helvetica, Arial, etc).
- Header image of your PDF document in JPG format (this can include a logo, etc).
- Font size used in your PDF document.
There are even more features that make it possible to create graphs, tables, etc. within the produced PDF document. But this would now be considered an advanced application and is not covered in this tutorial series.
You need to download the related files for this PHP class. The following is the list:
- Font (folder) — this contains all the font types.
- class.ezpf — PHP class 1 for PDF creation.
- class.pdf – PHP class 2 for PDF creation.
These files need to be uploaded to the root directory of your WordPress blog (implementation details will be discussed in part two). However, to make the files do the job, you need to create your own PDF creator script, which is not part of the class.
The PDF Creator script is a PHP file that will be uploaded to the root directory of your WordPress blog. The function of this file is to include the important PHP-PDF class files by R&OS, as well as to do the necessary job of properly communicating to the MySQL database to fetch the correct WordPress post content upon the user’s request.
For simplicity and for the purpose of this tutorial, we will name this PDF creator script "postpdfcreator.php." Properly structuring this PDF creator is essential for you to better program the script. Below is the flow chart for this script:
Details of the above script will be discussed later, and it is extremely important to sanitize user inputs to prevent any possibility of MySQL injection (details of this process will be covered in part two).
Based on the flowchart, one of the early steps is for the postpdfcreator.php to receive the GET request. Well, you need to formulate a GET request first which will be embedded in your WordPress post template as a hyperlink. For example, the hyperlink below can be used as a GET request input to a script:
In the link above, ID is the Post ID, and this POST ID can be easily retrieved in the postpdfcreator.php script:
This post ID can then be used by the PHP script to grab specific content in the MySQL database.
In a default WordPress URL structure, the above concept can be easily implemented. If a WordPress post URL takes the form of:
then you can use PHP to extract the ID from the URL, and then assign it to a variable. For example:
//step 1. grab URL from the POST and then assigned to variable
//step 2. Do some string manipulation of the URL to extract the ID
//step 3. Assign the ID to a PHP variable
$ID = $postID;
//step 4. Use the post ID PHP variable to append this to a create PDF link in the WordPress post
echo ‘<a href="http://www.someexamplewebsite.com/postpdfcreator.php?ID=’.$ID.’">CLICK TO SAVE THE PDF VERSION OF THIS POST</a>’;
So when viewed in the browser, the above script will produce a hyperlink like:
CLICK TO SAVE THE PDF VERSION OF THIS POST
And the link target URL could be: http://www.someexamplewebsite.com/?p=1. In this example, “1” is the post ID determined by the PHP script.
However, the real challenge is that most WordPress-powered websites do not using the default URL structure that contains the post ID. The websites are implemented with a lot of options that customize the URL structure, which can be any of the following (as you can see in your WordPress administration panel):
- Default structure (see example above).
- Day and name.
- Month and name.
- Custom Structure.
Also, this option even varies from one WordPress website to another, so there has to be a standard way of formulating the GET request, which serves the very important function of grabbing the correct post content from the MySQL database.
In part two, we will continue this tutorial and come up with the standard procedure.