6

I am generating a html2pdf using Dompdf and my code is

$html='<div>--content of pdf--</div>';
require_once('resources/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
$dompdf->stream("transaction.pdf");

My pdf generated successfully now i want to add image in header of that pdf so can any one tell me how can i insert header and footer for Dompdf and specially where to put the header code?

nitin kachhadiya
  • 959
  • 2
  • 9
  • 21
  • 1
    possible duplicate of [Header in PDF page using DOMPDF in PHP](http://stackoverflow.com/questions/7484318/header-in-pdf-page-using-dompdf-in-php) – Foobar Sep 02 '13 at 10:35
  • Not sure why you're spamming a whole bunch of non-answers to this question half a year later. But please don't do that. – BoltClock Apr 26 '14 at 08:11

1 Answers1

5

From dompdf FAQ

Q: How can I add an image to a header or footer on every page? A: 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>

EDIT :

Here are step by step instructions:

Somewhere in your html file, near the top, open a script tag with a "text/php" type:

<script type="text/php">

Check if the $pdf variable is set. dompdf sets this variable when evaluating embedded PHP.

<script type="text/php">

if ( isset($pdf) ) {
Foobar
  • 196
  • 1
  • 8
  • where to put this script? before generating the pdf or after generating pdf. can we put this script within php tag? – nitin kachhadiya Sep 02 '13 at 10:38
  • Note that if using inline script it currently needs to reside in the *body* of the HTML. Because of how the document is parsed you'll at the very least need to format your HTML as `
    --content of pdf--
    `. Though if you can, just create your header/footer using HTML+CSS.
    – BrianS Sep 03 '13 at 17:32
  • Where is this $text_height data was coming from your code? my compiler says undefined variable. – Suvra.D Aug 24 '23 at 10:52