browsershot

spatieMIT活跃维护

用 PHP 把网页 HTML 转成图片、PDF 或字符串,底层调用无头浏览器渲染,适合生成网页截图和导出 PDF 报告。

Convert HTML to an image, PDF or string

5.5.0发布于 2026-11-06 09:42 UTC5,251star507fork同步于 2026-11-10 17:37 UTC

怎么上手

由 README 提炼,执行前请确认
composer require spatie/browsershot
npm -g i puppeteer
硬件要求按 README 说明

README 未说明显存或 GPU 要求;需能运行 Node 与 Puppeteer(无头 Chrome),README 未列出具体 GPU 厂商或型号

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

用 Puppeteer 驱动无头 Chrome,把网页或 HTML 转成图片、PDF 或执行后的 HTML

用 PHP 把网页 HTML 转成图片、PDF 或字符串,底层调用无头浏览器渲染,适合生成网页截图和导出 PDF 报告。

核心能力

  • 调用 Puppeteer 无头 Chrome 渲染网页截图或 PDF
  • 支持 url、html、htmlFromFilePath 三种输入源
  • 按 save 路径扩展名自动决定输出图片还是 PDF
  • 可获取 JS 执行后的 body HTML 与页面触发的请求列表
  • 默认拦截 html 输入的本地文件访问,需显式开启

容易踩的坑

必须额外安装 Node 与 Puppeteer,否则无法渲染;html() 默认禁止读取本地文件,需 allowLocalFileAccess() 且仅用于可信内容;测试还需 poppler-utils 提供的 pdftotext CLI

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本5.5.0
  1. 5.5.0最新正式版2026-11-06 · 2026-11-06 09:42 UTC

    Local file access controls for supplied HTML

    Browsershot now blocks local file requests by default when rendering HTML passed to html() or setHtml(). The restriction is enforced on resolved browser requests, covering absolute filesystem paths, literal file URLs and file URLs constructed by JavaScript.

    For trusted HTML that intentionally loads local assets, explicitly opt in:

    Browsershot::html($html)
        ->allowLocalFileAccess()
        ->savePdf("invoice.pdf");
    

    Use allowLocalFileAccess(false) to restore the default restriction.

    Compatibility and security

    • Local images, stylesheets, scripts and iframes previously loaded through html() may now be missing from generated output unless local file access is explicitly enabled.
    • Literal file URLs are blocked during rendering instead of being rejected by html(), allowing the fluent opt-in and plain text mentioning file URLs.
    • htmlFromFilePath() retains its existing local-resource behavior. HTTP(S) resources and explicit content URLs retain their behavior.
    • Only enable local file access for trusted HTML: Chrome can read any local file its process has permission to access. Browsershot is not a sandbox for untrusted content, and these controls do not prevent SSRF.

    Includes regression tests for local-file disclosure payloads, plain-text file URLs, the one-time initial-document permission, local assets, opt-in revocation and HTTP resources. The local suite passed 157 tests with 228 assertions, and all PHP 8.2–8.6 CI matrix jobs passed before merge.

    What Changed

    Full Changelog: https://github.com/spatie/browsershot/compare/5.4.2...5.5.0

  2. 5.4.22026-10-26 · 2026-10-26 11:48 UTC
    查看更新说明

    Fix loading ESM-only Puppeteer when Node cannot load it with require(), including processes using --no-experimental-require-module.

    Browsershot still prefers CommonJS loading, preserves global installations resolved through NODE_PATH, and retains named exports used by device emulation. PHP and Node requirements are unchanged.

    The regression generates a real PDF and verifies its rendered text and emulated iPhone user agent. CI also refreshes apt package lists before installing poppler-utils.

    What's Changed

    Full Changelog: https://github.com/spatie/browsershot/compare/5.4.1...5.4.2

  3. 5.4.12026-10-19 · 2026-10-19 07:50 UTC
    查看更新说明

    Fix temporary HTML and options cleanup when browser execution or process setup fails, and clean up intermediate screenshots after failed image manipulations.

    Browser integration tests now use deterministic local HTTP fixtures. CI adds Pest 5 on PHP 8.4–8.5 while retaining compatible test tooling for PHP 8.2–8.3; runtime PHP requirements are unchanged.

    What's Changed

    New Contributors

    Full Changelog: https://github.com/spatie/browsershot/compare/5.4.0...5.4.1

  4. 5.4.02026-05-26 · 2026-05-26 13:13 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/spatie/browsershot/compare/5.3.0...5.4.0

  5. 5.3.02026-04-27 · 2026-04-27 08:01 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/spatie/browsershot/compare/5.2.3...5.3.0

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Latest Version MIT Licensed run-tests Total Downloads

    The package can convert a web page to an image or PDF. The conversion is done behind the scenes by Puppeteer which runs a headless version of Google Chrome.

    Here's a quick example:

    use Spatie\Browsershot\Browsershot;
    
    // an image will be saved
    Browsershot::url('https://example.com')->save($pathToImage);
    

    It will save a PDF if the path passed to the save method has a pdf extension.

    // a pdf will be saved
    Browsershot::url('https://example.com')->save('example.pdf');
    

    You can also use an arbitrary html input, simply replace the url method with html:

    Browsershot::html('<h1>Hello world!!</h1>')->save('example.pdf');
    

    Local file requests from html() are blocked by default, including absolute filesystem paths and JavaScript-generated file URLs. If your trusted HTML needs local assets, opt in with Browsershot::html($html)->allowLocalFileAccess()->save('example.pdf'). This permits access to any local file readable by Chrome; only enable it for trusted HTML. Browsershot is not a sandbox for untrusted content.

    If your HTML input is already in a file locally use the :

    Browsershot::htmlFromFilePath('/local/path/to/file.html')->save('example.pdf');
    

    Browsershot also can get the body of an html page after JavaScript has been executed:

    Browsershot::url('https://example.com')->bodyHtml(); // returns the html of the body
    

    If you wish to retrieve an array list with all of the requests that the page triggered you can do so:

    $requests = Browsershot::url('https://example.com')
        ->triggeredRequests();
    
    foreach ($requests as $request) {
        $url = $request['url']; //https://example.com/
    }
    

    To use Chrome's new headless mode pass the newHeadless method:

    Browsershot::url('https://example.com')->newHeadless()->save($pathToImage);
    

    Support us

    Learn how to create a package like this one, by watching our premium video course:

    Laravel Package training

    We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

    We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

    Documentation

    All documentation is available on our documentation site.

    Testing

    For running the testsuite, you'll need to have Puppeteer installed. Pleaser refer to the Browsershot requirements here. Usually npm -g i puppeteer will do the trick.

    Additionally, you'll need the pdftotext CLI which is part of the poppler-utils package. More info can be found in in the spatie/pdf-to-text readme. Usually brew install poppler-utils will suffice.

    Finally run the tests with:

    composer test
    

    Contributing

    Please see CONTRIBUTING for details.

    Security

    If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

    Alternatives

    If you're not able to install Node and Puppeteer, take a look at v2 of browsershot, which uses Chrome headless CLI to take a screenshot. v2 is not maintained anymore, but should work pretty well.

    If using headless Chrome does not work for you take a look at at v1 of this package which uses the abandoned PhantomJS binary.

    Credits

    License

    The MIT License (MIT). Please see License File for more information.