itbz/fpdi
Discontinued unofficial PSR-4 fork of Setasign FPDI (Free PDF Document Importer). Instantiates via \fpdi\FPDI and supports TCPDF with patched versions. Prefer the official FPDI via composer/GitHub, ideally FPDI 2.0+.
Installation:
composer require itbz/fpdi:~1.0
Ensure itbz/fpdf (~1.7) is also installed (dependency).
Basic Usage:
// Load TCPDF first (required for compatibility)
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor/tecnickcom/tcpdf/tcpdf.php';
$fpdi = new \fpdi\FPDI();
$fpdi->AddPage();
$fpdi->setSourceFile('existing.pdf');
$tplIdx = $fpdi->importPage(1); // Import first page
$fpdi->useTemplate($tplIdx); // Overlay template
$fpdi->SetFont('helvetica');
$fpdi->Write(10, 10, 'Hello, FPDI!');
$fpdi->Output('output.pdf', 'D'); // Download
Key Files:
vendor/fpdi/FPDI.php (core class)vendor/fpdi/fpdi.php (entry point)vendor/tecnickcom/tcpdf/tcpdf.php (dependency)Merge PDFs with Overlays:
$fpdi = new \fpdi\FPDI();
$fpdi->AddPage();
$fpdi->setSourceFile('template.pdf');
$tplIdx = $fpdi->importPage(1);
$fpdi->useTemplate($tplIdx);
$fpdi->SetFont('helvetica', '', 12);
$fpdi->Write(50, 50, 'Dynamic Content');
$fpdi->Output('merged.pdf', 'F'); // Save to file
PDF Importing:
$fpdi->setSourceFile('source.pdf');
$pageCount = $fpdi->setSourceFile('source.pdf'); // Returns page count
$tplIdx = $fpdi->importPage(2); // Import specific page
Template Overlay:
$fpdi->AddPage();
$fpdi->useTemplate($tplIdx); // Overlay imported page
$fpdi->SetXY(10, 10); // Position cursor
$fpdi->Cell(0, 10, 'Text'); // Draw content
Multi-Page Handling:
for ($i = 1; $i <= $pageCount; $i++) {
$fpdi->AddPage();
$tplIdx = $fpdi->importPage($i);
$fpdi->useTemplate($tplIdx);
// Add dynamic content per page
}
Laravel Service Provider:
namespace App\Providers;
use fpdi\FPDI;
class FpdiServiceProvider extends ServiceProvider {
public function register() {
$this->app->singleton(FPDI::class, function ($app) {
return new FPDI();
});
}
}
Dynamic Content Injection:
$fpdi = app(FPDI::class);
$fpdi->setSourceFile(storage_path('templates/base.pdf'));
$tplIdx = $fpdi->importPage(1);
$fpdi->useTemplate($tplIdx);
$fpdi->SetFont('times', '', 14);
$fpdi->Write(50, 50, $this->generateDynamicText());
Batch Processing:
$pdfs = ['doc1.pdf', 'doc2.pdf'];
foreach ($pdfs as $pdf) {
$fpdi->setSourceFile($pdf);
for ($i = 1; $i <= $fpdi->setSourceFile($pdf); $i++) {
$fpdi->AddPage();
$fpdi->importPage($i);
$fpdi->useTemplate($fpdi->importPage($i));
// Add watermark or metadata
}
$fpdi->Output("processed_{$pdf}", 'F');
}
TCPDF Loading Order:
Fatal error: Class 'TCPDF' not found.tcpdf.php before instantiating FPDI:
require_once __DIR__ . '/vendor/tecnickcom/tcpdf/tcpdf.php';
$fpdi = new \fpdi\FPDI();
Page Indexing:
$fpdi->importPage(1) for the first page.Memory Limits:
$fpdi->SetCreator('Laravel App');
$fpdi->SetAuthor('User');
$fpdi->SetCompression(true); // Reduce file size
Font Conflicts:
Could not find/load font.$fpdi->AddFont('dejavu', '', 'DejaVuSansCondensed.php');
$fpdi->SetFont('dejavu');
Verify Page Count:
$pageCount = $fpdi->setSourceFile('file.pdf');
if ($pageCount === false) {
throw new \Exception("Failed to load PDF");
}
Check Template Overlay:
if (!$fpdi->useTemplate($tplIdx)) {
throw new \Exception("Template overlay failed");
}
Log Errors:
$fpdi->ErrorMsg(); // Returns last error message
Custom Page Processing:
$fpdi->pageScript = <<<EOD
/setpagedevice {/PageSize [595 842] /ImagingBBox null} bind def
EOD;
Metadata Injection:
$fpdi->SetTitle('Custom Title');
$fpdi->SetSubject('PDF Subject');
$fpdi->SetKeywords('keyword1, keyword2');
Annotations/Bookmarks:
$fpdi->AddBookmark('Section 1', 0, 0);
$fpdi->AddLink();
Output Modes:
'I' (inline), 'D' (download), 'F' (save to file), 'S' (return as string).'S' for further processing:
$pdfContent = $fpdi->Output('', 'S');
Storage::put('custom_path.pdf', $pdfContent);
Coordinate Systems:
$fpdi->SetXY(50, 50); // Top-left corner
$fpdi->Cell(0, 10, 'Text', 0, 1, 'C'); // Centered
How can I help you explore Laravel packages today?