简体   繁体   中英

TCPDF returning error while generating table structure to print

I am working in SmartJobBoard with TCPDF. PDF was generated properly on the old server.

After we moved the site to a new server, it began to show errors while creating PDF invoices. The following error is shown by the TCPDF library.

Notice: Undefined index: cols in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22174
Notice: Undefined index: thead in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 21705
Notice: Undefined index: rows in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22162
Warning: array_push() expects parameter 1 to be array, null given in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22164
Warning: array_push() expects parameter 1 to be array, null given in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22164
Warning: array_push() expects parameter 1 to be array, null given in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22164
Notice: Undefined index: thead in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 21718

Exception Object
(
    [message:protected] => PDF generation failed: Some data has already been output, can't send PDF file
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php
    [line:protected] => 29841
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php
                    [line] => 9062
                    [function] => Error
                    [class] => ExceptionThrowingTCPDF
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Some data has already been output, can't send PDF file
                        )

                )

            [1] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/system/lib/payment/Invoices/InvoiceManager.php
                    [line] => 141
                    [function] => Output
                    [class] => TCPDF
                    [type] => ->
                    [args] => Array
                        (
                            [0] => invoice_3284.pdf
                            [1] => D
                        )

                )

            [2] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/system/user-scripts/payment/view_invoice.php
                    [line] => 55
                    [function] => generatePDFVersion
                    [class] => SJB_InvoiceManager
                    [type] => ::
                    [args] => Array
                        (
                            [0] => 




Rechnung

Datum: 01.10.2014
Rechnung #3284
Status der Rechnung: In Bearbeitung (Zahlung auf Rechnung (Abonnement wird freigeschalten, sobald Zahlungseingang erfolgt ist.))

Rechnungsempfänger





Zahlungsempfänger

Ocean Harvest GmbH
Ruessenstrasse 18
6340 Baar

Konto:
Bank: UBS AG, Zug
IBAN: CH81 0027 3273 1186 8701 T


Beschreibung    Betrag
PlacementBooster.ch
Firmen-Abonnement: 12 Monate
50 Mitarbeiter (51 Accounts)    CHF 13,808.00
Zwischensumme   CHF 13,808.00
MWST-Nr. CHE-108.643.082 MWST MWST Ref.-Nr. 417985  CHF 1,104.64
Insgesamt   CHF 14,912.64

                            [1] => 3284
                        )

                )

            [3] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/system/core/ModuleManager.php
                    [line] => 127
                    [function] => execute
                    [class] => SJB_Payment_ViewInvoice
                    [type] => ->
                    [args] => Array
                        (
                        )

                )

            [4] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/system/core/System.php
                    [line] => 140
                    [function] => executeFunction
                    [class] => SJB_ModuleManager
                    [type] => ->
                    [args] => Array
                        (
                            [0] => payment
                            [1] => view_invoice
                            [2] => 
                            [3] => /view-invoice/
                        )

                )

            [5] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/system/core/PageConstructor.php
                    [line] => 29
                    [function] => executeFunction
                    [class] => SJB_System
                    [type] => ::
                    [args] => Array
                        (
                            [0] => payment
                            [1] => view_invoice
                            [2] => 
                            [3] => /view-invoice/
                        )

                )

            [6] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/system/core/System.php
                    [line] => 193
                    [function] => getPage
                    [class] => SJB_PageConstructor
                    [type] => ::
                    [args] => Array
                        (
                            [0] => SJB_UserPageConfig Object
                                (
                                    [uri] => /view-invoice/
                                    [module] => payment
                                    [function] => view_invoice
                                    [template] => 
                                    [title] => View Invoice
                                    [parameters] => 
                                    [keywords] => 
                                    [page_exists] => 1
                                    [page_id] => 539
                                    [has_raw_output] => 
                                    [numberOfViews] => 
                                    [description] => 
                                )

                        )

                )

            [7] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/system/core/Request.php
                    [line] => 209
                    [function] => getPage
                    [class] => SJB_System
                    [type] => ::
                    [args] => Array
                        (
                            [0] => SJB_UserPageConfig Object
                                (
                                    [uri] => /view-invoice/
                                    [module] => payment
                                    [function] => view_invoice
                                    [template] => 
                                    [title] => View Invoice
                                    [parameters] => 
                                    [keywords] => 
                                    [page_exists] => 1
                                    [page_id] => 539
                                    [has_raw_output] => 
                                    [numberOfViews] => 
                                    [description] => 
                                )

                        )

                )

            [8] => Array
                (
                    [file] => /home/pb_admin/public_html/pb/index.php
                    [line] => 72
                    [function] => execute
                    [class] => SJB_Request
                    [type] => ->
                    [args] => Array
                        (
                        )

                )

        )

    [previous:Exception:private] => 
)

Why is this error happening?

TCPDF requires 100% valid XHTML syntax

Even "HTML Tidy" will not show you all the html errors. In my case I had a tr tag not closed, and after I corrected this the warnings disappeared, but the pdf file was ugly. The problem was solved by changing, for example, colspan=12 by colspan=”12” and rowspan=2 by rowspan=”2” .

Try inserting...

//Change To Avoid the PDF Error
  ob_end_clean();

if this doesnt work, try using ob_clean(); instead.

just before you do:

$pdf->Output($filename, $output_dest);

The error occures because TCPDF is throwing errors before the final rendering has been completed. In order to prevent TCPDF from creating non working PDFs it rather decides to crash.

Just in case TCPDF doesnt like you anymore, take a look at DomPDF :)

The problem is that your PHP script is printing to the browser before it sends the PDF . Why is PHP printing to the browser before sending the PDF? Because there are errors, and PHP is configured to print errors to the browser.

The two solutions given above are (bad) workarounds for supressing the errors . Fix the code, the errors will disappear, and then the PDF can be sent properly.

Why is PHP throwing errors? Let's take a look:

Notice: Undefined index: cols in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22174
Notice: Undefined index: thead in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 21705
Notice: Undefined index: rows in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22162
Warning: array_push() expects parameter 1 to be array, null given in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22164
Warning: array_push() expects parameter 1 to be array, null given in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22164
Warning: array_push() expects parameter 1 to be array, null given in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 22164
Notice: Undefined index: thead in /home/pb_admin/public_html/pb/system/ext/tcpdf/tcpdf.php on line 21718

The first three errors are issues of the code trying to access an array element before checking to see if it is set. As the problem is in the /tcpdf/tcpdf.php file I don't think that you can fix this. File a bug with TCPDF.

The next four errors are most likely due to the first three. Fix those first three, and the next four errors will most likely be resolved as well.

If you post a bug report then please post the link to it in the comments to this answer. Thanks.

I think you should make debug level 0 in your action. Use Configure::write('debug', 0);

You have to catch all buffer through ob_* function, no echo or print or header before ob_start function:

<?php
ob_start();
[ ... ]
$content_pdf = ob_get_contents();
ob_end_flush();
?>

Enjoy your code!

You should check that all opening tags have matching closing tags! That's one of the least suspected bugs in html, and probably the reason you get the error: "Headers already sent"

In my case the errors in tcpdf.php appeared because I had some mistakes in HTML - an unclosed td tag. It seems a HTML error like this will throw lots of errors in tcpdf.php file, including ones that does not have any logical link with the actual error.

My advice is to isolate the error by removing the entire 'html' that you send to output, and just add one letter. If you get your empty pdf, start checking your html variable.

Have come across same issue. This issue was due to bad html and hence TCPDF was throwing error. After fixing html issues TCPDF worked fine for me, to fix html issues click here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM