daif/chrome-pdf-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

daif/chrome-pdf-bundle

Composer 安装命令:

composer require daif/chrome-pdf-bundle

包简介

A Symfony bundle for generating PDFs and screenshots using a local Chrome/Chromium binary via Chrome DevTools Protocol, with a clean, builder-based API.

README 文档

README

WYSIWYPDF -- What You See Is What You PDF

The dockerless PDF generator for Symfony. Just Chrome. No containers, no wrapping, no kidding.

composer require daif/chrome-pdf-bundle

Why this bundle?

I wanted to use sensiolabs/gotenberg-bundle -- it's a great and complete Symfony bundle for PDF generation. But it relies on Gotenberg, which requires a running Docker container.

In my case, working in on-premise environments (banking, insurance, regulated industries), Docker is simply not available on production servers. Security policies and infrastructure constraints prevent running containers.

Yet these same machines almost always have a browser installed, or can easily add one. Google Chrome and Chromium are well-maintained, widely trusted, and available on virtually every Linux distribution through standard package managers.

So I built ChromePdfBundle: the same clean builder-based API, but driving Chrome/Chromium directly via the Chrome DevTools Protocol -- no Docker, no external service, no extra infrastructure.

Docker-based solutions ChromePdfBundle
Require Docker + a running container Requires only a Chrome/Chromium binary
HTTP calls to an external service Direct communication via CDP
Extra infrastructure to maintain Uses a browser already on the system
Not usable in Docker-free environments Works everywhere Chrome runs

How to install

Requirements

composer require daif/chrome-pdf-bundle

This installs the bundle along with chrome-php/chrome, the PHP library used to communicate with Chrome via the DevTools Protocol.

Enable the bundle

If not using Symfony Flex, manually register the bundle:

// config/bundles.php

return [
    // ...
    Daif\ChromePdfBundle\DaifChromePdfBundle::class => ['all' => true],
];

Configuration

Create a minimal configuration:

# config/packages/daif_chrome_pdf.yaml

daif_chrome_pdf:
    assets_directory: '%kernel.project_dir%/assets'

The bundle will automatically detect Chrome/Chromium on your system. You can also specify the binary path explicitly:

daif_chrome_pdf:
    chrome_binary: '/usr/bin/google-chrome'

Basic Usage

PDF from Twig template

namespace App\Controller;

use Daif\ChromePdfBundle\ChromePdfInterface;
use Symfony\Component\HttpFoundation\Response;

class InvoiceController
{
    public function generateInvoice(ChromePdfInterface $chromePdf): Response
    {
        return $chromePdf->html()
            ->content('invoice.html.twig', [
                'invoice' => $invoice,
            ])
            ->generate()
            ->stream()
        ;
    }
}

PDF from URL

use Daif\ChromePdfBundle\ChromePdfInterface;

class ReportController
{
    public function generateReport(ChromePdfInterface $chromePdf): Response
    {
        return $chromePdf->url()
            ->url('https://example.com/report')
            ->generate()
            ->stream()
        ;
    }
}

PDF from Markdown

use Daif\ChromePdfBundle\ChromePdfInterface;

class DocController
{
    public function generateDoc(ChromePdfInterface $chromePdf): Response
    {
        return $chromePdf->markdown()
            ->wrapper('wrapper.html.twig')
            ->files('content.md')
            ->generate()
            ->stream()
        ;
    }
}

Screenshot

use Daif\ChromePdfBundle\ChromeScreenshotInterface;

class ScreenshotController
{
    public function capture(ChromeScreenshotInterface $chromeScreenshot): Response
    {
        return $chromeScreenshot->html()
            ->content('page.html.twig')
            ->generate()
            ->stream()
        ;
    }
}

Twig assets

If a template needs to link to a static asset (image, CSS, font), use the {{ chrome_pdf_asset() }} Twig function:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>My PDF</title>
</head>
<body>
    <img src="{{ chrome_pdf_asset('img/logo.png') }}" alt="Logo"/>
</body>
</html>

Advanced Usage

  1. Chrome installation guide
  2. Configuration
  3. Processing (saving, streaming, S3...)
  4. Working with assets
  5. Working with fonts

PDF

  1. HTML Builder
  2. URL Builder
  3. Markdown Builder
  4. Header / Footer

Screenshot

  1. HTML Builder
  2. URL Builder
  3. Markdown Builder

Licence

MIT License (MIT): see the License File for more details.

daif/chrome-pdf-bundle 适用场景与选型建议

daif/chrome-pdf-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「bundle」 「pdf」 「chrome」 「screenshot」 「chromium」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 daif/chrome-pdf-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 daif/chrome-pdf-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 daif/chrome-pdf-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 37
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-11