如何将HTML数据放入tcpdf的头文件?

时间:2022-08-18 09:49:32

I'm using the tcpdf library to generate the pdf document. I'm using smarty template engine to hold the data. Below is the script to put in the header data:

我正在使用tcpdf库生成pdf文档。我正在使用智能模板引擎来保存数据。下面是放入标题数据的脚本:

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'PQR', 
'XYZ');

I want to put HTML table content of smarty template in place of XYZ, the table content is going to be dynamic(meaning the data in table may vary for each PDF document).

我想将智能模板的HTML表格内容放在XYZ的位置,表格内容将是动态的(意味着表格中的数据可能因每个PDF文档而异)。

3 个解决方案

#1


18  

As @vinzcoco says, you must extend TCPDF to achieve what you want. Here is a simple improvement that I think it could be useful for you:

正如@vinzcoco所说,你必须扩展TCPDF才能达到你想要的效果。这是一个简单的改进,我认为它对您有用:

class MyTCPDF extends TCPDF {

    var $htmlHeader;

    public function setHtmlHeader($htmlHeader) {
        $this->htmlHeader = $htmlHeader;
    }

    public function Header() {
        $this->writeHTMLCell(
            $w = 0, $h = 0, $x = '', $y = '',
            $this->htmlHeader, $border = 0, $ln = 1, $fill = 0,
            $reseth = true, $align = 'top', $autopadding = true);
    }

}

Now, once you've got your MyTCPDF object available, you just need to do this to set the HTML header content:

现在,一旦你的MyTCPDF对象可用,你只需要这样做来设置HTML标题内容:

$mytcpdfObject->setHtmlHeader('<table>...</table>');

and the HTML content won't be hardcoded into the Header() method (more flexible for you).

并且HTML内容不会硬编码到Header()方法中(对您来说更灵活)。

#2


4  

I used the following method to set header

我使用以下方法设置标头

$PDF_HEADER_LOGO = "logo.png";//any image file. check correct path.
$PDF_HEADER_LOGO_WIDTH = "20";
$PDF_HEADER_TITLE = "This is my Title";
$PDF_HEADER_STRING = "Tel 1234567896 Fax 987654321\n"
. "E abc@gmail.com\n"
. "www.abc.com";
$pdf->SetHeaderData($PDF_HEADER_LOGO, $PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING);

This is work for me.

这对我有用。

#3


4  

You must instance your PDF class and extend the TCPDF class. After your PDF class should look like this:

您必须实例化PDF类并扩展TCPDF类。在你的PDF类看起来像这样:

class MyTCPDF extends TCPDF{
  public function Header(){
     $html = '<table>...</table>';
     $this->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'top', $autopadding = true);
  }
}

You must adapt this to your own project.

您必须将其调整为您自己的项目。

#1


18  

As @vinzcoco says, you must extend TCPDF to achieve what you want. Here is a simple improvement that I think it could be useful for you:

正如@vinzcoco所说,你必须扩展TCPDF才能达到你想要的效果。这是一个简单的改进,我认为它对您有用:

class MyTCPDF extends TCPDF {

    var $htmlHeader;

    public function setHtmlHeader($htmlHeader) {
        $this->htmlHeader = $htmlHeader;
    }

    public function Header() {
        $this->writeHTMLCell(
            $w = 0, $h = 0, $x = '', $y = '',
            $this->htmlHeader, $border = 0, $ln = 1, $fill = 0,
            $reseth = true, $align = 'top', $autopadding = true);
    }

}

Now, once you've got your MyTCPDF object available, you just need to do this to set the HTML header content:

现在,一旦你的MyTCPDF对象可用,你只需要这样做来设置HTML标题内容:

$mytcpdfObject->setHtmlHeader('<table>...</table>');

and the HTML content won't be hardcoded into the Header() method (more flexible for you).

并且HTML内容不会硬编码到Header()方法中(对您来说更灵活)。

#2


4  

I used the following method to set header

我使用以下方法设置标头

$PDF_HEADER_LOGO = "logo.png";//any image file. check correct path.
$PDF_HEADER_LOGO_WIDTH = "20";
$PDF_HEADER_TITLE = "This is my Title";
$PDF_HEADER_STRING = "Tel 1234567896 Fax 987654321\n"
. "E abc@gmail.com\n"
. "www.abc.com";
$pdf->SetHeaderData($PDF_HEADER_LOGO, $PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING);

This is work for me.

这对我有用。

#3


4  

You must instance your PDF class and extend the TCPDF class. After your PDF class should look like this:

您必须实例化PDF类并扩展TCPDF类。在你的PDF类看起来像这样:

class MyTCPDF extends TCPDF{
  public function Header(){
     $html = '<table>...</table>';
     $this->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'top', $autopadding = true);
  }
}

You must adapt this to your own project.

您必须将其调整为您自己的项目。