Option 1: Install FPDF via Composer (Recommended)
-
Check if your WordPress hosting allows Composer.
SSH into your server and go to your WP root (wherewp-config.phpis).cd /var/www/html/your-wordpress-site/
-
Require FPDF with Composer:
composer require setasign/fpdf
This will add it inside vendor/.
-
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:
-
Download FPDF library
👉 https://www.fpdf.org/en/download.php -
Extract and place the
fpdf.phpfile in your theme or plugin folder.
Example:/wp-content/themes/your-theme/lib/fpdf/ -
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:
-
-
Create a folder
/wp-content/plugins/wp-fpdf/. -
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';
-
Put the fpdf.php file in /wp-content/plugins/wp-fpdf/fpdf/.
-
-
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 extendedPDF_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->AddPage();
// Title
$pdf->SetFont('Arial','B',12);
$pdf->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->SetFont('Arial','',10);
$fullWidth = $pdf->GetPageWidth() - $pdf->lMargin - $pdf->rMargin; // works inside class methods!
foreach ($datapoint6f as $item) {
$title = htmlspecialchars($item['PointA_title']);
$pdf->MultiCell($fullWidth, 6, utf8_decode($title), 1, 'L');
$pdf->Ln(2);
}
$pdf->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.