frengky/php-wkhtmltox

PHP的简单wkhtmltox包装器

v1.0.0 2020-04-26 03:54 UTC

This package is auto-updated.

Last update: 2024-09-26 13:59:25 UTC


README

Wkhtmltopdf 是将HTML渲染成PDF或图片的最佳选择。本包为 wkhtmltopdfwkhtmltoimage 提供了一个干净且易于使用的包装器。本包直接与wkhtmltopdf的进程(stdin和stdout)工作,不创建任何临时文件。

要求

为您的操作系统安装 Wkhtmltopdf 二进制文件,确保 wkhtmltopdfwkhtmltoimage 可在您的PATH中执行。

安装

通过 Composer 安装该包

composer require frengky/php-wkhtmltox

用法

将HTML字符串转换为PDF文件

<?php
use Frengky\WkHtml\PDF;

$htmlString = 'Hello, <strong>World</strong>!'
$outputPath = PDF::fromHtml($contents)
           ->set('--page-size',  'A4') // the args to wkhtmltopdf
           ->set('--orientation',  'Portrait')
           ->saveAs('storage/files/test-success.pdf');

if ($result) {
   echo "Generated pdf output file path: " . $outputPath;
}

您也可以与流一起工作。请参阅PSR-7 StreamInterface 并查看 guzzle/psr7

$htmlSource = Psr7\stream_for('Hello, <strong>World</strong>!');
$retval = PDF::fromHtml($htmlSource)
           ->set('--page-size',  'A4') // the args to wkhtmltopdf
           ->set('--orientation',  'Portrait')
           ->render(function(StreamInterface $output) {
				// Do something with the stream
				file_put_contents('output.pdf', $output->getContents());
			});

如果您处理的是 大型 PDF文件输出,这是推荐的方式。