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

3
我正在使用这个网站作为参考:http://www.ros.co.nz/pdf/ 我阅读了readme.pdf,但没有找到任何指示如何在pdf的每一页中添加页眉和页脚的功能。
3个回答

5

您可以通过打开对象、创建内容、关闭对象,然后将对象添加到PDF中来完成此操作。请参见当前(2021年)文档的第18-19页(PDF第21-22页)相关类方法。请注意,保留HTML标签。

以下是一个简单的示例:

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

非常感谢您提供的解决方案。我想补充一下,如果您添加的页脚或页眉高度较大,可以使用“$pdf->ezSetMargins()”来避免与正文重叠。 - Marco Sulla

1

90%的时间使用BrianS的解决方案。

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

例如,对于带有撕下汇款标签的收据,像这样的东西适合我:

$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');
    }
}

这将确保您的内容出现在最后一页的底部。

要插入内容,您可能需要使用 openObjectcloseObject,以便只有插入操作在 while 循环期间重新执行。


-4

长时间编辑后:

最终借助this的示例,添加关于最新版本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']);

?>

使用dompdf怎么样:

尝试以下代码用于页眉和页脚:

您可以使用PDF“对象”向每个页面添加图像和形状(线条、矩形等)。 PDF对象捕获所有呈现命令,作为一种模板,然后可以添加到多个页面中:

<script type="text/php">

if ( isset($pdf) ) {

  // Open the object: all drawing commands will
  // go to the object instead of the current page
  $footer = $pdf->open_object();

  $w = $pdf->get_width();
  $h = $pdf->get_height();

  // Draw a line along the bottom
  $y = $h - 2 * $text_height - 24;
  $pdf->line(16, $y, $w - 16, $y, $color, 1);

  // Add an initals box
  $font = Font_Metrics::get_font("helvetica", "bold");
  $text = "Initials:";
  $width = Font_Metrics::get_text_width($text, $font, $size);
  $pdf->text($w - 16 - $width - 38, $y, $text, $font, $size, $color);
  $pdf->rectangle($w - 16 - 36, $y - 2, 36, $text_height + 4, array(0.5,0.5,0.5), 0.5);

  // Add a logo
  $img_w = 2 * 72; // 2 inches, in points
  $img_h = 1 * 72; // 1 inch, in points -- change these as required
  $pdf->image("print_logo.png", "png", ($w - $img_w) / 2.0, $y - $img_h, $img_w, $img_h);

  // Close the object (stop capture)
  $pdf->close_object();

  // Add the object to every page. You can
  // also specify "odd" or "even"
  $pdf->add_object($footer, "all");
}

</script>

我实际上正在使用R&OS有限公司的免费PHP PDF创建库(http://php.find-info.ru/php/014/webdbapps2-CHP-13-SECT-2.html),所以我想知道是否可以在使用此库的同时创建页眉和页脚。 - anonymous123
关于R&OS PDF类的答案,请写下来。问题是关于它的。 - AH.
RIP示例链接。 - Jatin Dhoot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接