doudi5892
2015-07-16 17:17
浏览 56
已采纳

如何在静态类中使用TCPDF?

I was trying to make some static attributes to appear as the text of the pdf as follows, but I'm getting this error message:

TCPDF ERROR: Some data has already been output, can't send PDF file

I really do appreciate some help !!

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

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

On 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();
  }
}

On 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条回答 默认 最新

  • doushibu2453 2015-07-16 17:46
    已采纳

    This occurs when a header is set before the PDF is output, so the headers are wrong for PDF output and hence the exception is raised.

    By your example you have cass instead of class in printPdfStatic.php. This will likely cause an error to be rendered into the output when it's included hence setting headers. Try correcting this error and see if that sorts your issue.

    Typically this will happen because of a warning or notice generated by PHP being output in code which is run before the PDF is generated. If these problems are fixed then the PDF should render fine.

    Edit quick and dirty trick is to flush the output buffer just before rendering the PDF with ob_clean();. This will atleast get the PDF rendering although does not fix the fact that at underlying issue will remain.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题