PHP: Addings Images to WordPress PDFs

In this programming tutorial, you will be learning how to use PHP and the R&OS PDF class to convert WordPress posts to pdf’s without losing article images in the process.

Previously, I published two articles on converting WordPress posts to PDF files without using a plug-in:

Part 1:

Part 2:

In those examples, the scripts worked with one small glitch: they do not have the ability to include the WordPress post images in the PDF file along with the text content.

As a demonstration of how the original application works, feel free to test this script:; and download this PHP class: You can also try implementing it on your own test site.

For example if you have this WordPress post URL:, the converted PDF file of this post using the original scripts provided above is this:

You will not be able to see the images included in the PDF file. This tutorial will add this functionality. It will make the PDF equivalent of your WordPress post as complete as possible with image support. This is a common problem when converting web page content to PDF. For example on this page: , try clicking the SEO Chat “PDF Version Of Article” link. You will note that the converted PDF does not include images.

Strategy of Implementation

The first step in the analysis, is to break down the WordPress post into components or parts such as those shown below (assume a WordPress post with three images, although this application is designed to work with several images):

This means you can use PHP to grab the post content from the WordPress database, then filter or segregate the content into different sections (in terms of  text and images) such as shown above. Then, once the content and images are well segregated, each of these components can safely be rendered by the PDF class.

In the R&OS PDF class, the following are important parameters when outputting PDFs in PHP:

Writing text content to PDF:
[code]$pdf->eztext(“This is your text”,12);[/code]

The above line will write the text "This is your text" to a PDF document with font size 12.

Embedding Images to PDF

In this code:


The above line will embed images in the PDF document, with images hosted at this URL:, using padding set to 0, width to 300, resize to none, and justification to left.

Suppose you want to define two arrays to define the segregated sections as shown in the screenshot above. For the post text blocks, you would assign them in the following manner:

[code]$textblocks= array(); $textblocks[0]= text content for the first block or paragraph. $textblocks[1]= text content for the second block. $textblocks[2] = text content for the third block.[/code]

For images, it would be:

[code]$imageurls = array(); $imageurls[0] = Image URL for the first image. $imageurls[1]= Image URL for the second image.[/code]

Now that its well defined, you can write it to a PDF document using the R&OS class by following the structure in the previous screenshot:

[code]//Printing the first text block to PDF: $pdf->;eztext($textblocks[0],12); //Printing the first post image to PDF: $pdf->;ezImage($imageurls[0],0,300,’none’,’left’); //Printing the second text block to PDF: $pdf->;eztext($textblocks[1],12); //Printing the second post image to PDF: $pdf->;ezImage($imageurls[1],0,300,’none’,’left’); //Printing the third text block to PDF: $pdf->;eztext($textblocks[2],12);[/code]

You can employ loops in the process to automate this procedure for different WordPress posts with different number of blocks, as well as with number of images in the post.

The Problem with the PHP Strip Tags Function

If you are using the old script, you might have noticed that if your website content discusses PHP scripts, HTML coding, etc,  they won’t be rendered in the PDF document (the same situation with the images) because it uses the PHP strip_tags function:

Therefore, a strip_tags function needs to be customized to allow HTML tags, comments and PHP scripts, as discussed in the WordPress post, to be included in the PDF document. The following PHP function accomplishes this:

[code] >;?php function stripTags($text, $tags) { //Title: Don’t strip PHP tags function //Credits: Abdul Al-hasany // // replace php and comments tags so they do not get stripped $text = preg_replace(“@Related Articles
[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye