简体   繁体   中英

FPDF error: some data has already been output. Can't send PDF file

I'm using FPDF library to generate PDF document, but I'm getting error "Some data has already been output, can't send PDF" .

<?php
function fetch_data()
{
    //užklausa
    include('database_connection.php');
    $query = "SELECT * FROM mvap.daiktas";
    $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    $id = 1;
    foreach($result as $row)
    {
        $pavadinimas = $row['daiktoPavadinimas'];
        $inventorinis = $row['inventorinisNr'];
    }
    $data = '2020-01-25';
    $html = '
    <body>
        <style>
            th {font-weight: bold; font-family:  };
            tr {font-weight: normal};
        </style>
        <table border="1" cellpadding="4" cellspacing="0" style="text-align: center">
            <tr>
                <th width="30px" height="5">Eil. Nr.</th>
                <th width="130px" height="5">Materialinės vertybės pavadinimas</th>
                <th width="80px" height="5">Inventorinis numeris</th>
                <th width="100px" height="5">Gamyklinis numeris</th>
                <th width="70" height="5">Gavimo data</th>
                <th width="65" height="5">Gavėjo parašas</th>
                <th width="70" height="5">Grąžinimo data</th>
                <th width="80" height="5">Grąžinančio asmens parašas</th>
                <th width="70" height="5">Priimančio asmens parašas</th>
                <th width="80" height="5">Pastabos</th>
            </tr>
            <tr>
                <td>'. $id .'</td>
                <td>'. $pavadinimas .'</td>
                <td>'. $inventorinis .'</td>
                <td>-</td>
                <td>'. $data .'</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
                <td>-</td>
            </tr>

        </table>
    </body>

    ';
    return $html;
}
if(isset($_POST['submit1']))
{
    require('libs/TCPDF/tcpdf_import.php');
    $obj_pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $obj_pdf->SetCreator(PDF_CREATOR);
    $obj_pdf->SetTitle("Apskaitos kortelė");
    $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('dejavuserif');
    $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);
    $obj_pdf->setPrintHeader(false);
    $obj_pdf->setPrintFooter(false);
    $obj_pdf->SetAutoPageBreak(TRUE, 10);
    $obj_pdf->SetFont('dejavuserif', '', 10);
    $obj_pdf->AddPage();
    $obj_pdf->Cell(0,6,'ĮMONĖ',0,0,'C');
    $obj_pdf->Ln();
    $obj_pdf->SetLineWidth(0.5);
    $obj_pdf->Line(5, 15, 297-5, 15);
    $obj_pdf->Ln();
    $obj_pdf->SetFontSize(12);
    $obj_pdf->Cell(0,20,'KOMPIUTERINĖS TECHNIKOS, RYŠIO PRIEMONIŲ IR PROGRAMINĖS ĮRANGOS APSKAITOS KORTELĖ',0,0,'C');
    $obj_pdf->Ln();
    $obj_pdf->SetFontSize(10);
    $obj_pdf->Cell(20, 40, 'Valdybos, skyriaus pavadinimas' , 0, false, 'L', 0, '', 0, false, 'T', 'M');
    $obj_pdf->Cell(230, 40, 'SKOV NKC', 0, false, 'C', 0, '', 0, false, 'T', 'M');
    $obj_pdf->Line(10, 65, 297-100, 65);
    $obj_pdf->Ln(0);
    $obj_pdf->Cell(20, 55, 'Turto buvimo vieta, kab. Nr.' , 0, false, 'L', 0, '', 0, false, 'T', 'M');
    $obj_pdf->Cell(230, 55, 'Kabinetas', 0, false, 'C', 0, '', 0, false, 'T', 'M');
    $obj_pdf->Line(10, 72, 297-100, 72);
    $obj_pdf->Ln(0);
    $obj_pdf->Cell(20, 70, 'Atsakingo žmogaus vardas, pavardė' , 0, false, 'L', 0, '', 0, false, 'T', 'M');
    $obj_pdf->Cell(230, 70, 'Vardenis Pavardenis', 0, false, 'C', 0, '', 0, false, 'T', 'M');
    $obj_pdf->Line(10, 80, 297-100, 80);
    $content = '';
    $content .= fetch_data();
    $obj_pdf->SetXY(5, 90);
    $obj_pdf->writeHTML($content);
    $obj_pdf->Output('doc.pdf', 'I');
}
?>

I tried to add "ob_start()" at the beggining and "ob_end_flush()" at the end. I know that it's not first topic of this problem, but tried to find information in this forum and now I have no idea what to do more. I hope somebody could help me to fix this problem.

Solved my problem. I won't delete my topic. Hope this will help for someone. Solved problem by posting this php code to another blank php file and called that file from another file button click. Now it works without problem.

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