Menu

no pdf output

Help
Tikaa
2014-03-05
2014-03-06
  • Tikaa

    Tikaa - 2014-03-05

    i have this table generated by php and styled html inside patients.php file:

    <form name="pt_list" action="classes/MYPDF.php" method="post"><br/>
    <input type="submit" name="pt_pdf"  value="Download as PDF">
        </form>
    <table id="patients">
    <tr>
        <th>Pt. username</th>
        <th>Pt. number</th>
        <th>Full Name</th>
        <th>Added on</th>
    </tr>
    

    <?php $x=1;
    foreach ($users as $patient) {

     ?> <tr <?php if ($x % 2 == 0) {echo 'class="alt"'; } ?>>
          <td> <a href="profile.php?username=<?php echo        $patient['username'];?>"><?php echo $patient['username'];?></a>    </td>
          <td> <?php echo $patient['id'];?></td>
          <td> <?php echo $patient['name'];?></td>
          <td> <?php echo $patient['joined'];?></td>
      </tr>
    
    <?php
       $x++;
        } ?>
    </table>
    <?php
    $GLOBALS['table'] = ob_get_clean();
    $table = $GLOBALS['table'];
    
    ?>
    
     <?php
    
     echo $table;
    

    the table outputs fine on patients.php.i am trying to offer this table as a downloadable pdf for users but for the past few days i cant get it to output. here is what i tried in MYPDF.php:

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

    // Extend the TCPDF class to create custom Header and Footer
    class MYPDF extends TCPDF {

    //Page header
    public function Header() {
    // Logo
    $image_file = K_PATH_IMAGES.'logo_example.jpg';
    $this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
    // Set font
    $this->SetFont('helvetica', 'B', 20);
    // Title
    $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M');
    }

    // Page footer
    

    public function Footer() {
    // Position at 15 mm from bottom
    $this->SetY(-15);
    // Set font
    $this->SetFont('helvetica', 'I', 8);
    // Page number
    $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
    }

    // create new PDF document
    $pdf = new MYPDF(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 003');
    $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 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', 'BI', 12);

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

    // set some text to print

    // print a block of text using Write()
    $pdf->writeHTML($GLOBALS['table'], true, false, true, false, '');

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

    //Close and output PDF document
    $pdf->Output('example_003.pdf', 'D');

    however, when i click the submit button all i get is a blank page no pdf download. please any input is appreciated.

     

    Last edit: Tikaa 2014-03-06
  • Tikaa

    Tikaa - 2014-03-05

    i have error reporting set to E_All and display_errors to On. but no errors output when i click the submit button. only a blank page shows.

     
  • Cristian Gómez

    Cristian Gómez - 2014-03-06

    I answered yesterday from email and I don't know if it reachs you, in case it doesn't, here is my answer:

    Do you have the id property setted like that? without
    the double quotes?? Maybe that can be an issue, also try concatenating all
    the table HTML into $table string instead of make ob_get_clean (don't trust
    me in this but I think that other HTML output isn't well supported by tcpdf)

     
  • Tikaa

    Tikaa - 2014-03-06

    thank you for replying. the table outputs fine alone without trying to download as pdf. i tried to concatenate the table html but always gave me errors in concatenation.

     
  • Cristian Gómez

    Cristian Gómez - 2014-03-06

    I must insist Tikaa, the table is fine because HTML is very flexible but for tcpdf the interpreter must be more strict (imagine the parser look for property="value" patterns and you have property=value only).

    To avoid the problem, just take your table HTML output from page source and put it into a variable and use it with writeHTML to determine wether you have your table HTML malformed or is another issue.

    Go further without that test is waste of time for us to try to help you

     

    Last edit: Cristian Gómez 2014-03-06
  • Tikaa

    Tikaa - 2014-03-06

    i understand what you mean. i changed the id and class to have double quotes and also edited it in the code above. i grabbed the html table from page source and put that in MYPDF.php as a variable and it worked fine and can download the pdf.
    however when trying to output $GLOBALS['table'] in the writeHTML it gives me this error:

    Notice: Undefined index: table in /Users/Tika/PhpstormProjects/ooplr/classes/MYPDF.php on line 107

    Warning: Cannot modify header information - headers already sent by (output started at /Users/Tika/PhpstormProjects/ooplr/classes/MYPDF.php:107) in /Users/Tika/PhpstormProjects/ooplr/tcpdf/tcpdf.php on line 7631
    TCPDF ERROR: Some data has already been output to browser, can't send PDF file

     
  • Tikaa

    Tikaa - 2014-03-06

    i tried this after the table in patients.php:

    ~~~~
    <?php
    $table = ob_get_contents();
    ob_end_clean();
    ?>

    <br/><form name="pt_list" action="classes/MYPDF.php" method="post"><br/>
    <input type="submit" name="pdf"  value="Download as PDF">
        </form>
    <br><br><br>
    

    <?php
    echo $table;~~~~

    then included patients.php in MYPDF.php and when running:

    $pdf->writeHTML($table, true, false, true, false, '');
    $pdf->Output('example_003.pdf', 'D');

    i get a blank page with no errors.

     

    Last edit: Tikaa 2014-03-06

Log in to post a comment.