How to make a good PDF table using PHP?

10,762

There is a good library, albeit with very limited documentation, that extends the FPDF library called FPDF Advanced Table ( http://www.interpid.eu/ ).

Share:
10,762
user3213531
Author by

user3213531

Updated on June 14, 2022

Comments

  • user3213531
    user3213531 almost 2 years

    I just want to ask about creating a good pdf table using PHP? There are cells in the table that needs to be multicell, the problem is, I can't make the height of the other cells the same as of those in multicells. Here's the whole code for the PDF generation (I was searching for codes on the web and try if it's working, I haven't finished revising this code).

    <?php
    //============================================================+
    // File name   : example_004.php
    // Begin       : 2008-03-04
    // Last Update : 2013-05-14
    //
    // Description : Example 004 for TCPDF class
    //               Cell stretching
    //
    // Author: Nicola Asuni
    //
    // (c) Copyright:
    //               Nicola Asuni
    //               Tecnick.com LTD
    //               www.tecnick.com
    //               [email protected]
    //============================================================+
    
    /**
     * Creates an example PDF TEST document using TCPDF
     * @package com.tecnick.tcpdf
     * @abstract TCPDF - Example: Cell stretching
     * @author Nicola Asuni
     * @since 2008-03-04
     */
    
    // Include the main TCPDF library (search for installation path).
    require_once('tcpdf_include.php');
    include('../../../conn.php');
    $sql = mysql_query("SELECT * FROM tbl_video LIMIT 10");
    // 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 004');
    $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.' 004', 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 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);
    
    // 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 (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $pdf->setLanguageArray($l);
    }
    
    // ---------------------------------------------------------
    
    // set font
    $pdf->SetFont('times', '', 11);
    
    // add a page
    $pdf->AddPage();
    
    // MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)
    
    // test Cell stretching
    while($row = mysql_fetch_array($sql)){
        $pdf->MultiCell(55, 5, $row['ref_code'], 1, 'L', 0, 0, '', '', true);
        $pdf->MultiCell(55, 5, $row['video_title'], 1, 'L', 0, 0, '', '', true);
        $pdf->Ln();
    }
    
    $pdf->AddPage();
    
    // example using general stretching and spacing
    
    
    
    // ---------------------------------------------------------
    
    //Close and output PDF document
    $pdf->Output('example_004.pdf', 'I');
    
    //============================================================+
    // END OF FILE
    //============================================================+
    

    The photo of the table can be seen here.

  • user3213531
    user3213531 over 10 years
    I've already tried an alternative, and yes, I've used the HTML to PDF generation :) however, I used this html2pdf.fr/en/default instead and used the function wordwrap() to fit the contents.