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

composer require spatie/browsershot
npm -g i puppeteerREADME 未说明显存或 GPU 要求;需能运行 Node 与 Puppeteer(无头 Chrome),README 未列出具体 GPU 厂商或型号
用 Puppeteer 驱动无头 Chrome,把网页或 HTML 转成图片、PDF 或执行后的 HTML
用 PHP 把网页 HTML 转成图片、PDF 或字符串,底层调用无头浏览器渲染,适合生成网页截图和导出 PDF 报告。
核心能力
- 调用 Puppeteer 无头 Chrome 渲染网页截图或 PDF
- 支持 url、html、htmlFromFilePath 三种输入源
- 按 save 路径扩展名自动决定输出图片还是 PDF
- 可获取 JS 执行后的 body HTML 与页面触发的请求列表
- 默认拦截 html 输入的本地文件访问,需显式开启
容易踩的坑
- 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()orsetHtml(). 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
- Block local file access from supplied HTML by default in https://github.com/spatie/browsershot/pull/989
- Test PHP 8.6 with compatible Imagick in https://github.com/spatie/browsershot/pull/988
Full Changelog: https://github.com/spatie/browsershot/compare/5.4.2...5.5.0
- Local images, stylesheets, scripts and iframes previously loaded through
- 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
- Fall back to import() when Puppeteer can't be required by @MohannadElemary in https://github.com/spatie/browsershot/pull/986
- Update apt package lists before installing poppler-utils by @MohannadElemary in https://github.com/spatie/browsershot/pull/987
Full Changelog: https://github.com/spatie/browsershot/compare/5.4.1...5.4.2
- 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
- Fix typo in sysctl configuration instructions by @intrepidws in https://github.com/spatie/browsershot/pull/979
- Bump actions/checkout from 6 to 7 by @dependabot[bot] in https://github.com/spatie/browsershot/pull/980
- Remove duplicate packages from installation instructions by @stephensamra in https://github.com/spatie/browsershot/pull/981
- Fix temporary file cleanup when browser execution fails by @MohannadElemary in https://github.com/spatie/browsershot/pull/985
New Contributors
- @stephensamra made their first contribution in https://github.com/spatie/browsershot/pull/981
- @MohannadElemary made their first contribution in https://github.com/spatie/browsershot/pull/985
Full Changelog: https://github.com/spatie/browsershot/compare/5.4.0...5.4.1
- 5.4.02026-05-26 · 2026-05-26 13:13 UTC
查看更新说明
What's Changed
- feat: Add evaluateOnNewDocument support by @vinitkadam03 in https://github.com/spatie/browsershot/pull/978
New Contributors
- @vinitkadam03 made their first contribution in https://github.com/spatie/browsershot/pull/978
Full Changelog: https://github.com/spatie/browsershot/compare/5.3.0...5.4.0
- 5.3.02026-04-27 · 2026-04-27 08:01 UTC
查看更新说明
What's Changed
- Update requirements.md by @colinmackinlay in https://github.com/spatie/browsershot/pull/973
- Bump dependabot/fetch-metadata from 2.5.0 to 3.0.0 by @dependabot[bot] in https://github.com/spatie/browsershot/pull/974
- Bump dependabot/fetch-metadata from 3.0.0 to 3.1.0 by @dependabot[bot] in https://github.com/spatie/browsershot/pull/976
- feat: Use only request interception when needed by @kissifrot in https://github.com/spatie/browsershot/pull/975
New Contributors
- @kissifrot made their first contribution in https://github.com/spatie/browsershot/pull/975
Full Changelog: https://github.com/spatie/browsershot/compare/5.2.3...5.3.0
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
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:
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.
