TCPDF错误:已经输出了一些数据,无法为我的xampp服务器发送PDF文件

时间:2018-07-12 09:28:19

标签: wordpress tcpdf

我正在尝试通过我的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");

2 个答案:

答案 0 :(得分:0)

我也面临类似的问题,我通过删除表标签的所有属性解决了这一问题。 TCPDF仅支持有限的属性,请尝试

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

答案 1 :(得分:0)

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');