Atomic Smash homepage splash

Create PDF Documents in WordPress with FPDF

Words byAnthony HartnellJanuary 11, 2018

In this tutorial, I will show you how to create a PDF document using the content from posts within WordPress. I’ll be using FPDF –  a free and open source PDF library written in PHP, and integrate it into a custom admin plugin to manage the code.

What is FPDF?

FPDF Library Website

FPDF is a lightweight PDF library written in PHP. It weighs in at less than 100Kb which is a fraction of the size of other libraries like mPDF (40Mb~). The number of features and functions are really good and the manual is very clear to follow. It’s also a powerful library that can be extended easily to offer only the functionality you need.

By combining it with WordPress a range of possibilities can be achieved, for example, creating a pdf document from image galleries and building custom reports. In fact, every bit of data inside WordPress can be sent to FPDF when needed.

Aim of this tutorial

In the following example I will produce a single PDF file taken from the content in the 5 most recent posts. I’ll start by setting up the folder structure for a plugin and adding the code to build a custom admin interface. This will give me a page from which to create the PDF.

First download FDPF from The latest version as of writing is 1.81. I’ve unzipped this in to a plugin folder called atomicsmash-pdf-tutorial and created two blank php files inside of this called atomicsmash-pdf-helper-functions.php and atomicsmash-pdf-tutorial.php.

My folder structure now looks like this:

Add the following code to  atomicsmash-pdf-tutorial.php:

* Plugin Name: Atomic Smash FPDF Tutorial
* Description: A plugin created to demonstrate how to build a PDF document from WordPress posts.
* Version: 1.0
* Author: Anthony Hartnell
* Author URI:

if ( ! defined( 'ABSPATH' ) ) {

include( 'atomicsmash-pdf-helper-functions.php');
$pdf = new PDF_HTML();

if( isset($_POST['generate_posts_pdf'])){

add_action( 'admin_menu', 'as_fpdf_create_admin_menu' );
function as_fpdf_create_admin_menu() {
    $hook = add_submenu_page(
        'Atomic Smash PDF Generator',
        'Atomic Smash PDF Generator',

function output_pdf() {
    $posts = get_posts( 'posts_per_page=5' );

    if( ! empty( $posts ) ) {
        global $pdf;
        $title_line_height = 10;
        $content_line_height = 8;

        $pdf->SetFont( 'Arial', '', 42 );
        $pdf->Write(20, 'Atomic Smash FPDF Tutorial');

        foreach( $posts as $post ) {

            $pdf->SetFont( 'Arial', '', 22 );
            $pdf->Write($title_line_height, $post->post_title);

            // Add a line break

            // Image
            $page_width = $pdf->GetPageWidth() - 20;
            $max_image_width = $page_width;

            $image = get_the_post_thumbnail_url( $post->ID );
            if( ! empty( $image ) ) {
                $pdf->Image( $image, null, null, 100 );
            // Post Content
            $pdf->SetFont( 'Arial', '', 12 );


function as_fpdf_create_admin_page() {
<div class="wrap">
    <h1>Atomic Smash - Wordpress PDF Generator</h1>
    <p>Click below to generate a pdf from the content inside all the Wordpress Posts. </p>
    <p>Each post will be on its own pdf page containing the post title and post content.</p>
	<form method="post" id="as-fdpf-form">
        <button class="button button-primary" type="submit" name="generate_posts_pdf" value="generate">Generate PDF from Wordpress Posts</button>

After saving, go to the plugins page and you should see it listed:

After activating it you’ll now see a link to the plugin admin page in the Tools menu:

What is happening here?

On line 14 I’m including a php file which will contain some FPDF helper functions. By default, FPDF does not interpret HTML tags or external links so we have to extend the library with some extra code. This code contains a class called PDF_HTML which extends the standard FPDF class- you can see that being instantiated on line 15 and stored in the $pdf variable. The helper functions will be added at the end.

Triggering the 'output_pdf' function

On line 17 I’m checking if a $_POST request containing a key called generate_posts_pdf has a value that is set and not empty. This is being sent by the submit button in the admin page html on line 82. The value is only sent if the button is pressed so, effectively it’s a simple way of adding interaction to a plugin to kick off a function.

The output_pdf function uses get_posts and loops through each one to add content to the PDF with some simply named functions called AddPage and Write. The documentation is very well written and mostly self explanatory but outputting the post_content will not work without adding the helper functions.

Adding the helper functions

You might be wondering why the FPDF library can’t interpret HTML tags. I believe this is what makes the library so small to begin with. Their tutorials demonstrate that you are encouraged to extend the functionality of the library and add in the features where necessary. This makes it more customisable.

On Line 66 I’m utilising the WriteHTML function and this is included in the code that should go in the atomicsmash-pdf-helper-functions.php file.  The code I modified can be found here:

Enter the following code into atomicsmash-pdf-helper-functions.php:

include( 'fpdf181/fpdf.php');
 * Modified from

//function hex2dec
//returns an associative array (keys: R,G,B) from
//a hex html code (e.g. #3FE5AA)
function hex2dec($color = "#000000"){
    $R = substr($color, 1, 2);
    $rouge = hexdec($R);
    $V = substr($color, 3, 2);
    $vert = hexdec($V);
    $B = substr($color, 5, 2);
    $bleu = hexdec($B);
    $tbl_color = array();
    return $tbl_color;

//conversion pixel -> millimeter at 72 dpi
function px2mm($px){
    return $px*25.4/72;

function txtentities($html){
    $trans = get_html_translation_table(HTML_ENTITIES);
    $trans = array_flip($trans);
    return strtr($html, $trans);
class PDF_HTML extends FPDF
//variables of html parser
protected $B;
protected $I;
protected $U;
protected $HREF;
protected $fontList;
protected $issetfont;
protected $issetcolor;

function __construct($orientation='P', $unit='mm', $format='A4')
    //Call parent constructor
    $this->fontlist=array('arial', 'times', 'courier', 'helvetica', 'symbol');

function WriteHTML($html)
    //HTML parser
    $html=str_replace("\n",' ',$html);
    foreach($a as $i=>$e)
                //Extract attributes
                $a2=explode(' ',$e);
                foreach($a2 as $v)

function OpenTag($tag, $attr)
    //Opening tag
        case 'STRONG':
        case 'EM':
        case 'B':
        case 'I':
        case 'U':
        case 'A':
        case 'IMG':
            if(isset($attr['SRC']) && (isset($attr['WIDTH']) || isset($attr['HEIGHT']))) {
                    $attr['WIDTH'] = 0;
                    $attr['HEIGHT'] = 0;
                $this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
        case 'TR':
        case 'BLOCKQUOTE':
        case 'BR':
        case 'P':
        case 'FONT':
            if (isset($attr['COLOR']) && $attr['COLOR']!='') {
            if (isset($attr['FACE']) && in_array(strtolower($attr['FACE']), $this->fontlist)) {

function CloseTag($tag)
    //Closing tag
    if($tag=='B' || $tag=='I' || $tag=='U')
        if ($this->issetcolor==true) {
        if ($this->issetfont) {

function SetStyle($tag, $enable)
    //Modify style and select corresponding font
    $this->$tag+=($enable ? 1 : -1);
    foreach(array('B','I','U') as $s)

function PutLink($URL, $txt)
    //Put a hyperlink

}//end of class

The end result

After clicking Tools > Atomic Smash PDF Generator you should see the admin page like so:

Click on the button to download the PDF which should look similar to the image below:

If you try this out, please feel free to leave any comments or suggestions below.

Profile picture ofAnthony Hartnell

Anthony HartnellDeveloper

Anthony works in the development team and enjoys creating plugins and integrating maps, libraries and other APIs into Wordpress.

Go back to top

Keep up to date with Atomic news