pdfshift/pdfshift-php

此包已被放弃,不再维护。未建议替代包。

使用 PDFShift.io API 将 HTML 文档转换为 PDF。

v1.0.9 2018-12-10 09:58 UTC

This package is not auto-updated.

Last update: 2020-12-22 17:54:11 UTC


README

⚠️ 由于 PDFShift 的 API 实现简单且易于使用,我们意识到只需使用自定义库来包装网络库是不必要的。因此,我们决定关闭此包,并建议您使用 cURL 等网络库与 PDFShift 进行通信。

PDFShift PHP 包

此 PHP 包提供了一种简化的方式来与 PDFShift API 进行交互。

文档

请参阅 PDFShift 的完整文档

要求

PHP 5.4.0 及以上版本。

Composer

您可以通过 Composer 安装绑定。运行以下命令

composer require pdfshift/pdfshift-php

要使用绑定,请使用 Composer 的 自动加载

require_once('vendor/autoload.php');

手动安装

如果您不想使用 Composer,您可以下载 最新版本。然后,为了使用绑定,请包含 init.php 文件。

require_once('/path/to/pdfshift-php/init.php');

使用方法

此库需要配置您的 api_key,您在创建账户时接收。设置很简单,如下所示

\PDFShift\PDFShift::setApiKey('your_api_key');

基本示例

通过 URL

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');
PDFShift::convertTo('https://www.example.com', null, 'result.pdf');

通过内联 HTML 数据

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

$data = file_get_content('invoice.html');
PDFShift::convertTo(data, null, 'result.pdf');

自定义 CSS

从 URL 加载 CSS

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

$data = file_get_content('invoice.html');
PDFShift::convertTo(data, ['css' => 'https://www.example.com/public/css/print.css'], 'result.pdf');

从字符串加载 CSS

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

$data = file_get_content('invoice.html');
PDFShift::convertTo(data, ['css' => 'a {text-decoration: underline; color: blue}'], 'result.pdf');

自定义 HTTP 头

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->setHTTPHeaders(['X-Original-Header' => 'Awesome value']);
$pdfshift->addHTTPHeader('user-agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'); // Also works like this
$pdfshift->convert('https://httpbin.org/headers');
$pdfshift->save('result.pdf');

访问受保护页面

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->auth('user', 'passwd');
$pdfshift->convert('https://httpbin.org/basic-auth/user/passwd');
$pdfshift->save('result.pdf');

使用 Cookie

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->addCookie('session', '4cb496a8-a3eb-4a7e-a704-f993cb6a4dac');
$pdfshift->convert('https://httpbin.org/cookies');
$pdfshift->save('result.pdf');

添加水印(Oh hi Mark!)

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->watermark([
    'image' => 'https://pdfshift.io/static/img/logo.png',
    'offset_x' => 50,
    'offset_y' => '100px',
    'rotate' => 45
])
$pdfshift->convert('https://www.example.com');
$pdfshift->save('result.pdf');

自定义页眉(或页脚)

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->setFooter('<div>Page {{page}} of {{total}}</div>', '50px');
$pdfshift->convert('https://www.example.com');
$pdfshift->save('result.pdf');

保护生成的PDF文件

require_once('vendor/autoload.php');
use \PDFShift\PDFShift;

PDFShift::setApiKey('your_api_key');

// We use an instance of PDFShift instead of the ::convertTo to easily handle advanced configuration
$pdfshift = new PDFShift();
$pdfshift->protect([
    'userPassword' => 'user',
    'ownerPassword' => 'owner',
    'noPrint' => true
]);
$pdfshift->convert('https://www.example.com');
$pdfshift->save('result.pdf');

贡献

有关详细信息,请参阅CONTRIBUTING

许可协议

MIT许可协议(MIT)。有关更多信息,请参阅许可文件