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

Chrome Pdf Bundle Laravel Package

dreadnip/chrome-pdf-bundle

Symfony bundle that uses chrome-php/chrome to render HTML to PDF via Chrome/Chromium. Configure the Chrome binary via env, then generate PDFs from HTML with the PdfGenerator service or customize via the BrowserFactory for advanced options.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit The dreadnip/chrome-pdf-bundle is a lightweight Symfony bundle that wraps chrome-php/chrome, a headless Chrome automation library for PDF generation. Its architecture is modular and aligns well with Laravel applications requiring dynamic PDF generation, particularly those leveraging Symfony components (e.g., Laravel 10+ with Symfony 6.4+). The bundle abstracts Chrome/Chromium setup and PDF generation logic, reducing boilerplate while allowing customization via options (e.g., print settings, browser flags). Key strengths include:

  • Symfony Integration: Leverages Symfony’s service container and event system, enabling seamless autowiring and dependency injection in Laravel.
  • Template Support: Provides a base Twig template for structured PDF layouts, with extensibility for custom designs.
  • Headless Chrome: Ensures high-fidelity rendering for complex HTML/CSS/JS, critical for invoices, reports, or marketing collateral.

Integration Feasibility Integration is straightforward for Laravel applications using Symfony components. The bundle requires:

  1. Symfony Compatibility: Laravel 10+ (Symfony 6.4+) is explicitly supported (v0.7.0). For older Laravel versions, validate Symfony component compatibility (e.g., symfony/process).
  2. Chrome/Chromium: Must be installed and accessible via the CHROME_BINARY environment variable. Docker or CI environments may need additional setup (e.g., libx11, libgbm).
  3. Laravel-Symfony Bridge: Use spatie/laravel-symfony-bundle or manually register the bundle in config/bundles.php.

Technical Risk

  • Low to Medium Risk:
    • Dependency Stability: The bundle depends on chrome-php/chrome, which is actively maintained but may introduce breaking changes. Monitor updates.
    • Resource Overhead: Headless Chrome is resource-intensive. Test under load to avoid timeouts or crashes, especially for batch PDF generation.
    • Environment-Specific Issues: Chrome paths or missing system libraries (e.g., on Alpine Linux) may cause failures. Use containerized environments (Docker) to standardize dependencies.
  • Mitigation:
    • Test in staging with production-like payloads.
    • Implement fallback mechanisms (e.g., queue PDF generation jobs with retries).
    • Use Docker for consistent Chrome/Chromium versions across environments.

Key Questions

  1. Symfony/Laravel Version Alignment:
    • What Symfony components are installed in the Laravel app? Ensure compatibility with the bundle’s requirements (e.g., symfony/process, symfony/http-client).
    • For Laravel <10, can the bundle be adapted via a Symfony bridge or fork?
  2. Deployment Complexity:
    • How will Chrome/Chromium be deployed (e.g., system package, Docker image)? Are dependencies (e.g., libx11) available in all environments?
  3. Performance Requirements:
    • What is the expected volume of PDF generation requests? Are there SLAs for response times?
    • How will failures (e.g., Chrome crashes) be handled? Retries? Fallback to a simpler PDF library (e.g., DomPDF)?
  4. Customization Needs:
    • Are there requirements for custom PDF templates, headers/footers, or dynamic content injection?
    • Does the bundle support proxy settings, custom Chrome flags, or authentication headers?
  5. Security:
    • Are there risks of exposing Chrome/Chromium to untrusted input (e.g., XSS in HTML templates)? Sanitize input or use sandboxed Chrome instances.

Integration Approach

Stack Fit

  • Laravel Compatibility:
    • Laravel 10+: Native support via Symfony 6.4+ components. Use spatie/laravel-symfony-bundle for seamless integration.
    • Laravel 9: Possible with minor adjustments (e.g., Symfony 6.3 compatibility). Test thoroughly.
    • Laravel 8 or Older: Requires manual shimming or a forked bundle version. Not recommended due to Symfony 5.x limitations.
  • PHP Version: Requires PHP 8.1+ (aligned with Laravel 10+ and Symfony 6.4+).
  • Symfony Components: The bundle relies on symfony/process, symfony/http-client, and twig. Ensure these are installed and version-compatible.

