dfware/html2pdf
Composer 安装命令:
composer require dfware/html2pdf
包简介
Provides support for HTML to PDF and PHP to PDF conversion
README 文档
README
The preferred way to install this extension is through composer.
Either run
composer require dfware/html2pdf
or add
"dfware/html2pdf": "*"
to the require section of your composer.json.
Note: you'll have to install software for the actual HTML to PDF conversion separately, depending on the particular converter, you would like to use.
Usage
This extension provides support for HTML to PDF and PHP to PDF conversion. It allows composition of the PDF files from HTML and via rendering PHP templates.
Extension functionality is aggregated into \yii2tech\html2pdf\Manager application component.
Application configuration example:
<?php return [ 'components' => [ 'html2pdf' => [ 'class' => 'yii2tech\html2pdf\Manager', 'viewPath' => '@app/views/pdf', 'converter' => 'wkhtmltopdf', ], ], ... ];
For the simple conversion you can use \yii2tech\html2pdf\Manager::convert() and \yii2tech\html2pdf\Manager::convertFile() methods:
<?php $html = <<<HTML <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p>Simple Content</p> </body> </html> HTML; // create PDF file from HTML content : Yii::$app->html2pdf ->convert($html) ->saveAs('/path/to/output.pdf'); // convert HTML file to PDF file : Yii::$app->html2pdf ->convertFile('/path/to/source.html') ->saveAs('/path/to/output.pdf');
The actual conversion result determined by particular converter used.
You may use \yii2tech\html2pdf\Manager::$converter property for the converter setup.
Several built-in converters are provided:
- yii2tech\html2pdf\converters\Wkhtmltopdf - uses wkhtmltopdf utility for the conversion.
- yii2tech\html2pdf\converters\Dompdf - uses dompdf library for the conversion.
- yii2tech\html2pdf\converters\Mpdf - uses mpdf library for the conversion.
- yii2tech\html2pdf\converters\Tcpdf - uses TCPDF library for the conversion.
- yii2tech\html2pdf\converters\Callback - uses a custom PHP callback for the conversion.
Heads up! Most of the provided converters require additional software been installed, which is not provided by his extension by default. You'll have to install it manually, once you decide, which converter you will use. Please refer to the particular converter class for more details.
You may specify conversion options via second argument of the convert() or convertFile() method:
<?php Yii::$app->html2pdf ->convertFile('/path/to/source.html', ['pageSize' => 'A4']) ->saveAs('/path/to/output.pdf');
You may setup default conversion options at the \yii2tech\html2pdf\Manager level:
<?php return [ 'components' => [ 'html2pdf' => [ 'class' => 'yii2tech\html2pdf\Manager', 'viewPath' => '@app/pdf', 'converter' => [ 'class' => 'yii2tech\html2pdf\converters\Wkhtmltopdf', 'defaultOptions' => [ 'pageSize' => 'A4' ], ] ], ], ... ];
Note: the actual list of available conversion options depends on the particular converter to be used.
Template usage
You may create PDF files rendering PHP templates (view files), which composes HTML output.
Such files are processed as regular view files, allowing passing params and layout wrapping.
Method \yii2tech\html2pdf\Manager::render() used for this:
<?php Yii::$app->html2pdf ->render('invoice', ['user' => Yii::$app->user->identity]) ->saveAs('/path/to/output.pdf');
You may use a shared layout for the templates, which can be setup via \yii2tech\html2pdf\Manager::$layout.
During each rendering view is working in context of \yii2tech\html2pdf\Template object, which can be used to adjust
layout or PDF conversion options inside view file:
<?php /* @var $this \yii\web\View */ /* @var $context \yii2tech\html2pdf\Template */ /* @var $user \app\models\User */ $context = $this->context; $context->layout = 'layouts/payment'; // use specific layout for this template // specify particular PDF conversion for this template: $context->pdfOptions = [ 'pageSize' => 'A4', // ... ]; ?> <h1>Invoice</h1> <p>For: <?= $user->name ?></p> ...
dfware/html2pdf 适用场景与选型建议
dfware/html2pdf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 94 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「template」 「pdf」 「convert」 「conversion」 「html2pdf」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dfware/html2pdf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dfware/html2pdf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dfware/html2pdf 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a simple decimal/roman number converter
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Convert xml to array
Convert doctrine dbal query result to dto
Simple ASCII output of array data
E2E Studios PHP Framework adaptation of leafsphp/blade package
统计信息
- 总下载量: 94
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-09-27