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

这是一个关于如何使用Yii2框架的htmlconverter扩展将HTML转换为PDF文档或图片的指南。该扩展依赖于wkhtmltopdf工具,并通过Composer安装。用户可以通过配置组件轻松设置转换选项,例如页面大小、头部和脚部等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值