1

I am using TCPDF to generate the pdf file.

<form method='post'>
    <input type='submit' name='create_pdf' class='gs-btn-3' value='Create PDF'/>
</form>
<?php 
    if (isset($_POST["create_pdf"]))
        {
            require_once("myPDF/TCPDF/tcpdf.php");
            $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
            $obj_pdf->SetCreator(PDF_CREATOR);
            $obj_pdf->SetTitle('Thisi is Serena first PDF example');
            $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
            $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN));
            $obj_pdf->SetFooterFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA));
            $obj_pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
            $obj_pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
            $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
            $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
            $obj_pdf->SetPrintHeader(false);
            $obj_pdf->SetPrintFooter(false); //true
            $obj_pdf->SetAutoPageBreak(TRUE, 10);
            $obj_pdf->SetFont('dejavusans', '', 14);
            $obj_pdf->AddPage();

            $content = '
                        <h3 align="center"> Export HTML Table data to PDF using TCPDF in PHP</h3>
                        <table border="1" cellspacing="6" cellpadding="4">
                            <tr>
                                <th width="25%" align="center">Company Name</th>
                                <th width="25%">Contact</th>
                                <th width="10%">Address</th>
                                <th width="10%">Phone</th>
                                <th width="10%">Modify by</th>
                                <th width="10%">Modify on</th>  
                                <th width="10%">EDIT</th>                           
                            </tr>
                            if(isset($this->culist) && is_array($this->culist) && sizeOf($this->culist) > 0) {
                            foreach($this->culist as $key=>$value) {    
                            <tr>
                                <td style="width: 25%;">
                                    <?php echo $value->companyname; ?>                      
                                </td>
                                <td style="width: 25%;">
                                    <?php echo $value->firstname; ?>
                                </td>
                                <td style="width: 10%;">
                                    <?php echo $value->address; ?>
                                </td>
                                <td style="width: 10%;">
                                    <?php echo $value->phone; ?>
                                </td>
                                <td style="width: 10%;">
                                    <?php echo $value->modified_by; ?>
                                </td>   
                                <td style="width: 10%;">
                                    <?php echo $value->modified_on; ?>
                                </td>                               

                            </tr>
                            }
                        </table>
                            ';

                            $obj_pdf->WriteHTML($content, true, false, true, false, '');
                            ob_end_clean();

                            $obj_pdf->Output('sample.pdf', 'I');
                }

            ?>

There are two errors,

First is the "TCPDF ERROR: Some data has already been output, can't send PDF file". I tried "ob_end_clean();" before "$obj_pdf->Output('sample.pdf', 'I');", it shows so many unreadable code.

Second is that the generated table is not from the scripts which showed above, rather the table comes from the the view.

Anyone who can help me? Thanks in advance.

Erick Boshoff
  • 1,443
  • 2
  • 18
  • 30
serena
  • 13
  • 1
  • 4
  • You must be serena williams :) Just kidding .. BTW .. have you checked this thread https://stackoverflow.com/questions/16011050/tcpdf-error-some-data-has-already-been-output-cant-send-pdf-file ? – Mittul At TechnoBrave Aug 03 '17 at 05:40
  • thanks, I have looked through, but it does not suit my question.. – serena Aug 03 '17 at 05:53
  • ok mam .. just do one thing .. instead of sending big html data of table .. just put simple text and check whether its working or not. – Mittul At TechnoBrave Aug 03 '17 at 05:56
  • It is a good idea to put the simple text!! But the generated page is the table which is in the view. It seems that it does not go through the TCPDF php scripts. Actually, I am using the MVC structure and I put these scripts in the view file. Do you know where I should put? – serena Aug 03 '17 at 06:16
  • The reason i told you to put simple text is for debug purpose only mam whether we have an error in our html code or somewhere else. – Mittul At TechnoBrave Aug 03 '17 at 06:17
  • I guess you can put your php code in another php page mam and form action attribute needs to be set to this new php page. – Mittul At TechnoBrave Aug 03 '17 at 06:19
  • Remove the `from` from top of the page and try again (and change the `if` statement to send the PDF anyway, regardless of post `$_POST["create_pdf"]`, if it works, I might be able to help you. – Nima Aug 03 '17 at 13:50

1 Answers1

1

I know this is a very old question. I do have an answer, atleast in my scenario . same errors occur even with ob_end_clean() and no output. strangely, when i remove ob_end_clean(); it gets output to pdf

D_N_A
  • 13
  • 6