【问题标题】:TCPDF and database: conflicts in PHPTCPDF 和数据库:PHP 中的冲突
【发布时间】:2014-05-14 23:11:47
【问题描述】:

嗨,这是我的 php 代码:

<?php require_once('tcpdf/config/lang/eng.php'); ?>
    <?php require_once('tcpdf/tcpdf.php'); ?>
    <?php include_once("class/class_productos.php"); ?>
    <?php include_once("class/class_clientes.php"); ?>
    <?php include_once("class/class_img_gen.php"); ?>
    <?php include_once("class/class_acros.php"); ?>   // here is MY DB CONNECTION
    <?php 

    class MYPDF extends TCPDF {
        //Page header
        public function Header() {
            $auto_page_break = $this->AutoPageBreak;
            $this->SetAutoPageBreak(false, 0);
            $img_file = 'img/pdf_fondo.jpg';
            $this->Image($img_file, $x=0, $y=0, $w=210, $h=297, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0);
            $this->SetAutoPageBreak($auto_page_break);
        }
    }

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetCreator('ACROS');
    $pdf->SetAuthor('ACROS');
    $pdf->SetTitle('Lista de producto');
    $pdf->SetSubject('Lista de producto');
    $pdf->SetKeywords('ACROS, acros, mayorista, informática');
    $pdf->setPrintHeader(true);
    $pdf->setPrintFooter(false);
    $pdf->SetMargins(0, 0, 0);
    $pdf->SetAutoPageBreak(FALSE, PDF_MARGIN_BOTTOM);
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 
    $pdf->setLanguageArray($l); 
    $pdf->AddPage();

    $category = $_GET['c'];
    $getCategory = "SELECT * FROM prod_detalle WHERE fk_marca = '$category'";
    $getCategory = mysql_query($getCategory);
    $count = mysql_num_rows($getCategory);

    $txt = "result ".$count;
    // output the HTML content
    $pdf->writeHTML($txt, true, 0, true, 0);
    $pdf->SetY(-30);
    $pdf->SetX(0.65);
    $pdf->MultiCell(20, 0, $txtB, $border = 0,$align = 'L',$fill = 0,$ln = 1,$x = '',$y = '',$reseth = false, $stretch = 0, $ishtml = true, $autopadding = false, $maxh = 0);
    $pdf->Output('lista.pdf', 'I');
    ?>

我收到这两个警告:

警告:mysql_num_rows() 期望参数 1 是资源,布尔值 在 /mnt/futurehome/netlogiq/public_html/acros/lista_pdf.php 中给出 第 64 行

警告:无法修改标头信息 - 标头已由 (输出开始于 /mnt/futurehome/netlogiq/public_html/acros/lista_pdf.php:64)在 /mnt/futurehome/netlogiq/public_html/acros/tcpdf/tcpdf.php 上线 5405 TCPDF ERROR: 一些数据已经输出到浏览器,不能 发送PDF文件

谁能帮我解决这个问题??如果在 phpmyadmin 中运行查询,它会返回想要的数据。所以查询工作正常!

【问题讨论】:

  • 由于第一个警告,您收到了第二个警告。您的问题在 db 文件 class_acros.php 中,因此它与 MySQL 有关。

标签: tcpdf


【解决方案1】:

只需删除该行

require_once('tcpdf/config/lang/eng.php');

编辑 tcpdf 文件夹中的 tcpdf.php 文件: 添加行 ob_end_clean(); 如下(倒数第三行):

public function Output($name='doc.pdf', $dest='I') {
    //LOTS OF CODE HERE....}
    switch($dest) {
        case 'I': {
        // Send PDF to the standard output
        if (ob_get_contents()) {
        $this->Error('Some data has already been output, can\'t send PDF file');}
        //some code here....}
            case 'D': {         // download PDF as file
        if (ob_get_contents()) {
    $this->Error('Some data has already been output, can\'t send PDF file');}
            break;}
        case 'F':
        case 'FI':
        case 'FD': {
            // save PDF to a local file
                 //LOTS OF CODE HERE.....       break;}
        case 'E': {
            // return PDF as base64 mime email attachment)
        case 'S': {
            // returns PDF as a string
            return $this->getBuffer();
        }
        default: {
            $this->Error('Incorrect output destination: '.$dest);
        }
    }
           ob_end_clean(); //add this line here 
    return '';
}

【讨论】:

    【解决方案2】:

    您从mysql_num_rows() 收到错误的原因是您尚未初始化与数据库的连接。您还在使用旧的(并且已弃用为 og PHP 5.5)MySQL 接口 - 您应该考虑使用 mysqli 或 PDO。

    关于如何初始化与数据库的连接并使用正确的资源句柄与其通信的完整说明,IMO 超出了 SO 答案的范围。也许从这里开始:

    http://www.php.net/manual/en/mysqli.quickstart.connections.php

    您收到第二个错误的原因仅仅是因为在您致电 $pdf-&gt;Output() 之前,第一条错误消息已发送到浏览器。一旦您的数据库连接正常工作并删除错误消息,问题就会消失。

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多