如何在静态 class 中使用 TCPDF?

How to use TCPDF within a static class?

我试图使一些静态属性显示为 pdf 的文本,如下所示,但我收到此错误消息:

TCPDF ERROR: 部分数据已经输出,无法发送PDF文件

非常感谢您的帮助!!

在 printPdf.php

class printPdf extends conn {
    private $text1;
    private $text2;

    public function setText1() {...}
    public function setText2() {...}
    public function callMtds() {
        $this->setText1();
        $this->setText2();
    }

    public function getText1() {
        return $this->text1;
    }
    public function getText2() {
        return $this->text2;
    }

}
$Nt = new printPdf();
$Nt->callMtds();

在 printPdfStatic.php

require_once 'printPdf.php';
class printPdfStatic extends printPdf {
    public static $text1;
    public static $text2;

    public function setAttr() {
        self::$text1 = $this->getText1();
        self::$text2 = $this->getText2();
    }
}
$Nt = new setAttr();

在 MYPDF.php

require_once('tcpdf.php');
class MYPDF extends TCPDF {
  //Page header
  public function Header() {
    // get the current page break margin
    $bMargin = $this->getBreakMargin();
    // get current auto-page-break mode
    $auto_page_break = $this->AutoPageBreak;
    // disable auto-page-break
    $this->SetAutoPageBreak(false, 0);
    // set background image
    $img_file = '../img/boleto_bancario.jpg';
    $this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
    // restore auto-page-break status
    $this->SetAutoPageBreak($auto_page_break, $bMargin);
    // set the starting point for the page content
    $this->setPageMark();
  }
}

在 printPdf_Control.php

require_once 'printPdfStatic.php';
require_once 'MYPDF.php';

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);



// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Victor Almeida');
$pdf->SetTitle('Teste PDF 01');
$pdf->SetSubject('Iniciando com o uso de PDFs');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);

// remove default footer
$pdf->setPrintFooter(false);

// set auto page breaks
//$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetAutoPageBreak(FALSE, 0);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/por.php')) {
    require_once(dirname(__FILE__).'/lang/por.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('times', 'b', 8);

// remove default header
$pdf->setPrintHeader(false);

// add a page
$pdf->AddPage();


// -- set new background ---

// get the current page break margin
$bMargin = $pdf->getBreakMargin();
// get current auto-page-break mode
$auto_page_break = $pdf->getAutoPageBreak();
// disable auto-page-break
$pdf->SetAutoPageBreak(false, 0);
// set bacground image
$img_file = '../img/boleto_bancario.jpg';
$pdf->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
// restore auto-page-break status
$pdf->SetAutoPageBreak($auto_page_break, $bMargin);
// set the starting point for the page content
$pdf->setPageMark();


// Print a text

$pdf->Text(108.0, 96.6, printPdfStatic::$text1);

$pdf->Text(2.0, 103.0, printPdfStatic::$text2);



// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('Teste_PDF_01.pdf', 'I');

当在输出 PDF 之前设置 header 时会发生这种情况,因此 header 对于 PDF 输出是错误的,因此会引发异常。

根据您的示例,您在 printPdfStatic.php 中有 cass 而不是 class。这可能会导致错误被包含在输出中,因此设置 headers。尝试更正此错误,看看是否能解决您的问题。

通常会发生这种情况,因为 PHP 生成的警告或通知在生成 PDF 之前在 运行 的代码中输出。如果这些问题得到解决,那么 PDF 应该可以正常渲染。

Edit 快速而肮脏的技巧是在使用 ob_clean(); 渲染 PDF 之前刷新输出缓冲区。这将至少获得 PDF 渲染,尽管没有解决潜在问题仍然存在的事实。