boundstate/yii2-htmlconverter

支持将 HTML 转换为 PDF 或图片

安装次数: 9,010

依赖: 0

建议者: 0

安全: 0

星标: 9

关注者: 3

分支: 1

开放问题: 2

类型:yii2-extension

v0.0.1 2015-05-14 22:26 UTC

This package is auto-updated.

Last update: 2024-08-29 04:19:48 UTC


README

这是一个为 Yii2 框架提供的扩展,使用 wkhtmltopdf 将 HTML 转换为 PDF 或图片。

安装

此扩展依赖于 wkhtmltopdf。安装说明可在 [wkhtmltopdf 网站] wkhtmltopdf 上找到。

安装此扩展的首选方法是使用 composer

运行以下命令之一:

php composer.phar require --prefer-dist boundstate/yii2-htmlconverter "*"

"boundstate/yii2-htmlconverter": "*"

将其添加到您的 composer.json 文件的 require 部分中。

使用方法

在您的配置中设置组件

'htmlToPdf' => [
    'class' => 'boundstate\htmlconverter\HtmlToPdfConverter',
    'bin' => '/usr/bin/wkhtmltopdf',
    // global wkhtmltopdf command line options
    // (see http://wkhtmltopdf.org/usage/wkhtmltopdf.txt)
    'options' => [
        'print-media-type',
        'disable-smart-shrinking',
        'no-outline',
        'page-size' => 'letter',
        'load-error-handling' => 'ignore',
        'load-media-error-handling' => 'ignore'
    ],
],
'htmlToImage' => [
    'class' => 'boundstate\htmlconverter\HtmlToImageConverter',
    'bin' => '/usr/bin/wkhtmltoimage',
],
'response' => [
    'formatters' => [
        'pdf' => [
            'class' => 'boundstate\htmlconverter\PdfResponseFormatter',
            // Set a filename to download the response as an attachments (instead of displaying in browser)
            'filename' => 'attachment.pdf'
        ],
        'image' => [
            'class' => 'boundstate\htmlconverter\ImageResponseFormatter',
        ],
    ]
],

现在您可以以 PDF 格式格式化响应

Yii::$app->response->format = 'pdf';

或将响应格式化为图片

Yii::$app->response->format = 'image';

您还可以从 HTML 手动生成 PDF

$html = $this->render('hello-word');
$header = $this->render('hello-world-header');
$pdf = Yii::$app->htmlToPdf->convert($html, ['page-size' => 'A4', 'header-html' => $header]);

或从 HTML 手动生成图片

$html = $this->render('hello-word');
$pdf = Yii::$app->htmlToImage->convert($html);