如何使用TCPDF将数据插入pdf

时间:2017-05-19 13:56:57

标签: php html tcpdf

我正忙着尝试使用TCPDF创建一个pdf,但是pdf中没有显示任何内容,唯一的东西就是表格的标题。因此,不会显示搜索后调用的数据。 NB我也使用了两个表之间的连接。

<?php
ob_start();
function fetch_data(){
$output="";

if(isset($_POST["btnSearch"]))
{


if (isset($_POST['txtNameSearch'])){
$search = $_POST['txtNameSearch'];

$connection = mysqli_connect('localhost', 'root', '', 'bookstore');
$sql="SELECT * FROM order_details right join tblproduct on 
order_details.prod_id=tblproduct.prod_id WHERE id_login = $search";
$Joined_records=mysqli_query($connection,$sql);
 while ($row=mysqli_fetch_assoc($Joined_records)){
    $output .='
        <tr>
            <td>'.$row["order_details_id"].'</td>
            <td>'.$row["order_id"].'</td>
            <td>'.$row["prod_id"].'</td>
            <td>'.$row["id_login"].'</td>
            <td>'.$row["quantity"].'</td>
            <td>'.$row["price_per_unit"].'</td>
            <td>'.$row["prod_name"].'</td>
            <td>'.$row["prod_descr"].'</td>
            <td>'.$row["prod_cat"].'</td>
            <td>'.$row["prod_price"].'</td>
            <td>'.$row["prod_quan"].'</td>
        </tr>
    ';

                                            }
    return $output;                                        
                                }
}

                }



if(isset($_POST["create_pdf"]))
{
require_once("tcpdf/tcpdf.php");
$obj_pdf = new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true,"UTF-8",false);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetTitle("product sales for a specific customerproduct sales for a 
specific customer");
$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('helvetica');
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT,'5',PDF_MARGIN_RIGHT);
$obj_pdf->SetPrintHeader(false);
$obj_pdf->SetPrintFooter(false);
$obj_pdf->SetAutoPageBreak(TRUE,10);
$obj_pdf->SetFont('helvetica',"",12);
$obj_pdf->AddPage();

$content="";
$content.='
    <h3 align="center"> product sales for a specific customer </h3>
    <table border="1" cellspacing="0" cellpadding="5">
        <tr>
            <th width=9%>order_details_id</th>
            <th width=9%>order_id</th>
            <th width=9%>product_id</th>
            <th width=9%>id_login</th>
            <th width=9%>quanitity</th>
            <th width=9%>price_per_unit</th>
            <th width=9%>prod_name</th>
            <th width=25%>prod_descr</th>
            <th width=9%>prod_cat</th>
            <th width=9%>prod_price</th>
            <th width=9%>prod_quan</th>
        </tr>
';

$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
$obj_pdf->Output("sample.pdf","I");



}
  ?>

<html>
<head>
    <title>product sales for a specific customer</title>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
        <h3 align="center"> Please Search for a specific customer you want to see sales for: </h3>
        <br />
        <form method="POST" action="index.php">
        <input type="text" name="txtNameSearch" />
        <input class="src_btn" type="submit" name="btnSearch" value="Search" />
        </form>
            <table width="800" border="1" cellpadding="1" cellspacing="1">
            <tr>
            <th>Order Details Id</th>
            <th>Order ID</th>
            <th>Product Id</th>
            <th>Login ID</th>
            <th>Quantity</th>
            <th>Product Price per unit</th>
            <th>Product Name</th>
            <th>Product Descrp</th>
            <th>Genre</th>
            <th>Price</th>
            <th>Quantity Sold</th>
            </tr> 
                <form method="post">
                <input type="submit" name="create_pdf" class="btn btn-
danger" value="create_pdf" />
            </form>





<?php
echo fetch_data();
?>

1 个答案:

答案 0 :(得分:0)

似乎您在PDF之前输出了一些内容。尝试将整个PHP代码移到顶部之前。另请参阅此完全相同的重复问题:Fatal error: Some data has already been output, can't send PDF file

编辑:现在您已将所有PHP代码放在最顶端,如果它仍然执行相同的操作,则表示PHP代码中存在警告或错误,但由于您的ob_start而无法看到它( )。因此,要查看正在发生的情况,请暂时注释掉ob_start();行,并在最顶部(第一行)放置error_log(E_ALL);行。同时在die();行之前添加$obj_pdf->Output("sample.pdf","I");行。执行脚本。如果您在页面上看到打印出来的任何内容(我的意思是一无所知),那么这就是问题所在,您应该修复显示的任何错误或警告,否则它将无法正常工作。