如何在php中创建的PDF文件中添加页眉和页脚

如何在php中创建的PDF文件中添加页眉和页脚,php,pdf,pdf-generation,Php,Pdf,Pdf Generation,我使用此网站作为参考: 我阅读了readme.pdf,但还没有找到任何函数来指导如何在pdf的每个页面中添加页眉和页脚 经过长时间编辑: 最后,借助示例添加关于最新版本的R&OS PDF的答案 <?php include 'path/to/Cezpdf.php'; $pdf = new Cezpdf('a4', 'portrait', 'none', null); $all = $pdf->openObject(); $pdf->saveState(); // head

我使用此网站作为参考:


我阅读了readme.pdf,但还没有找到任何函数来指导如何在pdf的每个页面中添加页眉和页脚

经过长时间编辑:

最后,借助示例添加关于最新版本的R&OS PDF的答案

<?php

include 'path/to/Cezpdf.php';

$pdf = new Cezpdf('a4', 'portrait', 'none', null);

$all = $pdf->openObject();
$pdf->saveState();

// header line and text
$pdf->addText(20, 800, 14, 'This is header text');
$pdf->line(20, 790, 580, 790);

// footer line and text
$pdf->line(20, 40, 578, 40);
$pdf->addText(20, 30, 8, 'Left side header text');
$pdf->addText(580, 30, 8, 'Right side header text', 0, 'right');

$pdf->restoreState();
$pdf->closeObject();

$pdf->addObject($all,'all');

$pdf->ezSetMargins(100, 100, 50, 50);

// content text
$text = str_repeat("This is your content.\n", 100);
$pdf->ezText($text, 0, ['justification' => 'full']);

// output
$pdf->ezStream(['Content-Disposition' => 'mypdf.pdf']);

?>


如何使用:

在页眉和页脚处尝试此操作:

您可以向每个页面添加图像和形状(直线、矩形等) 使用PDF“对象”。PDF对象将所有渲染命令捕获为 然后可以添加到多个页面的模板类型:


如果(isset($pdf)){
//打开对象:所有图形命令都将
//转到对象而不是当前页面
$footer=$pdf->open_object();
$w=$pdf->get_width();
$h=$pdf->获取高度();
//沿底部画一条线
$y=$h-2*$text\U height-24;
$pdf->line(16,$y,$w-16,$y,$color,1);
//添加初始值框
$font=font_度量:获取字体(“helvetica”、“bold”);
$text=“缩写:”;
$width=Font\u度量::获取文本宽度($text,$Font,$size);
$pdf->text($w-16-$width-38,$y,$text,$font,$size,$color);
$pdf->矩形($w-16-36,$y-2,36,$text\U height+4,数组(0.5,0.5,0.5),0.5);
//添加一个标志
$img_w=2*72;//2英寸,以点为单位
$img_h=1*72;//1英寸,以点为单位--根据需要更改这些点
$pdf->image(“print_logo.png”、“png”、($w-$img_w)/2.0、$y-$img_h、$img_w、$img_h);
//关闭对象(停止捕获)
$pdf->close_object();
//将对象添加到每个页面。您可以
//同时指定“奇数”或“偶数”
$pdf->add_对象($footer,“all”);
}

您应该能够通过打开一个对象、创建内容、关闭该对象,然后将该对象添加到PDF中来完成此操作。有关类方法的相关信息,请参见当前(2021)文档的(PDF第21-22页)

一个简单的例子:

<?php
include ('class.ezpdf.php');
$pdf =& new Cezpdf();
$pdf->selectFont('fonts/Helvetica.afm');

$footer = $pdf->openObject();
$pdf->addText(50, 50, 8, "some footer text");
$pdf->line(50,60,562,60);
$pdf->closeObject();
$pdf->addObject($footer, "all");

$pdf->ezText('Hello World!',50);
$pdf->ezStream();
?>

90%的时间使用Brian的解决方案

如果您不总是知道内容的高度,页脚可能会复杂得多

例如,对于带有撕下汇款标签的收据,类似的内容对我适用:

$ok = 0;
$offset = (0 - $pdf->y);
while (!$ok) {
    $thisPageNum = $pdf->ezPageCount;
    $pdf->transaction('start');

    $offset = $offset + 1;
    $this->ezSetDy($offset);

    // Add your content here

    if ($this->ezPageCount==$thisPageNum) {
        $this->transaction('commit');
        $ok=1;
    } else {
        $this->transaction('rewind');
    }
}
这将确保您的内容显示在最后一页的底部


对于插入内容,您可能需要使用
openObject
closeObject
,这样在
while
循环过程中只需重新插入内容。

我实际上使用的是R&OS Ltd的免费PHP PDF创建库(),所以我想知道我是否可以在仍然使用该库的情况下创建页眉和页脚。编写关于R&OS PDF类的答案。问题是关于itRIP示例链接。非常感谢您提供的解决方案。我想补充一点,如果以这种方式添加的页脚或页眉高度较大,可以使用
$pdf->ezSetMargins()
来避免与正文文本重叠。
$ok = 0;
$offset = (0 - $pdf->y);
while (!$ok) {
    $thisPageNum = $pdf->ezPageCount;
    $pdf->transaction('start');

    $offset = $offset + 1;
    $this->ezSetDy($offset);

    // Add your content here

    if ($this->ezPageCount==$thisPageNum) {
        $this->transaction('commit');
        $ok=1;
    } else {
        $this->transaction('rewind');
    }
}