itbz/fpdf
Discontinued PSR-0/Composer package for FPDF 1.7, namespaced as \fpdf\FPDF. Includes FPDF_EXTENDED with UTF-8 input, easier page totals, relative image paths, cursor move helpers, graceful font fallback, and GetPdf().
Installation Add the package via Composer:
composer require itbz/fpdf
Ensure autoload is dumped:
composer dump-autoload
First Use Case: Basic PDF Generation Create a controller method or service to generate a simple PDF:
use Itbz\Fpdf\Fpdf;
public function generatePdf()
{
$pdf = new Fpdf();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Hello, Laravel!');
return $pdf->Output('example.pdf', 'D'); // 'D' forces download
}
Where to Look First
vendor/itbz/fpdf/src/ for core classes (Fpdf.php, Fpdf_tpl.php).examples/ for use cases.PDF Generation in Controllers
Use middleware to handle PDF requests (e.g., Route::get('/pdf', [PdfController::class, 'generate'])).
Example:
public function generateInvoice($id)
{
$invoice = Invoice::findOrFail($id);
$pdf = new Fpdf();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, "Invoice #{$invoice->id}", 0, 1, 'C');
// Add more content...
return $pdf->Output("invoice_{$id}.pdf", 'D');
}
Dynamic Content with Views Extend FPDF to render Blade views:
use Itbz\Fpdf\Fpdf;
use Illuminate\Support\Facades\View;
public function renderViewAsPdf($view, $data = [])
{
$pdf = new Fpdf();
$pdf->AddPage();
$html = View::make($view, $data)->render();
$pdf->WriteHTML($html); // Requires FPDF's HTML extension (see Gotchas)
return $pdf->Output();
}
Storing PDFs in Storage
Save generated PDFs to storage/app/public:
use Illuminate\Support\Facades\Storage;
public function savePdf($filename, $content)
{
Storage::put("pdfs/{$filename}", $content);
return Storage::url("pdfs/{$filename}");
}
Queueing PDF Jobs Offload PDF generation to a queue (e.g., for large reports):
use Illuminate\Support\Facades\Queue;
Queue::push(new GeneratePdfJob($userId));
mix.js('js/pdf-handler.js', 'public/js') to handle client-side PDF triggers.return response($pdf->Output('', 'S'), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="report.pdf"',
]);
$mockPdf = Mockery::mock('Itbz\Fpdf\Fpdf');
$mockPdf->shouldReceive('Output')->andReturn('fake-pdf');
HTML Extension Missing
composer require setasign/fpdf-html
use setasign\Fpdf\Html;
$pdf = new Fpdf();
$pdf->AddPage();
$pdf->SetFont('Arial');
$pdf->WriteHTML('<b>Hello, HTML!</b>');
Font Paths
vendor/itbz/fpdf/fonts/ to your project (e.g., public/fonts/).Fpdf.php or use:
$pdf->AddFont('DejaVu', '', 'DejaVuSansCondensed.php');
Memory Limits
memory_limit. Increase it in .env:
MEMORY_LIMIT=512M
Deprecated Methods
SetXY) are deprecated. Use SetLeftMargin/SetTopMargin or Ln() for line breaks.Archived Package Risks
mikehaertl/phpwkhtmltopdf for HTML-to-PDF.$pdf->Output('debug.pdf', 'F') to save intermediate states.try-catch:
try {
$pdf->Cell(0, 0, 'Error-prone content');
} catch (\Exception $e) {
Log::error("PDF Generation Failed: " . $e->getMessage());
}
Custom PDF Classes
Extend Fpdf to add reusable methods:
class CustomPdf extends Fpdf {
public function addLogo($path, $width = 30) {
$this->Image($path, 10, 8, $width);
}
}
Dynamic Styling Create a helper to apply consistent styles:
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetTextColor(0, 0, 0); // Black
$pdf->SetDrawColor(200, 200, 200); // Light gray borders
Multi-Language Support Use FPDF’s built-in language support:
$pdf->AddPage();
$pdf->SetFont('Arial', '', 'utf-8');
$pdf->Write(5, 'Привет, мир!'); // Russian
Barcode Integration
Combine with libraries like milbo/milbo-barcode for dynamic barcodes:
$barcode = new Milbo\Barcode\Barcode();
$barcode->render('png', $pdf->GetX(), $pdf->GetY(), 20, 50, '12345678');
How can I help you explore Laravel packages today?