【问题标题】:How to insert data into a pdf using TCPDF如何使用 TCPDF 将数据插入 pdf
【发布时间】:2017-05-19 13:56:56
【问题描述】:

我正忙于尝试使用 TCPDF 创建 pdf,但 pdf 中没有显示任何内容,唯一的内容是表格的标题。所以搜索后调用的数据都不会显示。注意,我还在两个表之间使用了连接。

<?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();
?>

【问题讨论】:

    标签: php html tcpdf


    【解决方案1】:

    您似乎在 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); 行放在最顶部(在第一行)。还要在$obj_pdf-&gt;Output("sample.pdf","I"); 行之前放置一个die(); 行。执行脚本。如果您在页面上看到打印出任何内容(我的意思是所有内容),那么这就是问题所在,您应该修复显示的任何错误或警告,否则它将永远无法工作。

    【讨论】:

    • 请注意我改写了这个问题
    • 这个@MOHAMMEDAMEEN 有什么更新吗?如果我不理解您的问题,请清理并重新表述您的问题,尽管您最初的问题似乎已经改变。
    猜你喜欢
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2017-12-19
    • 2017-02-26
    • 1970-01-01
    相关资源
    最近更新 更多