How to add Header and Footer in the created PDF file in php
Get the solution ↓↓↓Solution:
You should be able to do this by opening an object, creating your content, closing the object, and then adding the object to your PDF. See pages 18-19 (PDF pages 21-22) of the current (2021) documentation for the relevant class methods.
A brief example:
<?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();
?>
Answer
Solution:
90% of the time use BrianS's solution.
Footers can be a good deal more complicated if you don't always know the height of the content.
For a receipt with a tear-off remittance label, for example, something like this works for me:
$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');
}
}
This will make sure your content appears at the bottom of the last page.
For inserting content, you may want to useopenObject
andcloseObject
so only the insertion is redone during thewhile
loop.
Answer
Solution:
Edited after long time:
Finally adding answer about latest version of R&OS PDF with the help of this example.
<?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']);
?>
What about using dompdf:
Try this for header and footer:
You can add images and shapes (line, rectangles, etc.) to every page using PDF 'objects'. A PDF object captures all rendering commands as a sort of template that can then be added to multiple pages:
<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>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.