[EXTENSION] PDF generation with TCPDF

Feel free to discuss this extension here.

http://www.yiiframew…extension/tcpdf

Very nice work indeed! Are you converting your prado components into Yii's?

Quote

Are you converting your prado components into Yii's?

Yes, I'm trying to (and I learn Yii and more PHP OOP in the process :slight_smile: ). I'm not able to release everything, since several are commercial closed source work (and most of those not generic nor useful outside my projects), but the ones I can, I will, to give back to the project and the FLOSS community.

I'm trying to do add some html to a pdf by doing the following in my controller class:

$html = $this->renderPartial('_details', array('model' => $model), true);

$pdf->writeHTML($html, true, false, false, false, '');

but I get the following error:

array_push() [<a href='function.array-push'>function.array-push</a>]: First argument should be an array

it seems like the writeHTML doesn't accept the html code, which is returned by the renderPartial function.

could anyone help me?

More working examples would be useful as its not working now.

i get error with same images




$pdf->Image($imagen, 1, 1.5, 3, 3, '', '', '', true, 600, '',false,false,1);



this not work http://i50.tinypic.com/2cc1lt.gif

but this work fine http://i48.tinypic.com/2qi2ooo.jpg

the extension is not a problem, the reverse also happens

help

[edit]

the problem is the size of image

i resize to 95x95 and work

but 100x100 not work

help me!!!

i do a test

download source from http://sourceforge.net/projects/tcpdf/files/

put tcpdf in host an run example 9

http://localhost/tcpdf/examples/example_009.php

work fine

after this

  1. create a new webapp with yicc webapp (test)

  2. install extension tcpdf

  3. modify site/index.php putting code of example 9

    change




  require_once('../config/lang/eng.php');

  require_once('../tcpdf.php');



for




  Yii::import('application.extensions.*');

  require_once('tcpdf/config/lang/eng.php');

  require_once('tcpdf/tcpdf.php');



put image "image_demo.jpg" in /images

change




$pdf->Image('../images/image_demo.jpg', 50, 50, 100, 150, '', 'http://www.tcpdf.org', '', true, 150);



for




$pdf->Image('images/image_demo.jpg', 50, 50, 100, 150, '', 'http://www.tcpdf.org', '', true, 150);true, 150);



and… error

now pleads

anyone can tell me if I do something wrong, or is a bug in the framework?

someone with time can do the same test that I do

thanks

hi

me again

i probe http://www.fpdf.org/ tutorial 2

and get same error with large images

how debug this? is a bug?

[edit]

the error is "an eror has ocurred while trying to use this document"

only happend with large image

try with browsers :mozilla and chrome

OS:ubuntu and winXP

the problem is solved

i get this error because i use this classes in views

but the correct use is in controller <_<

sorry

i newbie

Do you have some example for the newbies like me???

mmmmm, yes





	public function actionImprime($idEgreso)

	{




                       $egreso=Egreso::model()->with('dom_estado')->findByPk($idEgreso);


                       $criteria= new CDbCriteria();

                       $criteria->condition='idEgreso='.$idEgreso;

                       $criteria->order='idEgresoDetalle';

                       $detalles=EgresoDetalles::model()->findAll($criteria);

                


                $ancho=21;

                $alto=29.7;

                $format=array($ancho,$alto);




                $pdf = Yii::createComponent('application.extensions.tcpdf.tcpdf.MyTcPDF');

                $pdf->SetCreator(PDF_CREATOR);

                $pdf->SetAuthor(Yii::app()->name);

                $pdf->SetTitle("Remito Egreso");




$programa=$egreso->programa->nombre;

$efector=$egreso->efector->nombre;


$medico=($egreso->medico)?$egreso->medico->profesional->persona->nombre.', '.$egreso->medico->profesional->persona->apellido:'';

$paciente=($egreso->padron)?$egreso->padron->persona->nombre.' '.$egreso->padron->persona->apellido:'';


$estado=Varios::descripcionDominio('DR_ESTADO_EGRESO',$egreso->estado);


$anio=substr($egreso->fecha,6,4);

$titulo='REMITO DE '.strtoupper($programa).' ('.$egreso->nroRemito.'/'.$anio.')';







$cabecera = <<<EOD


<br />

<br />

<b>Efector:</b> <font size="18">$efector </font>

<br />

<b>Programa:</b> $programa 

<br />


<b>Remito:</b> $egreso->nroRemito/$anio

<br />

<b>Estado:</b>$estado

<br />


<b>Fecha:</b>$egreso->fecha

<br />

<b>Médico Solicitante:</b>$medico

<br />

<b>Paciente:</b>$paciente

<br />


EOD;




                $pdf->SetHeaderData('logocpsp.jpg', 0,$titulo,$cabecera);




                // set header and footer fonts

                $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));

                $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));


                // set default monospaced font

                $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);


                //set margins

                $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

                $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);

                $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

                $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

                $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);




                /*Primer página*/

                $pdf->AddPage();


                


