yasinstc/phpchromepdf

A slim PHP wrapper around google-chrome to convert url to pdf or to take screenshots, easy to use and clean OOP interface

0.0.1 2024-07-10 10:47 UTC

This package is not auto-updated.

Last update: 2024-09-19 09:58:25 UTC


README

PHP ChromeToPdf 提供了一个简单且清晰的接口,方便使用 Google Chrome 创建 PDF 和图片。**Google Chrome 必须安装并运行在您的系统上。**请参阅下文部分以获取详细信息。

历史

每次我需要将 URL 转换为 PDF 或对某个 URL 进行截图时,我都必须安装几个库。我知道有很多很好的库可用,如 phantomJs、wkhtmltopdf 以及其他一些库...但是总会有渲染问题,比如一些支持 bootstrap,一些不支持,有时存在 html5 问题 等等。这都很令人烦恼,所以我决定围绕 Chrome 制作一个小型包装器,使用 Chrome 结果我实现了以下结果。

  • 不再有 CSS 问题(bootstrap、css3)
  • 不再有 html5 问题
  • 不再需要使用 xvfb

实际上,转换过程非常简单

Laravel 安装

此包支持 Laravel。您可以在 Laravel 应用程序中按以下方式安装它

composer require yasinstc/phpchromepdf

完成。

Simple PHP App 安装

通过 composer 安装此包

composer require yasinstc/phpchromepdf

请确保,在您的代码库中包含了 composer 自动加载器

示例

在 examples 文件夹中也提供了几个示例

将 URL 转换为 PDF(以较大浏览器访问站点)

use yasinstc\phpChrome\Chrome;


$chrome=new Chrome('https://youtube.com','/usr/bin/google-chrome');
$chrome->setOutputDirectory(__DIR__);
//not necessary to set window size
$chrome->setWindowSize($width=1477,$height=768);
print "Pdf successfully generated :".$chrome->getPdf().PHP_EOL;

将 URL 转换为 PDF(以移动浏览器访问站点)

use yasinstc\phpChrome\Chrome;

$chrome=new Chrome('https://facebook.com','/usr/bin/google-chrome');
$chrome->setOutputDirectory(__DIR__);
$chrome->useMobileScreen();
//not necessary to set window size
$chrome->setWindowSize($width=768,$height=768);
print "Pdf successfully generated :".$chrome->getPdf().PHP_EOL;

对 URL 进行截图(以较大浏览器访问站点)

use yasinstc\phpChrome\Chrome;


$chrome=new Chrome('https://facebook.com','/usr/bin/google-chrome');
$chrome->setOutputDirectory(__DIR__);
//not necessary to set window size
$chrome->setWindowSize($width=1366,$height=1024);
print "Image successfully generated :".$chrome->getScreenShot().PHP_EOL;

对 URL 进行截图(以移动浏览器访问站点)

use yasinstc\phpChrome\Chrome;


$chrome=new Chrome('https://facebook.com','/usr/bin/google-chrome');
$chrome->setOutputDirectory(__DIR__);
$chrome->useMobileScreen();

//not necessary to set window size
$chrome->setWindowSize($width=768,$height=768);
print "Image successfully generated :".$chrome->getScreenShot().PHP_EOL;

对 HTML 文件进行截图

include '../vendor/autoload.php';

use yasinstc\phpChrome\Chrome;

$chrome=new Chrome(null,'/usr/bin/google-chrome');
$chrome->useHtmlFile(__DIR__.'/index.html');
print "Image successfully generated :".$chrome->getScreenShot().PHP_EOL;

将 HTML 文件转换为 PDF

include '../vendor/autoload.php';

use yasinstc\phpChrome\Chrome;

$chrome=new Chrome(null,'/usr/bin/google-chrome');
$chrome->useHtmlFile(__DIR__.'/index.html');
print "Pdf successfully generated :".$chrome->getPdf().PHP_EOL;

将 HTML 代码转换为 PDF / 截图

include '../vendor/autoload.php';

use yasinstc\phpChrome\Chrome;

$chrome=new Chrome(null,'/usr/bin/google-chrome');
$chrome->useHtml("<h2>I am test html</h2>");
print "Pdf successfully generated :".$chrome->getPdf().PHP_EOL;
print "screenShot successfully generated :".$chrome->getScreenShot().PHP_EOL;

将 HTML 代码转换为 PDF / 截图并保存到指定位置

include '../vendor/autoload.php';

use yasinstc\phpChrome\Chrome;

$chrome=new Chrome(null,'/usr/bin/google-chrome');
$chrome->useHtml("<h2>I am test html</h2>");
print "Pdf successfully generated :".$chrome->getPdf("/tmp/mypdf.pdf").PHP_EOL;
print "screenShot successfully generated :".$chrome->getScreenShot("/tmp/hello/test.jpg").PHP_EOL;

设置选项

google-chrome 命令行接受不同类型的选项:您可以访问 https://peter.sh/experiments/chromium-command-line-switches/ 以获取选项的完整列表。

包装方法

  • constructor:接受第一个参数作为要访问的 URL(用于 PDF/screenshot),您可以将此参数传递为 null,然后稍后使用 setUrl。第二个参数是系统上安装的 google-chrome 的二进制路径。如果没有提供二进制路径,则使用默认位置 /usr/bin/google-chrome,但您仍然可以使用 setBinaryPath 方法提供二进制路径。构造函数还添加了一些默认参数,例如 headless, disable-gpu,这对于 google-chrome 在命令行中工作至关重要。
  • setBinaryPath 接受二进制路径并将其设置为您
  • setArguments 用于设置 google-chrome 的选项,它接受以特定格式提供的选项数组
[
   $argument1=>$value1,
   $argument2=>$value2,
]

如果您的参数没有值,例如 --headless,则可以传递空值,例如 [--headless=>'']

  • setArgument 用于设置 google-chrome 的选项,它接受两个参数 $argument, $value。如果您的参数没有值,例如 --headless,则可以传递空值,例如 setArgument('--headless','')

  • setChromeDirectory 是 google-chrome 将保存您的配置文件的目录。这不是必需的,因为 google-chrome 默认使用某个目录,但在需要时,您可以使用此方法更改该目录

  • setUrl 用于设置要转换为 PDF 或截图的 URL

  • useHtmlFile 使用文件而不是URL来转换成PDF或截图

  • useHtml 使用HTML代码而不是URL来转换成PDF或截图

  • setOutputDirectory 设置输出目录(截图和PDF保存的位置),默认目录是操作系统的临时目录

  • getPdf 接收可选的路径参数来保存PDF文件,如果没有提供,它将在输出目录或操作系统的临时目录中保存,根据您是否正确设置了输出目录,请检查setOutputDirectory选项,它将转换您提供的URL到PDF并返回新保存的PDF的位置

  • getScreenShot 接收可选的路径参数来保存PDF文件,如果没有提供,它将在输出目录或操作系统的临时目录中保存,根据您是否正确设置了输出目录,请检查setOutputDirectory选项,它将截取您提供的URL的屏幕截图并返回新保存的图片的位置

  • setWindowSize 您可以使用此方法设置Chrome窗口的大小,它接受两个参数$width和$height

  • useMobileScreen 请求Chrome以移动浏览器的形式访问网站

  • getArguments 返回所有设置的参数

还有其他一些getter可用,以防您需要getUrl , getBinaryPath , getOutputDirectory

Google-Chrome的安装(Linux/mac)

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'

sudo apt-get install libxss1 libappindicator1 libindicator7 libosmesa6

sudo apt-get update
sudo apt-get install -y google-chrome-stable

sudo ln -s /usr/lib/x86_64-linux-gnu/libOSMesa.so.6 /opt/google/chrome/libosmesa.so

然后尝试从shell运行google-chrome来验证安装

Google-Chrome的安装(Windows)

Just Install updated version of chrome(after V 61.*)
that-s it

然后尝试从命令提示符运行C:\Program Files (x86)\Google\Chrome\Application>chrome.exe --headless来验证安装

请注意,在您的情况下,Chrome目录的路径可能不同

许可协议

PhpChromeToPdf 是开源软件,根据MIT许可协议授权。

贡献

感谢所有贡献者,请检查贡献指南