php 如何将 HTML 数据放入 tcpdf 的标题中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14495688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put HTML data into header of tcpdf?
提问by PHPLover
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 文档。我正在使用 smarty 模板引擎来保存数据。以下是放入标题数据的脚本:
// 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).
我想用 smarty 模板的 HTML 表格内容代替 XYZ,表格内容将是动态的(意味着表格中的数据可能因每个 PDF 文档而异)。
回答by Arturo
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:
现在,一旦您的 MyTCPD 对象可用,您只需要这样做来设置 HTML 标题内容:
$mytcpdfObject->setHtmlHeader('<table>...</table>');
and the HTML content won't be hardcoded into the Header()method (more flexible for you).
并且 HTML 内容不会被硬编码到Header()方法中(对您来说更灵活)。
回答by vijay
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 [email protected]\n"
. "www.abc.com";
$pdf->SetHeaderData($PDF_HEADER_LOGO, $PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING);
This is work for me.
这对我来说是工作。
回答by vinzcoco
You must instance your PDF class and extend the TCPDF class. After your PDF class should look like this:
您必须实例化您的 PDF 类并扩展 TCPPDF 类。在你的 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.
您必须将其调整到您自己的项目中。

