如何在静态类中使用TCPDF?

时间:2015-07-16 17:17:43

标签: php static tcpdf

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

TCPDF错误:某些数据已经输出,无法发送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');

1 个答案:

答案 0 :(得分:0)

在输出PDF之前设置标题时会发生这种情况,因此PDF输出的标题错误,因此会引发异常。

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

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

编辑快速而肮脏的技巧是在使用ob_clean();呈现PDF之前刷新输出缓冲区。这将至少得到PDF渲染,虽然不能解决基础问题仍然存在的事实。