vendon / yii-pdf
本包的最新版本(0.4.1)没有提供许可证信息。
PHP-PDF库(mPDF、HTML2PDF)的Yii包装器
0.4.1
2016-09-30 08:09 UTC
Requires
- mpdf/mpdf: ^6.1
- spipu/html2pdf: ^4.6
- yiisoft/yii: ^1.1
This package is not auto-updated.
Last update: 2024-09-28 20:14:28 UTC
README
这是一个小的Yii扩展,用于封装一些PHP库(目前是mPDF和HTML2PDF),以便将HTML转换为PDF
资源
- Yii框架
- mPDF - 是一个PHP类,用于从HTML生成具有Unicode/UTF-8和CJK支持的PDF文件
- HTML2PDF - 是一个PHP类,使用FPDF为PHP4版本,使用TCPDF为PHP5版本。它可以转换有效的HTML和xHTML到PDF
需求
官方文档和示例
安装
- 下载并解压缩扩展到目录
protected/extensions/yii-pdf
- 下载并解压缩库(mPDF和/或HTML2PDF)到目录
protected/vendors
中的自己目录,或在'params'
数组中设置新的'librarySourcePath'
参数值 - 数组
'defaultParams'
- 这是选定库构造函数的默认参数数组。如果您想更改默认参数,您可以在配置文件中设置它们(如下所示)。如果这样做,您必须保留数组项的顺序! - 在您的
protected/config/main.php
中,添加以下内容
通过composer安装
- 将以下行添加到您的
composer.json
文件中"borales/yii-pdf": "dev-master"
<?php //... 'components'=>array( //... 'ePdf' => array( 'class' => 'ext.yii-pdf.EYiiPdf', 'params' => array( 'mpdf' => array( 'librarySourcePath' => 'application.vendors.mpdf.*', 'constants' => array( '_MPDF_TEMP_PATH' => Yii::getPathOfAlias('application.runtime'), ), 'class'=>'mpdf', // the literal class filename to be loaded from the vendors folder. /*'defaultParams' => array( // More info: http://mpdf1.com/manual/index.php?tid=184 'mode' => '', // This parameter specifies the mode of the new document. 'format' => 'A4', // format A4, A5, ... 'default_font_size' => 0, // Sets the default document font size in points (pt) 'default_font' => '', // Sets the default font-family for the new document. 'mgl' => 15, // margin_left. Sets the page margins for the new document. 'mgr' => 15, // margin_right 'mgt' => 16, // margin_top 'mgb' => 16, // margin_bottom 'mgh' => 9, // margin_header 'mgf' => 9, // margin_footer 'orientation' => 'P', // landscape or portrait orientation )*/ ), 'HTML2PDF' => array( 'librarySourcePath' => 'application.vendors.html2pdf.*', 'classFile' => 'html2pdf.class.php', // For adding to Yii::$classMap /*'defaultParams' => array( // More info: http://wiki.spipu.net/doku.php?id=html2pdf:en:v4:accueil 'orientation' => 'P', // landscape or portrait orientation 'format' => 'A4', // format A4, A5, ... 'language' => 'en', // language: fr, en, it ... 'unicode' => true, // TRUE means clustering the input text IS unicode (default = true) 'encoding' => 'UTF-8', // charset encoding; Default is UTF-8 'marges' => array(5, 5, 5, 8), // margins by default, in order (left, top, right, bottom) )*/ ) ), ), //... ) //...
使用方法
<?php ... public function actionIndex() { # mPDF $mPDF1 = Yii::app()->ePdf->mpdf(); # You can easily override default constructor's params $mPDF1 = Yii::app()->ePdf->mpdf('', 'A5'); # render (full page) $mPDF1->WriteHTML($this->render('index', array(), true)); # Load a stylesheet $stylesheet = file_get_contents(Yii::getPathOfAlias('webroot.css') . '/main.css'); $mPDF1->WriteHTML($stylesheet, 1); # renderPartial (only 'view' of current controller) $mPDF1->WriteHTML($this->renderPartial('index', array(), true)); # Renders image $mPDF1->WriteHTML(CHtml::image(Yii::getPathOfAlias('webroot.css') . '/bg.gif' )); # Outputs ready PDF $mPDF1->Output(); //////////////////////////////////////////////////////////////////////////////////// # HTML2PDF has very similar syntax $html2pdf = Yii::app()->ePdf->HTML2PDF(); $html2pdf->WriteHTML($this->renderPartial('index', array(), true)); $html2pdf->Output(); //////////////////////////////////////////////////////////////////////////////////// # Example from HTML2PDF wiki: Send PDF by email $content_PDF = $html2pdf->Output('', EYiiPdf::OUTPUT_TO_STRING); require_once(dirname(__FILE__).'/pjmail/pjmail.class.php'); $mail = new PJmail(); $mail->setAllFrom('webmaster@my_site.net', "My personal site"); $mail->addrecipient('mail_user@my_site.net'); $mail->addsubject("Example sending PDF"); $mail->text = "This is an example of sending a PDF file"; $mail->addbinattachement("my_document.pdf", $content_PDF); $res = $mail->sendmail(); } ...