$tbl = <<<EOD

    <font size="8">

    <table border="1" >

    <thead>

     <tr style="background-color:#666666;">

      <td width="35" align="center"><b>Reng</b></td>

      <td width="45" align="center"><b>Cant</b></td>

      <td width="280" align="center"><b>Medicamento</b></td>

      <!--<td width="100" align="center"> <b>Presentación</b></td>-->

      <td width="70" align="center"><b>Vto.</b></td>

      <td width="70" align="center"><b>Lote</b></td>

     </tr>

    </thead>

EOD;


foreach ($detalles as $i => $renglon) {

                $medicamento=$renglon->producto->nombre;

                $presentacion=$renglon->producto->presentacion;

                $fechaVto=$renglon->lote->fecha_vto;

                $lote=$renglon->lote->lote;

                $nroRenglon=$i+1;

                $tbl .= <<<EOD

                 <tr>

                  <td width="35" align="center">$nroRenglon</td>

                  <td width="45" align="center">$renglon->cantidad</td>

                  <td width="280">$medicamento</td>

                  <!--<td width="100"> $presentacion</td>-->

                  <td width="70" align="center">$fechaVto</td>

                  <td width="70" align="center">$lote</td>

                 </tr>

EOD;


}


$tbl .= <<<EOD

</table>

</font>

EOD;


$pdf->writeHTML($tbl, true, false, false, false, '');





$tbl = <<<EOD


<font size="8">

<table border="1">

<thead>

 <tr style="background-color:#666666;">

  <td width="125" align="center"><b>Autoriza</b></td>

  <td width="125" align="center"><b>Prepara</b></td>

  <td width="125" align="center"><b>Resp Téc.</b></td>

  <td width="125" align="center"> <b>Recibe</b></td>

 </tr>

</thead>

<tr>

  <td  width="125" align="center" height="50" > &nbsp;</td>

  <td  width="125" align="center" height="50" > &nbsp;</td>

  <td  width="125" align="center" height="50" > &nbsp;</td>

  <td  width="125" align="center" height="50" > &nbsp;</td>

 </tr>

 <tr align="center">

  <td width="125"  > Firma</td>

  <td width="125"  > Firma</td>

  <td width="125"  > Firma</td>

  <td width="125"  > Firma</td>

 </tr>

<tr>

  <td  width="125" height="50" > &nbsp;</td>

  <td  width="125" height="50" > &nbsp;</td>

  <td  width="125" height="50" > &nbsp;</td>

  <td  width="125" height="50" > &nbsp;</td>

 </tr>


<tr align="center">

  <td width="125">Aclaración</td>

  <td width="125">Aclaración</td>

  <td width="125">Aclaración</td>

  <td width="125">Aclaración</td>

 </tr>

 <tr>

  <td width="125" >Fecha:   </td>

  <td width="125" >Fecha:   </td>

  <td width="125" >Fecha:   </td>

  <td width="125" >Fecha:   </td>

 </tr>

</table>

</font>


EOD;


$pdf->writeHTML($tbl, true, false, false, false, '');


if (!empty($paciente)) {

        $tbl = <<<EOD

                <font size="8">

                <table border="1">

                <thead>

                 <tr style="background-color:#666666;">

                  <td  width="500" align="center"><b>Paciente</b></td>

                 </tr>

                </thead>

                <tr>

                  <td  width="500"  height="40">&nbsp;</td>

                </tr>


                <tr>

                  <td width="500" align="center">(Firma, Aclaración y Fecha)</td>

                </tr>

                </table>

                </font>


                


EOD;


    $pdf->writeHTML($tbl, true, false, false, false, '');


} 







if (!empty($egreso->obs)) {

        $tbl = <<<EOD

                <font size="8">

                <table border="1">

                <thead>

                 <tr style="background-color:#666666;">

                  <td  width="500" align="center"><b>Observaciones</b></td>

                 </tr>

                </thead>


                <tr>

                  <td  width="500" >$egreso->obs</td>

                </tr>

                </table>

                </font>

                

EOD;


    $pdf->writeHTML($tbl, true, false, false, false, '');


}


if (!empty ($egreso->fechaLimiteEnvio))

        $pdf->writeHTML('<b> Fecha limite de envío:'.$egreso->fechaLimiteEnvio.'</b>', true, false, false, false, '');


$pdf->writeHTML('<b> Fecha Salida Droguería: <br> Transporte:</b>', true, false, false, false, '');





//pongo una linea y leyenda

$tbl='<hr> <div align="center"><b>DEVOLVER CONFORMADO A ORGANISMO CENTRAL </b> <br> <b>DENTRO DE LOS 10 (DIEZ) DÍAS CORRIDOS</b> </div>';

$pdf->writeHTML($tbl, true, false, false, false, '');


                // reset pointer to the last page

                $pdf->lastPage();

                $pdf->Output($egreso->programa->nombre.'    '.$egreso->nroRemito.'\/'.substr($egreso->fecha,6,4). '.pdf', "D");

                


	}




What is MyTcPDF?

How/Where do you call your action ‘actionImprime’?

I have a ‘Preview’ button, and my idea is to generate the PDF when I click it. I am really lost with this stuff

my custom TcPdf

from the view …

try with something like this




CHtml::button('preview', array('submit' => array('imprime','id'=>$data->id)));



One last question, How I create a custon TcPdf??? copy/paste and change the name of the file?

I use this code

protected/view/site/index




<?php


Yii::import('application.extensions.tcpdf.*');

require_once('config/lang/eng.php');

require_once('tcpdf.php');;


// create new PDF document

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);


// set document information

$pdf->SetCreator(PDF_CREATOR);

$pdf->SetAuthor('Nicola Asuni');

$pdf->SetTitle('TCPDF Example 002');

$pdf->SetSubject('TCPDF Tutorial');

$pdf->SetKeywords('TCPDF, PDF, example, test, guide');


// remove default header/footer

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);


// set default monospaced font

$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);


//set margins

$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);


//set auto page breaks

$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);


//set image scale factor

$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);


//set some language-dependent strings

$pdf->setLanguageArray($l);


// ---------------------------------------------------------


// set font

$pdf->SetFont('times', 'BI', 20);


// add a page

$pdf->AddPage();


// set some text to print

$txt = <<<EOD

TCPDF Example 002


Default page header and footer are disabled using setPrintHeader() and setPrintFooter() methods.

EOD;


// print a block of text using Write()

$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);


// ---------------------------------------------------------


//Close and output PDF document

$pdf->Output('example_002.pdf', 'I');


?>



and result : Failed To Load PDF Document, how to resolve…?? Please help me… :-[

I have code this


<?php

$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8');

$pdf->SetCreator(PDF_CREATOR);

$pdf->SetAuthor("Nicola Asuni");

$pdf->SetTitle("TCPDF Example 002");

$pdf->SetSubject("TCPDF Tutorial");

$pdf->SetKeywords("TCPDF, PDF, example, test, guide");

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);

$pdf->AliasNbPages();

$pdf->AddPage();

$pdf->SetFont("times", "BI", 20);

$pdf->Cell(0,10,"Example 002",1,1,'C');

$pdf->Output("example_002.pdf", "I");

?>

but when I open file PDF canot, I get error message ‘not PDF file or corrupted’.

sorry I am newbie.

please help me