我使用TCPDF生成pdf报告。我需要自定义页眉和页脚,因此我扩展了原始类以覆盖官方文档(https://tcpdf.org/examples/example_002.phps)中建议的页眉和页脚方法。
您在这里是代码:
class AppPdf extends \TCPDF {
CONST LOGO_PATH = '/../../../public_html/public/images/logo-big.png';
private $xLogo;
private $yLogo;
private $wLogo;
public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false, $xLogo = 8, $yLogo = 0, $wLogo = 50) {
parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
$this->xLogo = $xLogo;
$this->yLogo = $yLogo;
$this->wLogo = $wLogo;
}
public function Header() {
$this->Image(__DIR__ . self::LOGO_PATH, $this->xLogo, $this->yLogo, $this->wLogo);
}
public function Footer() {
$this->SetXY(34,260);
$this->SetFont('Helvetica', 'I', 8);
$this->SetTextColor(0, 0, 0);
$this->MultiCell(130, 20, "footer text", 0, "C", false);
}
}
然后我有一个基本模板,它用于所有生成的文档:
class BasePdf {
CONST CREATOR = 'Creator';
CONST TITLE = 'Title';
CONST PDF_FONT_NAME_MAIN = 'Times';
CONST PDF_FONT_SIZE_MAIN = 11;
protected $pdf;
public function __construct($xLogo = 8, $yLogo = 0, $wLogo = 50)
{
$this->pdf = new AppPdf('P', 'mm', 'A4', true, 'UTF-8', false, false, $xLogo, $yLogo, $wLogo);
$this->pdf->SetCreator(self::CREATOR);
$this->pdf->SetAuthor(self::CREATOR);
$this->pdf->SetTitle(self::TITLE);
$this->pdf->SetFont(self::PDF_FONT_NAME_MAIN, "", self::PDF_FONT_SIZE_MAIN);
}
public function getPdf()
{
return $this->pdf;
}
}
使用基本模板,如以下类所示:
use AppBundle\Entity\HPVExam;
class HPVReport extends BasePdf
{
public function __construct(HPVExam $HPVExam)
{
parent::__construct(8, 10, 75);
$this->pdf->AddPage();
}
}
问题是这段代码会生成pdf,顶部有一条恼人的水平线,页脚有另一条水平线,如下图所示。
我已经尝试过这里提供的建议PHP / TCPDF: Template Bug?和Changing or eliminating Header & Footer in TCPDF,但没有运气。
我做错了什么?似乎原始的页眉和页脚方法没有被正确覆盖......任何想法?谢谢!
答案 0 :(得分:0)
只要对TCPDF说不要打印标题或去修改源....
$pdf->SetPrintHeader(false);