robregonm/yii2-pdf

Yii 2 PDF 响应格式化工具

安装次数: 45,370

依赖关系: 2

建议者: 0

安全: 0

星标: 46

关注者: 8

分支: 36

开放问题: 9

类型:yii2-extension

1.1.0 2019-08-02 16:47 UTC

This package is auto-updated.

Last update: 2024-08-29 04:12:41 UTC


README

使用 mPDF 库为 Yii2 提供的 PDF 格式化工具。

此扩展可以将 HTML 响应格式化为 PDF 文件(默认情况下,Yii2 包含 HTML、JSON 和 XML 格式化工具)。非常适合使用 HTML 视图/布局创建 PDF 格式的报告。

##安装

安装此扩展的最佳方式是通过 Composer

运行以下命令之一:

$ php composer.phar require robregonm/yii2-pdf "*"

或将其添加到您的 composer.json 文件的 require 部分:

"robregonm/yii2-pdf": "*"

使用方法

安装扩展后,修改您的应用程序配置以包含以下内容:

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

祝您使用愉快!

Flattr this git repo