【问题标题】:TCPDF ERROR: Some data has already been output, can't send PDF file for my xampp serverTCPDF ERROR: Some data has been output, can't send PDF file for my xampp server
【发布时间】:2018-07-12 09:28:19
【问题描述】:

我正在尝试通过我的 wordpress 插件生成 pdf,但它重复相同的错误。该问题的解决方案是什么? 我已经检查了php标签前后的空白。

       require_once(plugin_dir_path(__FILE__) . '/tcpdf/tcpdf.php');


                                    // create new PDF document
                                    $pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

                                    // set document information
                                    $pdf->SetCreator(PDF_CREATOR);
                                    //$pdf->SetAuthor('Our Code World');
                                    $pdf->SetTitle('First printed pdf!');
                                    // set default header data
                                    $pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);

                                    // set header and footer fonts
                                    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
                                    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

                                    // set default monospaced font
                                    $pdf->SetDefaultMonospacedFont('helvetica');

                                    // set margins

                                    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
                                    $pdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
                                    $pdf->setPrintHeader(false);
                                    $pdf->setPrintFooter(false);
                                    // set auto page breaks
                                    $pdf->SetAutoPageBreak(TRUE, 10);
                                    //set font
                                    $pdf->SetFont('helvetica', '', 12);
                                    $pdf->AddPage();

                                    /*
                                     * content run from here
                                     */

                                    $content = '';
                                    $content .= '
                                        <table border="1" cellspacing="0" cellpadding="5">
                                            <tr>
                                                <th width="5%">ID</th>
                                                <th width="30%">Name</th>

                                            </tr>
                                    ';
                                    $content .= '
                                        <tr>
                                                <td>2574</td>
                                                <td>Rafiq</td>

                                            </tr>
                                    ';
                                    $content .= '</table>';


                                    $pdf->writeHTML($content);
                                    ob_end_clean();
                                    $pdf->Output("sample.pdf", "I");

【问题讨论】:

  • 你能把你的代码贴在这里吗?
  • 您是否在此行之前打印或回显$pdf-&gt;Output("sample.pdf", "I");
  • 否。有条件执行我的代码。提交表单后出现错误消息。请参阅我的代码的一部分。 data-title="Total">FILE) . '/tcpdf/tcpdf.php'); // 创建新的 PDF 文档 $pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  • 什么是data-title="Total"&gt;
  • 它是我文件的 html 块。

标签: wordpress tcpdf


【解决方案1】:

我也遇到了类似的问题,我通过删除表格标签的所有属性解决了这个问题。 TCPDF 只支持有限的属性,试试这个

    $content = '';
    $content .= '<table>
            <tr>
                <th>ID</th>
                <th>Name</th>
            </tr>';
    $content .= '<tr>
                <td>2574</td>
                <td>Rafiq</td>
            </tr>';
    $content .= '</table>';

【讨论】:

    【解决方案2】:
    include 'includes/session.php';
    
    $range = $_POST['date_range'];
    $ex = explode(' - ', $range);
    $from = date('Y-m-d', strtotime($ex[0]));
    $to = date('Y-m-d', strtotime($ex[1]));
    
    $sql = "SELECT *, SUM(amount) as total_amount FROM deductions";
    $query = $conn->query($sql);
    $drow = $query->fetch_assoc();
    $deduction = $drow['total_amount'];
    
    $from_title = date('M d, Y', strtotime($ex[0]));
    $to_title = date('M d, Y', strtotime($ex[1]));
    
    require_once('../tcpdf/tcpdf.php');  
    $pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
    $pdf->SetCreator(PDF_CREATOR);  
    $pdf->SetTitle('Payslip: '.$from_title.' - '.$to_title);  
    $pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
    $pdf->SetDefaultMonospacedFont('helvetica');  
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
    $pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);  
    $pdf->setPrintHeader(false);  
    $pdf->setPrintFooter(false);  
    $pdf->SetAutoPageBreak(TRUE, 10);  
    $pdf->SetFont('helvetica', '', 11);  
    $pdf->AddPage(); 
    $contents = '';
    
    $sql = "SELECT *, SUM(num_hr) AS total_hr, attendance.employee_id AS empid, employees.employee_id AS employee FROM attendance LEFT JOIN employees ON employees.id=attendance.employee_id LEFT JOIN position ON position.id=employees.position_id WHERE date BETWEEN '$from' AND '$to' GROUP BY attendance.employee_id ORDER BY employees.lastname ASC, employees.firstname ASC";
    
    $query = $conn->query($sql);
    while($row = $query->fetch_assoc()){
        $empid = $row['empid'];
                      
        $casql = "SELECT *, SUM(amount) AS cashamount FROM cashadvance WHERE employee_id='$empid' AND date_advance BETWEEN '$from' AND '$to'";
      
        $caquery = $conn->query($casql);
        $carow = $caquery->fetch_assoc();
        $cashadvance = $carow['cashamount'];
    
        $gross = $row['rate'] * $row['total_hr'];
        $total_deduction = $deduction + $cashadvance;
        $net = $gross - $total_deduction;
    
        $contents .= '
            <h2 align="center">TechSoft IT Solutions</h2>
            <h4 align="center">'.$from_title." - ".$to_title.'</h4>
            <table cellspacing="0" cellpadding="3">  
                <tr>  
                    <td width="25%" align="right">Employee Name: </td>
                    <td width="25%"><b>'.$row['firstname']." ".$row['lastname'].'</b></td>
                    <td width="25%" align="right">Rate per Hour: </td>
                    <td width="25%" align="right">'.number_format($row['rate'], 2).'</td>
                </tr>
                <tr>
                    <td width="25%" align="right">Employee ID: </td>
                    <td width="25%">'.$row['employee'].'</td>   
                    <td width="25%" align="right">Total Hours: </td>
                    <td width="25%" align="right">'.number_format($row['total_hr'], 2).'</td> 
                </tr>
                <tr> 
                    <td></td> 
                    <td></td>
                    <td width="25%" align="right"><b>Gross Pay: </b></td>
                    <td width="25%" align="right"><b>'.number_format(($row['rate']*$row['total_hr']), 2).'</b></td> 
                </tr>
                <tr> 
                    <td></td> 
                    <td></td>
                    <td width="25%" align="right">Deduction: </td>
                    <td width="25%" align="right">'.number_format($deduction, 2).'</td> 
                </tr>
                <tr> 
                    <td></td> 
                    <td></td>
                    <td width="25%" align="right">Cash Advance: </td>
                    <td width="25%" align="right">'.number_format($cashadvance, 2).'</td> 
                </tr>
                <tr> 
                    <td></td> 
                    <td></td>
                    <td width="25%" align="right"><b>Total Deduction:</b></td>
                    <td width="25%" align="right"><b>'.number_format($total_deduction, 2).'</b></td> 
                </tr>
                <tr> 
                    <td></td> 
                    <td></td>
                    <td width="25%" align="right"><b>Net Pay:</b></td>
                    <td width="25%" align="right"><b>'.number_format($net, 2).'</b></td> 
                </tr>
            </table>
            <br><hr>
        ';
    }
    $pdf->writeHTML($contents);  
    $pdf->Output('payslip1.pdf', 'I');
    

    【讨论】:

    • 请解释你的答案
    猜你喜欢
    • 2011-10-26
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2016-11-04
    • 2016-04-08
    • 2020-10-12
    • 2020-03-13
    • 1970-01-01
    相关资源
    最近更新 更多