Yii-PDF Extension
Small Yii extension, that wraps a few PHP libraries (mPDF and HTML2PDF so far) to convert HTML to PDF
protected/extensions/yii-pdfprotected/vendors or set new value for 'librarySourcePath' parameter in 'params' array'defaultParams' - this is an array of constructor's default params of selected library.
If you want to change default params - you can set them in config file (like shown below).
If you do so - you must keep the order of array items!protected/config/main.php, add the following://... '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) )*/ ) ), ), //... ) //...
... 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(); } ...
Version 0.3.2 (2013-04-05)
Version 0.3 (2012-07-07)
Version 0.2a (2012-02-05)
Version 0.2 (2012-01-24)
'defaultParams' is not required anymore (will be used constructor's default params of selected library)Output() method (detailed info located in extension's class)class EYiiPdf extends CApplicationComponent { ... const OUTPUT_TO_BROWSER = "I"; const OUTPUT_TO_DOWNLOAD = "D"; const OUTPUT_TO_FILE = "F"; const OUTPUT_TO_STRING = "S"; ... } #Example (make sure that target directory is writable) $html2pdf->Output('/path/to/file.pdf', EYiiPdf::OUTPUT_TO_FILE);
Total 20 comments
Hi Borales,
Thanks for your answer. What do you mean by "applied correctly in the given scope" ?
I tried doing render instead of renderPartial, but this gives me a blank (empty) PDF ... any ideas ?
Jonathan
staticblue, are you sure your CSS rules are applied correctly in the given scope? Try to render a whole page (renderPartial -> render).
It definitely finds my screen.css, but doesn't seem to apply any of the styles ?
staticblue, can you show your code with CSS part here?
Thanks for this awesome extension !
For some reason, with mPDF, I cannot get my CSS to be used in the PDF. I don't get any "file not found" error so I am guessing my path is correct, but still the CSS is not used. Any suggestions ?
Thanks, Jonathan
Just in case anyone was wondering, I was able to get the landscape orientation to work by using this method of declaring the class
Just exactly what I need for.. Thank you!
I have some report to export to pdf. If the report only need 1 page, it can render the pdf. but if my data make the report more then 1 page. i get this error.. Please help me to fix this...
If i use Html2PDF how to include the css in it..
i have edit my EYiiPdf.php like this:
but i still get error "You must set parameters first "
@shibly27 Try http://www.minte9.com/kb/force-page-breaks-while-converting-html-to-pdf-programming-css-i16
How could I give a page break?
zitter, thanks. I will fix this asap.
I've changed
to
to make it work with parameters passed on constructor.
Thanks for your extension. Can I use it to convert images/documents(doc,ppt,etc) to pdf file ?
Thanks
borales, thanks for your reply. Tried out your suggestion but got error: "Fatal error: Cannot pass parameter 2 by reference" ie. _setPageSize('A4', 'L')
$pdf = Yii::app()->ePdf->mpdf(); $pdf->_setPageSize('A4', 'L');MrLe, try to do like this:
I've tried setting orientation to 'L', but the generated pdf file still remains portrait. The only way I could do it was to manually set the variables as follows. Anyone manage to get landscape orientation working or is this a known bug in mpdf.php?
Code example produces landscape 11.69 x 8.26 inch:
$pdf = Yii::app()->ePdf->mpdf('', 'A4', '','','','','','','','','P'); $pdf->writeHTMLfooter=false; $pdf->writeHTMLheader=false; $pdf->DeflMargin=5; $pdf->DefrMargin=5; $pdf->tMargin=5; $pdf->bMargin=5; $pdf->w=297; //manually set width $pdf->h=209.8; //manually set heightThanks for the great extension. And thanks also for letting me know about mPDF and HTML2PDF — I thought the world was spinning around TCPDF and Zend_Pdf :)
Just FYI, I tested it on a not-so-basic view (with relative divs, photos, and UTF8) on Yii 1.1.8 and it works like a charm. I tried it also on 1.1.9.
Well I may have now to test with HTML2PDF and TCPDF before maybe digging into mPDF options, in order to compare the three as per size and performance.
Edit: Maybe someone would be interested on a display-or-generate snippet:
Replace
With
Do the same for the HTML2PDF() function just bellow the mpdf() function.
Thanks for this cool extension !
Leave a comment
Please login to leave your comment.