perochak / yii2-pdf
Yii 2 PDF 响应格式化工具
1.1.3
2021-07-13 11:55 UTC
Requires
- mpdf/mpdf: 8.0.4
- yiisoft/yii2: *
- yiisoft/yii2-composer: *
This package is not auto-updated.
Last update: 2024-09-18 22:26:02 UTC
README
使用 mPDF 库的 Yii2 PDF 格式化工具。
该扩展将 "格式化" HTML 响应转换为 PDF 文件(默认情况下,Yii2 包含 HTML、JSON 和 XML 格式化工具)。非常适合使用 HTML 视图/布局创建 PDF 格式的报告。
##安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
$ php composer.phar require perochak/yii2-pdf "*"
或者
"robregonm/yii2-pdf": "*"
将其添加到您的 composer.json
文件的 require 部分中。
用法
扩展安装完成后,修改您的应用程序配置以包括
return [ 'components' => [ ... 'response' => [ 'formatters' => [ 'pdf' => [ 'class' => 'robregonm\pdf\PdfResponseFormatter', 'mode' => '', // Optional 'format' => 'A4', // Optional but recommended. http://mpdf1.com/manual/index.php?tid=184 'defaultFontSize' => 0, // Optional 'defaultFont' => '', // Optional 'marginLeft' => 15, // Optional 'marginRight' => 15, // Optional 'marginTop' => 16, // Optional 'marginBottom' => 16, // Optional 'marginHeader' => 9, // Optional 'marginFooter' => 9, // Optional 'orientation' => 'Landscape', // optional. This value will be ignored if format is a string value. 'options' => [ // mPDF Variables // 'fontdata' => [ // ... some fonts. http://mpdf1.com/manual/index.php?tid=454 // ] ] ], ] ], ... ], ];
在控制器中
class MyController extends Controller { public function actionPdf(){ Yii::$app->response->format = 'pdf'; // Rotate the page Yii::$container->set(Yii::$app->response->formatters['pdf']['class'], [ 'format' => [216, 356], // Legal page size in mm 'orientation' => 'Landscape', // This value will be used when 'format' is an array only. Skipped when 'format' is empty or is a string 'beforeRender' => function($mpdf, $data) {}, ]); $this->layout = '//print'; return $this->render('myview', []); } }
许可证
Yii2-Pdf 使用 BSD-3 许可证发布。有关详细信息,请参阅附带的 LICENSE.md
文件。
有用的 URL
祝您使用愉快!