Weave Code
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 lightweight PHP PDF generator based on FPDF with built-in TrueType/Unicode font support. Create PDFs with custom fonts, UTF-8 text, and simple drawing/layout features without external dependencies—ideal for invoices, reports, and basic document output.

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.
  • 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')).
  • 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.

Gotchas and Tips

  • 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.
  • No Active Maintenance: Last release in 2022—verify license compatibility (LGPL) and consider forking for critical security updates if needed.
  • 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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4