Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Tfpdf Laravel Package

setasign/tfpdf

tFPDF is a UTF-8 capable PDF generation library for PHP with TrueType font subset embedding. Composer package setasign/tfpdf provides the official tFPDF release with autoloader support and no namespace.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install via Composer: composer require setasign/tfpdf. This package is a drop-in replacement for FPDF with UTF-8 support.
  • Pin to ^1.34 (or at minimum >=1.34) — this patch fixes faulty CMAP generation, which affects text extraction/copy-paste from generated PDFs.
  • Start by extending \setasign\Tfpdf\Tfpdf instead of FPDF in your PDF-generating classes.
  • For basic usage, instantiate the class, set font (must be a TTF font), add a page, and output:
    $pdf = new \setasign\Tfpdf\Tfpdf();
    $pdf->AddFont('DejaVu', '', 'DejaVuSans.ttf', true);
    $pdf->AddPage();
    $pdf->SetFont('DejaVu', '', 12);
    $pdf->Write(0, 'Hello, 世界 🌍');
    $pdf->Output();
    
  • First place to look: check the examples/ directory in the repo and examples/font/ for TTF setup instructions.
  • Primary use case: generating multilingual PDFs (e.g., Arabic, Cyrillic, CJK via fallback, though limited) without external dependencies.

Implementation Patterns

  • Font Registration: Use AddFont() with the 4th parameter set to true to register UTF-8-capable TrueType fonts. Fonts must reside in the fonts/ directory (usually vendor/setasign/tfpdf/fonts/ or custom path via setFontLocation()).
  • Custom Font Paths: Override the default font directory with Tfpdf::setFontLocation($path) before calling AddFont().
  • Integration with Laravel:
    • Create a service provider or helper that initializes a configured Tfpdf instance (e.g., preloading DejaVu fonts).
    • Use in console commands or jobs where PDFs are generated without web output (use Output('pdf.pdf', 'F')).
    • Wrap generation in a queued job that writes to Storage::disk('local') and returns a signed URL; keeps the request cycle fast.
  • Styling & Layout: Use same FPDF methods (Cell, MultiCell, SetXY)—UTF-8 only affects text encoding, not layout.
  • Fallback Strategy: Since full CJK isn’t supported out-of-the-box, combine with SetFont('DejaVu'...) for basic Latin + diacritics, and consider MultiCell for line-breaking text.
  • Verifying text extraction: after upgrading to 1.34, add a smoke test that generates a PDF containing accented/Cyrillic text and asserts the output extracts back correctly (e.g. via pdftotext in CI). This is the behaviour the CMAP fix targets, and it is the cheapest way to catch regressions in font/CMAP handling.

Gotchas and Tips

  • Stale font definition files after upgrading: TfPDF caches generated font definition files (.php/.z artifacts written next to the .ttf) which embed the character map. After moving to 1.34, delete any previously generated definitions for your fonts and let them be rebuilt — otherwise you keep shipping PDFs with the old faulty CMAP even though the library is patched.
  • CMAP / ToUnicode correctness: pre-1.34, some fonts produced an invalid CMAP, so text rendered fine visually but extracted as garbage, was unsearchable, or copy-pasted incorrectly in Acrobat/Preview. If you have downstream text-extraction, indexing, or accessibility (PDF/UA) requirements, this upgrade is worth taking deliberately rather than opportunistically.
  • No True CJK Support: Tfpdf supports UTF-8 byte sequences, but rendering depends on font coverage. DejaVu Sans covers most Latin, Cyrillic, Greek, and basic Arabic—but not Chinese/Japanese/Korean fully. For full CJK, consider TCPDF or dompdf instead.
  • Font Loading Path Issues: The AddFont() method expects fonts relative to the fonts/ directory unless setFontLocation() is called. Common error: font not found due to incorrect path—use absolute paths.
  • Empty Output on CLI: In console environments, ensure output_buffering = Off and zlib.output_compression = Off in php.ini. Tfpdf may silently fail to output if headers have already been sent.
  • Debugging Font Rendering: Use Tfpdf::getError() after AddFont() or SetFont() to check for issues.
  • Compatibility: This is not PSR-0/PSR-4 compatible by default; ensure autoloading via Composer is configured correctly. Avoid manual require_once statements.
  • Maintenance cadence: releases are infrequent and patch-sized (1.34 is a single CMAP bugfix, not a feature release), so don't expect upstream fixes to land quickly. Verify license compatibility (LGPL) and consider forking or vendoring a pinned copy if you depend on security-sensitive behaviour.
  • Extension Tip: To add custom content (e.g., barcodes, shapes), extend Tfpdf and override Header(), Footer(), or add custom methods.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels