its404/php-puppeteer
Composer 安装命令:
composer require its404/php-puppeteer
包简介
PHP Wrapper for Google Chrome Puppeteer
关键字:
README 文档
README
This project provides the ability to generate PDF with Puppeteer in PHP
- PHP 5 Compatible
- No Dependent Package
- Easy to Use
Dependencies
The library is running based on Nodejs(7.6 above) and Puppeteer It is tested under NodeJS 8.
Installation on CentOS 7:
sudo curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum install -y nodejs pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc
sudo npm install --global --unsafe-perm puppeteer
sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium
Installation on Ubuntu 16.04:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
sudo npm install --global --unsafe-perm puppeteer
sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium
Installation
Install its404/php-puppeteer using Composer.
composer require its404/php-puppeteer
Usage
Parameters
The library has set some default parameter values to support basic features, you can set the customized parameters to override the default ones, it supports all parameters of Puppeteer API
Sample parameter array:
$config = [
'html' => "<h1>Hello World</h1>",
'pdf' => [
'path' => '/tmp/test.pdf',
'margin' => [
'top' => '10mm',
'right' => '10mm',
'bottom' => '10mm',
'left' => '10mm',
]
]
];
Note:
- If both
$config['pdf']['html']and $config['pdf']['url'] are set,htmlwill be picked up.- If
$config['pdf']['path']is not set, will return pdf data
Import
You need to import this namespace at the top of your PHP class
use Its404\PhpPuppeteer\Browser;
PDF by URL
You can generate PDF by URL through pdf function:
public function actionTest1()
{
$config = [
"url" => "https://www.highcharts.com/demo/line-basic",
];
$browser = new Browser();
$browser->isDebug = true;
$content = $browser->pdf($config);
header("Content-type:application/pdf");
echo $content;
}
You can set any parameter in $config to override the default values
PDF by HTML
You can generate PDF by html code through pdf function:
public function actionTest2()
{
$config = [
"html" => "<h1>Hello Wolrd</h1>"
];
$browser = new Browser();
$content = $browser->pdf($config);
header("Content-type:application/pdf");
echo $content;
}
Debug The class
Browserhas one parameter$isDebug, it would be debug mode if it'strue, detailed error messages would be returned from
PDF to specific path
You can generate PDF to a specific path by setting $config['pdf']['path']
public function actionTest3()
{
$config = [
"html" => "<h1>Hello Wolrd</h1>",
'pdf' => [
'path' => '/tmp/test.pdf',
]
];
$browser = new Browser();
$browser->isDebug = true;
$result = $browser->pdf($params);
if (isset($result['returnVal'])&& $result['returnVal'] == 0) {
echo "PDF generated successfully";
} else {
echo "Failed to generate PDF";
var_dump($result['output']);
}
}
Start a new page on PDF
You can add following html code in the html where you'd like to start a new page for PDF.
<div style="page-break-after:always;"></div>
More Examples?
You can find more examples from here
its404/php-puppeteer 适用场景与选型建议
its404/php-puppeteer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 322.38k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2018 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「pdf」 「screenshot」 「puppeteer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 its404/php-puppeteer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 its404/php-puppeteer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 its404/php-puppeteer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Instrument headless chrome/chromium instances from php5.6
The official SDK for Capture.
Provides TCPDF integration for Symfony
Alfabank REST API integration
Finds individual items inside an image and gets their size/coordinates.
A adapter for the mPDF print library
统计信息
- 总下载量: 322.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 24
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-05