PHP - TCPDF Error: Some data has already been output, can't send PDF file

95 Views Asked by At

I have a problem with last version of TCPDF for PHP (version 8.3).

Executing this code and using TCPDF library:

        require_once("/var/www/shop/tcpdf/tcpdf.php");
        function CheckPageBreak($fpdf,$h) {
            //If the height h would cause an overflow, add a new page immediately
            if($fpdf->GetY()+$h>$fpdf->PageBreakTrigger()) {$fpdf->AddPage($fpdf->CurOrientation());}
        }
        
        class PDF extends TCPDF {
            function Footer() {
                if($this->PageNo()>1) {
                    $this->SetY(-10);
                    $this->SetFont('Arial','B',8);
                    $pag=($this->PageNo())-1;
                    $this->Cell(0,10,'Pagina '.$pag,0,0,'C');
                }
            }
            function Header() {
                if($this->PageNo()>1) {
                    global $header, $w, $fontfamily;
                    $this->SetFillColor(255,255,204);
                    $this->SetDrawColor(181,11,18);
                    $this->SetTextColor(181,11,18);
                    $this->SetFont($fontfamily,'B',10);
                    $this->Ln();
                }
            }
        }

        $fontfamily = 'Arial';
        $header = array('COD.','PRODOTTO'); 

        $w = array(17,75,28,31,15,30,5); //larghezza colonne
        $altezza = 5; //altezza di una riga articolo
        $pdf = new PDF();
        $pdf->SetAutoPageBreak(true,10);
        $pdf->SetMargins(5,5,5);

        $pdf->AddPage();
        $pdf->SetFont($fontfamily,'',50);
        $pdf->SetFillColor(255,255,204);
        $pdf->SetDrawColor(181,11,18);
        $pdf->SetTextColor(181,11,18);
        $pdf->Rect(5,5,array_sum($w),282,'DF');
        $pdf->Ln(118);

        $pdf->AddPage();
        $cat_prec = "";

        foreach($item as $pro):
            $codice=         trim(mb_convert_encoding($pro["codice"], 'ISO-8859-1', 'UTF-8'));
            $descrizione=    trim($pro["prodotto"], 'ISO-8859-1');
            $nomecategoria2= trim(mb_convert_encoding($pro["nomecategoria2"], 'ISO-8859-1', 'UTF-8'));
            $pdf->SetFont($fontfamily,'',8);

            $righe = max($hdesc, $hconf, $hpeso);
            if($righe>1) {$altezza = 4;}
            else {$altezza = 5;}
            $hr = $altezza*$righe;

            if($nomecategoria2!=$cat_prec) {
                $pdf->SetFont($fontfamily,'B',10);
                $pdf->SetTextColor(0,0,0);
                $pdf->SetDrawColor(200,200,200);
                $pdf->Ln(2);
                CheckPageBreak($pdf,5+$hr);
                $pdf->Cell(array_sum($w),5,strtoupper($nomecategoria2),0,0,'L');
                $x=$pdf->GetX();
                $pdf->Ln();
                $pdf->Line($pdf->GetX(),$pdf->GetY(),$x,$pdf->GetY());
            }
                
            CheckPageBreak($pdf,$hr);

            $pdf->SetFont($fontfamily,'B',9);
            $pdf->SetTextColor(181,11,18);
            $pdf->SetDrawColor(200,200,200);
            $pdf->Cell($w[0], $hr, $codice, 0, 0, 'L');

            $pdf->SetFont($fontfamily,'',8);
            $pdf->SetTextColor(51,51,51);
            $x=$pdf->GetX();
            $x1=$pdf->GetX();
            $y=$pdf->GetY();

            $pdf->SetX($x);
            if($hdesc>1) {$pdf->MultiCell($w[1]-$s, $altezza, $descrizione, 0, 'L');}
            else $pdf->MultiCell($w[1]-$s, $hr, $descrizione, 0, 'L');

            $pdf->SetXY($x1+$w[1],$y);
            
            $cat_prec = $nomecategoria2;
            
        endforeach;
        
        $pdf->Output("/var/www/shop/download/listino_prodotti.pdf");
        ?><script>document.getElementById('link').href='../download/listino_prodotti.pdf'; document.getElementById('link').style.display='block';</script><?php
    }

When i click on button that runs this script, TCPDF returns this error:

TCPDF Error: Some data has already been output, can't send PDF file

I don't understand where is the problem, I don't use echo or print_r, how can I solve?

0

There are 0 best solutions below