Cambiar la librería fpdf por tcpdf

He estado utilizando la librería tcpdf para generar reportes en pdf. Se basa en fpdf y es mucho mejor ya que pasa de html a pdf con mucha precisición además de otras funcionalidades que mejoran con creces la librería padre.

Comentarios

  • Tienes algunos ejemplo que puedas hacerme llegar y hacer las pruebas...

    deivinsontejeda NOT SPAM kumbiaphp.com
  • edited 10:17
    Los ejemplos de tcpdf (<!-- m --><a class="postlink" href="http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples">http://www.tecnick.com/public/code/cp_d ... f_examples</a><!-- m -->) valen para mostrar la funcionalidad, resumo un pequeño ejemplo.

    Los pasos que yo he seguido son:
    1) Descargar la librería de <!-- m --><a class="postlink" href="http://sourceforge.net/project/showfiles.php?group_id=128076">http://sourceforge.net/project/showfile ... _id=128076</a><!-- m --> y descomprimir la carpeta en library. Editar el archivo de configuración para añadir la libreria tcpdf.

    2) Crear un controlador pdf_controller.php
    <?php
    class PdfController extends ApplicationController {

    function reporte(){
    $this->set_response("view");
    // create new PDF document
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor("Nicola Asuni");
    $pdf->SetTitle("TCPDF Example 006");
    $pdf->SetSubject("TCPDF Tutorial");
    $pdf->SetKeywords("TCPDF, PDF, example, test, guide");

    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

    // 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 margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

    //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);

    //initialize document
    $pdf->AliasNbPages();

    // add a page
    $pdf->AddPage();

    //

    // set font
    $pdf->SetFont("dejavusans", "", 10);

    // create some HTML content
    $htmlcontent = "<h1>HTML Example</h1>&lt; € &euro; &#8364; &amp; è &egrave; &copy; &gt; \\slash \\\\double-slash \\\\\\triple-slash<h2>List</h2>List example:<ol><li><b>bold text</b></li><li><i>italic text</i></li><li><u>underlined text</u></li><li><b>b<i>bi<u>biu</u>bi</i>b</b></li><li><a href=\"http://www.tecnick.com\"; dir=\"ltr\">link to <!-- m --><a class="postlink" href="http://www.tecnick.com">http://www.tecnick.com</a><!-- m --></a></li><li>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.<br />Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</li><li>SUBLIST<ol><li>row one<ul><li>sublist</li></ul></li><li>row two</li></ol></li><li><b>T</b>E<i>S</i><u>T</u> <del>line through</del></li><li><font size=\"+3\">font + 3</font></li><li><small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal</li></ol><dl><dt>Coffee</dt><dd>Black hot drink</dd><dt>Milk</dt><dd>White cold drink</dd></dl><div style=\"text-align:center\">IMAGES<br /><img src=\"../images/logo_example.png\" alt=\"test alt attribute\" width=\"100\" height=\"100\" border=\"0\" /><img src=\"../images/tiger.ai\" alt=\"test alt attribute\" width=\"100\" height=\"100\" border=\"0\" /><img src=\"../images/logo_example.jpg\" alt=\"test alt attribute\" width=\"100\" height=\"100\" border=\"0\" /></div>";

    // output the HTML content
    $pdf->writeHTML($htmlcontent, true, 0, true, 0);

    // output some RTL HTML content
    $pdf->writeHTML("<div style=\"text-align:center\">The words &#8220;<span dir=\"rtl\">&#1502;&#1494;&#1500; [mazel] &#1496;&#1493;&#1489; [tov]</span>&#8221; mean &#8220;Congratulations!&#8221;</div>", true, 0, true, 0);

    // test some inline CSS
    $inlinecss = "<p>This is just an example of html code to demonstrate some supported CSS inline styles.
    <span style=\"font-weight: bold;\">bold text</span>
    <span style=\"text-decoration: line-through;\">line-trough</span>
    <span style=\"text-decoration: underline line-through;\">underline and line-trough</span>
    <span style=\"color: rgb(0, 128, 64);\">color</span>
    <span style=\"background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);\">background color</span>
    <span style=\"font-weight: bold;\">bold</span>
    <span style=\"font-size: xx-small;\">xx-small</span>
    <span style=\"font-size: x-small;\">x-small</span>
    <span style=\"font-size: small;\">small</span>
    <span style=\"font-size: medium;\">medium</span>
    <span style=\"font-size: large;\">large</span>
    <span style=\"font-size: x-large;\">x-large</span>
    <span style=\"font-size: xx-large;\">xx-large</span>
    </p>";

    $pdf->writeHTML($inlinecss, true, 0, true, 0);

    // reset pointer to the last page
    $pdf->lastPage();

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Print a table

    // add a page
    $pdf->AddPage();

    // create some HTML content
    $subtable = "<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\"><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>";

    $htmltable = "<h2>HTML TABLE:</h2><table border=\"1\" cellspacing=\"2\" cellpadding=\"2\"><tr><th>#</th><th align=\"right\">RIGHT align</th><th align=\"left\">LEFT align</th><th>4A</th></tr><tr><td>1</td><td bgcolor=\"#cccccc\" align=\"center\" colspan=\"2\">A1 ex<i>amp</i>le <a href=\"http://www.tcpdf.org\">link</a>; column span. One two tree four five six seven eight nine ten.<br />line after br<br /><small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla<ol><li>first<ol><li>sublist</li><li>sublist</li></ol></li><li>second</li></ol><small color=\"#FF0000\" bgcolor=\"#FFFF00\">small small small small small small small small small small small small small small small small small small small small</small></td><td>4B</td></tr><tr><td>".$subtable."</td><td bgcolor=\"#0000FF\" color=\"yellow\" align=\"center\">A2 € &euro; &#8364; &amp; è &egrave;<br/>A2 € &euro; &#8364; &amp; è &egrave;</td><td bgcolor=\"#FFFF00\" align=\"left\"><font color=\"#FF0000\">Red</font> Yellow BG</td><td>4C</td></tr><tr><td>1A</td><td rowspan=\"2\" colspan=\"2\" bgcolor=\"#FFFFCC\">2AA<br />2AB<br />2AC</td><td bgcolor=\"#FF0000\">4D</td></tr><tr><td>1B</td><td>4E</td></tr><tr><td>1C</td><td>2C</td><td>3C</td><td>4F</td></tr></table>";

    // output the HTML content
    $pdf->writeHTML($htmltable, true, 0, true, 0);

    // Print some HTML Cells

    $cellcontent = "<span color=\"red\">red</span> <span color=\"green\">green</span> <span color=\"blue\">blue</span><br /><span color=\"red\">red</span> <span color=\"green\">green</span> <span color=\"blue\">blue</span>";

    $pdf->SetFillColor(255,255,0);

    $pdf->writeHTMLCell(0, 0, '', '', $cellcontent, "LRTB", 1, 0, true, 'L');
    $pdf->writeHTMLCell(0, 0, '', '', $cellcontent, "LRTB", 1, 1, true, 'C');
    $pdf->writeHTMLCell(0, 0, '', '', $cellcontent, "LRTB", 1, 0, true, 'R');

    // reset pointer to the last page
    $pdf->lastPage();

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Print a table

    // add a page
    $pdf->AddPage();

    // create some HTML content

    $tablealign = '<h1>Image alignments on HTML table</h1><table cellpadding="1" cellspacing="1" border="1" style="text-align:center;">
    <tr><td><img src="../images/logo_example.png" border="0" height="41" width="41" /></td></tr>
    <tr style="text-align:left;"><td><img src="../images/logo_example.png" border="0" height="41" width="41" align="top" /></td></tr>
    <tr style="text-align:center;"><td><img src="../images/logo_example.png" border="0" height="41" width="41" align="middle" /></td></tr>
    <tr style="text-align:right;"><td><img src="../images/logo_example.png" border="0" height="41" width="41" align="bottom" /></td></tr>
    <tr><td style="text-align:left;"><img src="../images/logo_example.png" border="0" height="41" width="41" align="top" /></td></tr>
    <tr><td style="text-align:center;"><img src="../images/logo_example.png" border="0" height="41" width="41" align="middle" /></td></tr>
    <tr><td style="text-align:right;"><img src="../images/logo_example.png" border="0" height="41" width="41" align="bottom" /></td></tr>
    </table>';

    // output the HTML content
    $pdf->writeHTML($tablealign, true, 0, true, 0);

    // reset pointer to the last page
    $pdf->lastPage();

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // Print all HTML colors

    // add a page
    $pdf->AddPage();

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

    $textcolors = "<h1>HTML Text Colors</h1>";
    $bgcolors = "<hr /><h1>HTML Background Colors</h1>";

    foreach($webcolor as $k => $v) {
    $textcolors .= "<span color=\"#".$v."\">".$v."</span> ";
    $bgcolors .= "<span bgcolor=\"#".$v."\" color=\"#333333\">".$v."</span> ";
    }

    // output the HTML content
    $pdf->writeHTML($textcolors, true, 0, true, 0);
    $pdf->writeHTML($bgcolors, true, 0, true, 0);

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

    // Test word-wrap

    // create some HTML content
    $htmltxt = "<hr /><h1>Various tests</h1><font face=\"courier\"><b>thisisaverylongword</b></font> <font face=\"helvetica\"><i>thisisanotherverylongword</i></font> <font face=\"times\"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face=\"arialunicid0\">thisisaverylongword</font> <font face=\"courier\"><b>thisisaverylongword</b></font> <font face=\"helvetica\"><i>thisisanotherverylongword</i></font> <font face=\"times\"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face=\"arialunicid0\">thisisaverylongword</font> <font face=\"courier\"><b>thisisaverylongword</b></font> <font face=\"helvetica\"><i>thisisanotherverylongword</i></font> <font face=\"times\"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face=\"arialunicid0\">thisisaverylongword</font> <font face=\"courier\"><b>thisisaverylongword</b></font> <font face=\"helvetica\"><i>thisisanotherverylongword</i></font> <font face=\"times\"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face=\"arialunicid0\">thisisaverylongword</font> <font face=\"courier\"><b>thisisaverylongword</b></font> <font face=\"helvetica\"><i>thisisanotherverylongword</i></font> <font face=\"times\"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face=\"arialunicid0\">thisisaverylongword</font> ";

    // output the HTML content
    $pdf->writeHTML($htmltxt, true, 0, true, 0);

    // Test fonts nesting

    $teststr1 = "Default <font face=\"courier\">Courier <font face=\"helvetica\">Helvetica <font face=\"times\">Times <font face=\"arialunicid0\">arialunicid0 </font>Times </font>Helvetica </font>Courier </font>Default";

    $teststr2 = '<small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal';

    $teststr3 = '<font size="10" color="#ff7f50">The</font> <font size="10" color="#6495ed">quick</font> <font size="14" color="#dc143c">brown</font> <font size="18" color="#008000">fox</font> <font size="22"><a href="http://www.tcpdf.org">jumps</a></font>; <font size="22" color="#a0522d">over</font> <font size="18" color="#da70d6">the</font> <font size="14" color="#9400d3">lazy</font> <font size="10" color="#4169el">dog</font>.';

    $html = $teststr1."<br />".$teststr2."<br />".$teststr3."<br />".$teststr3."<br />".$teststr2;

    // output the HTML content
    $pdf->writeHTML($html, true, 0, true, 0);

    // reset pointer to the last page
    $pdf->lastPage();

    //

    //Close and output PDF document
    $this-­>file="public/temp/".md5(uniqid()).".pdf";
    $pdf-­>Output($this­->file, 'D');

    //============================================================+
    // END OF FILE
    //============================================================+

    }
    }
    ?>

    Y luego en la vista lo mismo que hay en el manual de la versión 0.5
    pdf/reporte.phtml

    <?php
    if ($file != null) {echo "<script type='text/javascript'> " .
    "window.open('".KUMBIA_PATH.$file."', false); </script>";
    Flash::success("<h4><font color='navy'>Reporte Generado...</
    font></h4>");
    }
    content();
    ?>

    Con esto debe de valer.
  • Esto sigue siendo horrible tanto como fpdf!
    Alguin conoce una forma mas facil de general reporte

  • Es muy fácil, lo que pasa es que el ejemplo es un poco largo jejeje básicamente debes tomar tu html y ponerlo en una variable e imprimirlo en el PDF, así de simple.

    Es una manera muy muy fácil de generar reportes, o por lo menos así lo veo yo ;)
Sign In or Register to comment.