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

Tcpdf Bundle Laravel Package

acucchieri/tcpdf-bundle

Symfony bundle integrating TCPDF for quick PDF generation and delivery. Build PDFs via a TCPDF-based PdfBuilder and output inline, as download, attachment/base64, string, or save to disk. Includes MultiCell row helper for table-like layouts.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require acucchieri/tcpdf-bundle
    

    Ensure Composer is installed globally.

  2. Enable the Bundle: Add to config/bundles.php (Symfony 4.3+):

    return [
        // ...
        AC\TcpdfBundle\ACTcpdfBundle::class => ['all' => true],
    ];
    

    (Note: Update from AppKernel.php to config/bundles.php for modern Symfony versions.)

  3. First PDF Generation: Inject PdfBuilder into a controller/service:

    use AC\TcpdfBundle\Pdf\PdfBuilder;
    
    public function generatePdf(PdfBuilder $pdfBuilder)
    {
        $pdf = $pdfBuilder->create();
        $pdf->AddPage();
        $pdf->SetFont('helvetica', 'B', 16);
        $pdf->Cell(0, 10, 'Hello, TCPDF in Symfony!', 0, 1, 'C');
        return $pdf->output('example.pdf', 'D'); // 'D' = download
    }
    

First Use Case

Generate a simple invoice PDF with dynamic data:

$pdf = $pdfBuilder->create();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, "Invoice #{$invoice->id}", 0, 1, 'L');
$pdf->Ln(10);
$pdf->writeHTML("<table><tr><td>Item</td><td>Price</td></tr></table>", true, false, true, false, '');
return $pdf->output('invoice_{$invoice->id}.pdf', 'D');

Implementation Patterns

Core Workflows

  1. Dependency Injection: Always inject PdfBuilder into controllers/services for reusability:

    public function __construct(private PdfBuilder $pdfBuilder) {}
    
  2. Reusable PDF Templates: Create a service to encapsulate PDF logic:

    // src/Service/PdfGenerator.php
    class PdfGenerator {
        public function __construct(private PdfBuilder $pdfBuilder) {}
    
        public function generateReport(array $data): string {
            $pdf = $this->pdfBuilder->create();
            $pdf->AddPage();
            $pdf->writeHTML($this->renderReportHtml($data));
            return $pdf->output('report.pdf', 'S'); // 'S' = send to browser
        }
    }
    
  3. Dynamic Content Injection: Use TCPDF’s methods (Cell(), writeHTML(), MultiCell()) with Symfony’s twig:

    $html = $this->twig->render('pdf/report.html.twig', ['data' => $data]);
    $pdf->writeHTML($html);
    
  4. Streaming Large PDFs: For memory efficiency, stream directly to response:

    $response = new StreamedResponse(function () use ($pdf) {
        echo $pdf->Output('large_report.pdf', 'D');
    });
    return $response;
    

Integration Tips

  • Twig Integration: Extend Twig with TCPDF filters for reusable PDF components:

    // src/Twig/PdfExtension.php
    class PdfExtension extends \Twig\Extension\AbstractExtension {
        public function getFilters() {
            return [
                new \Twig\TwigFilter('pdf_table', [$this, 'renderTable']),
            ];
        }
    }
    

    Use in Twig:

    {{ data|pdf_table }}
    
  • Event-Driven PDFs: Trigger PDF generation via Symfony events (e.g., post-order confirmation):

    // src/EventListener/OrderListener.php
    class OrderListener {
        public function __construct(private PdfGenerator $pdfGenerator) {}
    
        public function onOrderConfirmed(OrderEvent $event) {
            $this->pdfGenerator->generateInvoice($event->getOrder());
        }
    }
    
  • Configuration: Override TCPDF defaults in config/packages/ac_tcpdf.yaml:

    ac_tcpdf:
        default:
            format: 'a4'
            orientation: 'P'
            unit: 'mm'
            font_size: 10
            font: 'helvetica'
    

Gotchas and Tips

Pitfalls

  1. Bundle Registration:

    • Symfony 4.3+: Use config/bundles.php (not AppKernel.php).
    • Symfony < 4.3: Ensure ACTcpdfBundle is in the registerBundles() array.
    • Error: ClassNotFoundException → Verify Composer autoload (composer dump-autoload).
  2. Memory Limits:

    • TCPDF can exhaust memory for large PDFs. Use Output() with streaming:
      $pdf->Output('large_file.pdf', 'D', true); // Force download + streaming
      
    • Fix: Increase PHP memory (ini_set('memory_limit', '512M')) or optimize content.
  3. Font Paths:

    • TCPDF requires font files (e.g., helvetica.php). Ensure they’re copied to vendor/tecnickcom/tcpdf/fonts/.
    • Fix: Manually copy fonts or use a custom font path:
      $pdf->SetFont('dejavusans', '', 12, '', true);
      
  4. Caching Issues:

    • TCPDF caches compiled PDFs. Clear cache if changes aren’t reflected:
      php bin/console cache:clear
      
    • Debug: Disable caching temporarily:
      $pdf->setPrintHeader(false);
      $pdf->setPrintFooter(false);
      

Debugging

  1. TCPDF Errors:

    • Enable TCPDF error logging:
      $pdf->setErrorAction('error');
      $pdf->SetErrorHandler('myErrorHandler');
      
    • Check Symfony logs (var/log/dev.log) for TCPDF exceptions.
  2. Output Issues:

    • Blank PDF: Verify AddPage() is called before content.
    • Corrupted PDF: Ensure no unclosed tags in writeHTML() (e.g., <table> must close).
  3. Symfony Response Conflicts:

    • Avoid mixing output() with Symfony’s Response:
      // Wrong:
      return $pdf->output('file.pdf', 'D'); // Returns string, not Response
      
      // Correct:
      return new Response($pdf->Output('file.pdf', 'S'), 200, ['Content-Type' => 'application/pdf']);
      

Extension Points

  1. Custom TCPDF Classes: Extend PdfBuilder to add methods:

    // src/Pdf/CustomPdfBuilder.php
    class CustomPdfBuilder extends PdfBuilder {
        public function addLogo(string $path, float $width = 30) {
            $this->Image($path, 10, 10, $width);
        }
    }
    

    Bind to Symfony’s container:

    # config/services.yaml
    services:
        App\Pdf\CustomPdfBuilder:
            tags: ['ac_tcpdf.pdf_builder']
    
  2. Hooks for Pre/Post Processing: Use Symfony’s compiler passes to modify TCPDF behavior:

    // src/DependencyInjection/Compiler/PdfPass.php
    class PdfPass implements CompilerPassInterface {
        public function process(ContainerBuilder $container) {
            $definition = $container->findDefinition('ac_tcpdf.pdf_builder');
            $definition->addMethodCall('setCustomHeader', ['My Header']);
        }
    }
    
  3. Dynamic Font Loading: Load fonts dynamically from a database:

    $pdf->AddFont('custom_font', '', 'custom_font.php', true);
    

Performance Tips

  • Reuse PDF Instances: Cache PdfBuilder instances for repeated use:
    # config/services.yaml
    services:
        App\Service\PdfGenerator:
            arguments:
                $pdfBuilder: '@ac_tcpdf.pdf_builder'
    
  • Lazy-Load Content: Stream data in chunks for large PDFs:
    $pdf->startTransaction();
    foreach ($largeData as $item) {
        $pdf->Cell(0, 5, $item->name);
        $pdf->endTransaction();
        $pdf->startTransaction();
    }
    
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