PDF in WordPress using FPDF : Custom PDF Generator

PDF_WordPress_FPDF_PDF_Generator_ipdata_code

Option 1: Install FPDF via Composer (Recommended)

  1. Check if your WordPress hosting allows Composer.
    SSH into your server and go to your WP root (where wp-config.php is).

    cd /var/www/html/your-wordpress-site/
    
  2. Require FPDF with Composer:

     composer require setasign/fpdf

    This will add it inside vendor/.

  3. Include FPDF in your theme/plugin PHP file:

    require_once __DIR__ . '/vendor/autoload.php';
    
    use FPDF\FPDF;
    
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello WordPress + FPDF!');
    $pdf->Output();
    

Option 2: Manually Add FPDF

If you don’t have Composer access:

  1. Download FPDF library
    👉 https://www.fpdf.org/en/download.php

  2. Extract and place the fpdf.php file in your theme or plugin folder.
    Example: /wp-content/themes/your-theme/lib/fpdf/

  3. Include it in your code:

     
    require get_template_directory() . '/lib/fpdf/fpdf.php';
    
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'FPDF in WordPress!');
    $pdf->Output();
    
    

Option 3: Create a Small Plugin

If you want to keep things neat:

    1. Create a folder /wp-content/plugins/wp-fpdf/.

    2. Inside it, create wp-fpdf.php:

       
      <?php
      /*
      Plugin Name: WP FPDF
      Description: Generate PDFs using FPDF inside WordPress.
      Version: 1.0
      */
      
      require_once plugin_dir_path(__FILE__) . 'fpdf/fpdf.php';
      
    3. Put the fpdf.php file in /wp-content/plugins/wp-fpdf/fpdf/.

  1. Activate the plugin in WP Admin → Plugins.

Since WordPress runs PHP the same way, the PDF generation code (using FPDF / TCPDF / mPDF) can be integrated. The “Cannot access protected property PDF_Table::$lMargin” issue is not WordPress-specific — it comes from how the library is written.

Here’s how you can integrate properly in WordPress:

4. Load the PDF library

  • Put fpdf.php (or your extended PDF_Table.php) inside your theme or a custom plugin folder.

/wp-content/plugins/custom-pdf/
fpdf.php
pdf-table.php
custom-pdf.php

In custom-pdf.php (your plugin main file):

/**
* Plugin Name: Custom PDF Generator
*/
require_once plugin_dir_path(__FILE__) . 'fpdf.php';
require_once plugin_dir_path(__FILE__) . 'pdf-table.php';

5. Create a shortcode for generating PDFs

You can make a shortcode so you can add [generate_pdf] anywhere in posts/pages:

add_shortcode('generate_pdf', function() {
if (isset($_GET['download_pdf'])) {
// Include PDF class
$pdf = new PDF_Table();
$pdf-&gt;AddPage();

// Title
$pdf-&gt;SetFont('Arial','B',12);
$pdf-&gt;Cell(0,10,utf8_decode("Brief Summary of Session"),0,1,'L',true);

// Sample JSON (replace with dynamic data from DB or WP fields)
$point6f = '[{"PointA_title":"k1 Brief of Session"},{"PointA_title":"k2 Brief of Session"}]';
$datapoint6f = json_decode($point6f, true);

$pdf-&gt;SetFont('Arial','',10);
$fullWidth = $pdf-&gt;GetPageWidth() - $pdf-&gt;lMargin - $pdf-&gt;rMargin; // works inside class methods!

foreach ($datapoint6f as $item) {
$title = htmlspecialchars($item['PointA_title']);
$pdf-&gt;MultiCell($fullWidth, 6, utf8_decode($title), 1, 'L');
$pdf-&gt;Ln(2);
}

$pdf-&gt;Output('D', 'session-summary.pdf'); // Force download
exit;
}

// Button to trigger PDF
return '<a class="button" href="?download_pdf=1">Download PDF</a>';
});

 

6. Use in WordPress

  • Place [generate_pdf] inside a page.

  • It will show a button → clicking it generates and downloads the PDF with full-width rows.

Previous Article

Create a chatbot in PHP using the Gemini API (from Google AI)

Next Article

How to Generate PDF with Full-Width Rows in WordPress Using FPDF

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