Migration Path

  1. Prerequisites:
    • Install Chrome/Chromium and dependencies in all environments. For Docker, use an image with preinstalled Chrome (e.g., chromium:latest).
    • Example Dockerfile snippet:
      RUN apt-get update && apt-get install -y chromium libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libasound2
      
  2. Bundle Installation:
    • Add to composer.json:
      composer require dreadnip/chrome-pdf-bundle
      
    • Register the bundle in config/bundles.php:
      return [
          // ...
          Dreadnip\ChromePdfBundle\ChromePdfBundle::class => ['all' => true],
      ];
      
  3. Configuration:
    • Set Chrome path in .env:
      CHROME_BINARY="/usr/bin/chromium-browser"
      
    • Publish and configure the bundle’s config (optional):
      php artisan vendor:publish --tag=chrome-pdf-config
      
  4. Service Integration:
    • Inject PdfGenerator into controllers or commands:
      use Dreadnip\ChromePdfBundle\Service\PdfGenerator;
      
      public function generatePdf(PdfGenerator $pdfGenerator) {
          $html = view('pdf.template')->render();
          $path = $pdfGenerator->generate($html, 'path/to/output.pdf');
      }
      
  5. Testing:
    • Test PDF generation in a staging environment with sample templates.
    • Validate edge cases (e.g., large HTML, complex CSS, JavaScript-heavy pages).

Compatibility

  • Symfony 6.4+: Fully supported (Laravel 10+).
  • Symfony 5.4–6.3: Likely compatible but untested. Validate against the Laravel-installed Symfony components.
  • Legacy Systems: Not recommended for Laravel <9 or PHP <8.1 due to Symfony version constraints.

Sequencing

  1. Phase 1: Setup
    • Install Chrome/Chromium and dependencies in all environments.
    • Add the bundle to composer.json and register it.
  2. Phase 2: Configuration
    • Configure Chrome paths and bundle settings.
    • Extend the base Twig template if custom layouts are needed.
  3. Phase 3: Integration
    • Implement PDF generation in non-critical routes (e.g., admin reports).
    • Test with sample payloads and validate output quality.
  4. Phase 4: Rollout
    • Gradually enable PDF generation for high-priority features (e.g., invoices).
    • Monitor performance and failures in production.

Operational Impact

Maintenance

  • Dependency Management:
    • Monitor updates to dreadnip/chrome-pdf-bundle and chrome-php/chrome for breaking changes.
    • Pin versions in composer.json to avoid unexpected updates:
      "dreadnip/chrome-pdf-bundle": "^0.7",
      "chrome-php/chrome": "^1.5"
      
  • Configuration:
    • Centralize Chrome paths and timeouts in environment variables (e.g., .env) to avoid hardcoding.
    • Example:
      CHROME_BINARY=/usr/bin/chromium-browser
      CHROME_PDF_TIMEOUT=30
      
  • Logging:
    • Extend the bundle’s logging to capture failures (e.g., Chrome crashes, timeouts). Use Laravel’s Log facade:
      use Psr\Log\LoggerInterface;
      
      public function generatePdf(PdfGenerator $pdfGenerator, LoggerInterface $logger) {
          try {
              $path = $pdfGenerator->generate($html, 'output.pdf');
          } catch (\Exception $e) {
              $logger->error('PDF generation failed', ['error' => $e->getMessage()]);
              throw $e;
          }
      }
      
  • Updates:
    • Test bundle updates in a staging environment before deploying to production.
    • Pay special attention to changes in chrome-php/chrome (e.g., new options, deprecations).

Support

  • Common Issues:
    • Chrome Not Found: Verify CHROME_BINARY points to a valid path. Use absolute paths in .env.
    • Timeouts: Increase the timeout option or optimize HTML/CSS for faster rendering.
    • Missing Dependencies: Ensure system libraries (e.g., libx11) are installed in all environments.
    • Memory Limits: Headless Chrome may exceed PHP’s memory limit. Adjust memory_limit in php.ini or use smaller chunks for batch processing.
  • **Debugging
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.
althinect/enum-permission
andydefer/laravel-actions
aimeos/prisma
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
spatie/mailcoach-vapor