sbooker/yii2-pdf

Yii 2 PDF 响应格式化工具

此软件包的官方仓库似乎已不存在,因此该软件包已被冻结。

安装次数: 1,170

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 30

开放问题: 0

类型:yii2-extension

1.0.4 2017-07-27 13:57 UTC

This package is auto-updated.

Last update: 2021-05-01 00:17:04 UTC


README

使用 mPDF 7.x 库为 Yii2 实现的 PDF 格式化工具。

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

##安装

安装此扩展的首选方法是通过 composer

运行以下命令之一:

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

"sbooker/yii2-pdf": ">=1.0.2"

将以下内容添加到您的 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

有用的网址

祝您使用愉快!

Flattr this git repo