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

Fpdi Laravel Package

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+.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require itbz/fpdi:~1.0
    

    Ensure itbz/fpdf (~1.7) is also installed (dependency).

  2. 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
    
  3. Key Files:

    • vendor/fpdi/FPDI.php (core class)
    • vendor/fpdi/fpdi.php (entry point)
    • vendor/tecnickcom/tcpdf/tcpdf.php (dependency)

First Use Case

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

Implementation Patterns

Core Workflows

  1. PDF Importing:

    $fpdi->setSourceFile('source.pdf');
    $pageCount = $fpdi->setSourceFile('source.pdf'); // Returns page count
    $tplIdx = $fpdi->importPage(2); // Import specific page
    
  2. Template Overlay:

    $fpdi->AddPage();
    $fpdi->useTemplate($tplIdx); // Overlay imported page
    $fpdi->SetXY(10, 10);        // Position cursor
    $fpdi->Cell(0, 10, 'Text');  // Draw content
    
  3. Multi-Page Handling:

    for ($i = 1; $i <= $pageCount; $i++) {
        $fpdi->AddPage();
        $tplIdx = $fpdi->importPage($i);
        $fpdi->useTemplate($tplIdx);
        // Add dynamic content per page
    }
    

Integration Tips

  • 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');
    }
    

Gotchas and Tips

Common Pitfalls

  1. TCPDF Loading Order:

    • Error: Fatal error: Class 'TCPDF' not found.
    • Fix: Load tcpdf.php before instantiating FPDI:
      require_once __DIR__ . '/vendor/tecnickcom/tcpdf/tcpdf.php';
      $fpdi = new \fpdi\FPDI();
      
  2. Page Indexing:

    • Gotcha: Pages are 1-indexed (not 0-indexed).
    • Fix: Use $fpdi->importPage(1) for the first page.
  3. Memory Limits:

    • Issue: Large PDFs may hit memory limits.
    • Workaround: Process pages in batches or optimize TCPDF settings:
      $fpdi->SetCreator('Laravel App');
      $fpdi->SetAuthor('User');
      $fpdi->SetCompression(true); // Reduce file size
      
  4. Font Conflicts:

    • Error: Could not find/load font.
    • Fix: Ensure fonts are added to TCPDF:
      $fpdi->AddFont('dejavu', '', 'DejaVuSansCondensed.php');
      $fpdi->SetFont('dejavu');
      

Debugging Tips

  • 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
    

Extension Points

  1. Custom Page Processing:

    $fpdi->pageScript = <<<EOD
    /setpagedevice {/PageSize [595 842] /ImagingBBox null} bind def
    EOD;
    
  2. Metadata Injection:

    $fpdi->SetTitle('Custom Title');
    $fpdi->SetSubject('PDF Subject');
    $fpdi->SetKeywords('keyword1, keyword2');
    
  3. Annotations/Bookmarks:

    $fpdi->AddBookmark('Section 1', 0, 0);
    $fpdi->AddLink();
    

Configuration Quirks

  • Output Modes:

    • 'I' (inline), 'D' (download), 'F' (save to file), 'S' (return as string).
    • Tip: Use 'S' for further processing:
      $pdfContent = $fpdi->Output('', 'S');
      Storage::put('custom_path.pdf', $pdfContent);
      
  • Coordinate Systems:

    • Gotcha: Y-axis increases downward (like FPDF).
    • Fix: Adjust coordinates accordingly:
      $fpdi->SetXY(50, 50); // Top-left corner
      $fpdi->Cell(0, 10, 'Text', 0, 1, 'C'); // Centered
      
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky