nuzkito/chrome-html-to-pdf

此包的最新版本(2.0.0)没有可用的许可信息。

使用 Google Chrome 将 HTML 转换为 PDF

2.0.0 2020-05-17 22:47 UTC

This package is auto-updated.

Last update: 2024-09-18 09:00:33 UTC


README

一个简单的包装器,用于在无头模式下使用 Google Chrome 将 HTML 转换为 PDF。

安装

composer require nuzkito/chrome-html-to-pdf

需要安装在 Linux 和 Mac 上的 Chrome 59,以及 Windows 上的 Chrome 60。

在基于 Debian 的 Linux 服务器上安装 Chrome

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt-get install -f

在其他情况下,请从 https://www.google.es/chrome/browser/desktop/index.html 下载

使用

<?php

use Nuzkito\ChromePdf\ChromePdf;

// By default it will search for Chrome in the default path in each OS,
$pdf = new ChromePdf();
// but you need it, you can specify the route to the binary.
$pdf = new ChromePdf('/path/to/google-chrome');

// Route when PDF will be saved.
$pdf->output('/path/to/result.pdf');

// You can generate a PDF from a url
$pdf->generateFromUrl('http://google.es');
// ... from a HTML file
$pdf->generateFromFile('/path/to/html/document.html');
// ... or pass a string containing the HTML.
$pdf->generateFromHtml('<h1>Hello world!</h1>');

测试

要执行自动化测试,您需要安装 pdftotext

sudo apt-get install poppler-utils

然后,执行 vendor/bin/phpunit

配置

如果您需要指定 Chrome 的路径,可以创建一个包含以下内容的 config.php 文件

<?php

$_ENV['chrome-executable'] = '/path/to/google-chrome';