yii2 html转换,GitHub - boundstate/yii2-htmlconverter

yii2-htmlconverter

Extension for the Yii2 framework that converts HTML to PDF or images using wkhtmltopdf.

Installation

This extensions relies on wkhtmltopdf. Installation insructions are provided on the [wkhtmltopdf website] wkhtmltopdf.

The preferred way to install this extension is through composer.

Either run

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

or add

"boundstate/yii2-htmlconverter": "*"

to the require section of your composer.json file.

Usage

Setup the components in your config:

'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',

],

]

],

Now you can format a response as a PDF:

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

Or format a response as an image:

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

You can also manually generate a PDF from HTML:

$html = $this->render('hello-word');

$header = $this->render('hello-world-header');

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

Or manually generate an image from HTML:

$html = $this->render('hello-word');

$pdf = Yii::$app->htmlToImage->convert($html);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值