【问题标题】:generating pdf using tcpdf library使用 tcpdf 库生成 pdf
【发布时间】:2020-03-03 09:04:54
【问题描述】:

我正在尝试使用 tcpdf 库从数据库数据生成 pdf,但不幸的是它似乎没有成功。我不知道我哪里出错了。有人好心帮忙。 这是我打算将表中的数据输出到pdf的代码。该代码还生成了一个运行良好的表格,但是当涉及到 pdf 时,它会带来一个空白图像,如文本中所示。我想知道我哪里出错了。

<?php
ob_start();
ini_set('display errors', 1);
error_reporting('0');
session_start();
if (!isset($_SESSION['name'])) {
    header('Location:login.php');
} elseif (isset($_SESSION['name'])) {
    $cuser = $_SESSION['name'];
}
require 'logis.php';

if(isset($_POST["generate_pdf"]))  
 {  
     //get data into pdf
function fetch_data()
{
    $year = $_POST['year'];
  $month = $_POST['month'];
    $output = '';
    // $output.='<tr><td>Youuuuuuu<td></tr>';
    include 'includes/config.php';
    $sql = "SELECT DISTINCT item_name FROM SALES WHERE MONTH(date_sold)='$month' AND YEAR(date_sold)='$year'";
    $query = mysqli_query($conn, $sql);
    $sales = mysqli_fetch_all($query, MYSQLI_ASSOC);
    foreach ($sales as $sale) {
        $item = $sale['item_name'];

        $sql1 = "SELECT SUM(total_price) AS totalsum,SUM(quantity_sold) AS totalsold  FROM sales WHERE item_name='$item' AND MONTH(date_sold)='$month' AND YEAR(date_sold)='$year' ";
        $result1 = mysqli_query($conn, $sql1);
        $row = mysqli_fetch_assoc($result1);
        $sumsold = $row['totalsold'];
        $sumprice = $row['totalsum'];
        $output .= '<tr><td>' . $item . '</td>
           <td>' . $sumsold . '</td>
           <td>' . $sumprice . '</td>
           </tr>';
    }
    $resultsum = mysqli_query($conn, "SELECT SUM(total_price) AS totalsum FROM sales WHERE MONTH(date_sold)='$month' AND YEAR(date_sold)='$year'");
    $rowsum = mysqli_fetch_assoc($resultsum);
    $sum = $rowsum['totalsum'];
    $output .= '<tr><td colspan="2" style="text-align:center;">Total Amount: </td>
         <td colspan="1" style="text-decoration:bold;color:blue;">' . $sum . '</td></tr>';
         echo $output;
    return $output;
}
      require_once('includes/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("Generate monthly summary");  
      $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, '10', PDF_MARGIN_RIGHT);  
      $obj_pdf->setPrintHeader(false);  
      $obj_pdf->setPrintFooter(false);  
      $obj_pdf->SetAutoPageBreak(TRUE, 10);  
      $obj_pdf->SetFont('helvetica', '', 11);  
      $obj_pdf->AddPage();  
      $content = '';  
      $content .= '  
      <h4 align="center">MONTHLY SUMMARY </h4><br /> 
      <table border="1" cellspacing="0" cellpadding="3">  
           <tr>  
                <th width="40%">Item Name</th>  
                <th width="30%">Amount Sold(Kgs)</th>  
                <th width="30%">Total Price(Kshs)</th>  
           </tr>  
      ';  
      $content .= fetch_data();  
      $content .= '</table>';  
      $obj_pdf->writeHTML($content);
      ob_end_clean(); 
      $obj_pdf->Output('summary.pdf', 'I');  
 } 

here is the output

【问题讨论】:

    标签: php tcpdf


    【解决方案1】:

    你为什么使用ob_start()?可能这是导致错误的原因。在下面的代码中,我删除了ob function callsfetch_data() 函数中$output 的回声。请尝试一下,如果出现错误,请显示。如果这不起作用,请展示您如何仅以正确的结果执行表格。

    error_reporting('E_ALL');
    session_start();
    if (!isset($_SESSION['name'])) {
        header('Location: login.php');
    } elseif (isset($_SESSION['name'])) {
        $cuser = $_SESSION['name'];
    }
    require 'logis.php';
    
    if(isset($_POST["generate_pdf"]))  
     {  
         //get data into pdf
    function fetch_data()
    {
        $year = $_POST['year'];
        $month = $_POST['month'];
        $output = '';
        include 'includes/config.php';
        $sql = "SELECT DISTINCT item_name FROM SALES WHERE MONTH(date_sold)='$month' AND YEAR(date_sold)='$year'";
        $query = mysqli_query($conn, $sql);
        $sales = mysqli_fetch_all($query, MYSQLI_ASSOC);
        foreach ($sales as $sale) {
            $item = $sale['item_name'];
            $sql1 = "SELECT SUM(total_price) AS totalsum,SUM(quantity_sold) AS totalsold  FROM sales WHERE item_name='$item' AND MONTH(date_sold)='$month' AND YEAR(date_sold)='$year' ";
            $result1 = mysqli_query($conn, $sql1);
            $row = mysqli_fetch_assoc($result1);
            $sumsold = $row['totalsold'];
            $sumprice = $row['totalsum'];
            $output .= '<tr><td>' . $item . '</td><td>' . $sumsold . '</td><td>' . $sumprice . '</td></tr>';
        }
        $resultsum = mysqli_query($conn, "SELECT SUM(total_price) AS totalsum FROM sales WHERE MONTH(date_sold)='$month' AND YEAR(date_sold)='$year'");
        $rowsum = mysqli_fetch_assoc($resultsum);
        $sum = $rowsum['totalsum'];
        $output .= '<tr><td colspan="2" style="text-align:center;">Total Amount: </td><td colspan="1" style="text-decoration:bold;color:blue;">' . $sum . '</td></tr>';
    
        return $output;
    }
          require_once('includes/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("Generate monthly summary");  
          $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, '10', PDF_MARGIN_RIGHT);  
          $obj_pdf->setPrintHeader(false);  
          $obj_pdf->setPrintFooter(false);  
          $obj_pdf->SetAutoPageBreak(TRUE, 10);  
          $obj_pdf->SetFont('helvetica', '', 11);  
          $obj_pdf->AddPage();  
          $content = '';  
          $content .= '  
          <h4 align="center">MONTHLY SUMMARY </h4><br /> 
          <table border="1" cellspacing="0" cellpadding="3">  
               <tr>  
                    <th width="40%">Item Name</th>  
                    <th width="30%">Amount Sold(Kgs)</th>  
                    <th width="30%">Total Price(Kshs)</th>  
               </tr>  
          ';  
          $content .= fetch_data();  
          $content .= '</table>';  
          $obj_pdf->writeHTML($content);
          $obj_pdf->Output('summary.pdf', 'I');  
     } 
    

    【讨论】:

      猜你喜欢
      • 2016-10-06
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 2015-02-26
      相关资源
      最近更新 更多