【问题标题】:FPDF error: some data has already been output. Can't send PDF fileFPDF 错误:一些数据已经输出。无法发送 PDF 文件
【发布时间】:2020-05-15 09:23:23
【问题描述】:

我正在使用 FPDF 库生成 PDF 文档,但出现错误 “某些数据已输出,无法发送 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');
}
?>

我尝试在开头添加 "ob_start()" 并在末尾添加 "ob_end_flush()"。我知道这不是这个问题的第一个主题,但试图在这个论坛中找到信息,现在我不知道该怎么做。我希望有人可以帮助我解决这个问题。

【问题讨论】:

  • 应该在浏览器(开发控制台)中看到该输出。可能是错误消息。

标签: php fpdf


【解决方案1】:
最近更新 更多