1

On the dompdf FAQ page, they give this example:

<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>

I tried putting HTML in text but it didn't work. Is there a way to insert HTML with add_object?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

2 Answers2

1

There is not. When you use inline script you are accessing the back-end PDF library directly. You only have access to what the library allows. The two supported libraries (CPDF and PDFLib) do not support working with HTML.

That's not to say you can't achieve the same result in other ways. THe 0.6.0 release (currently at beta 2) supports fixed-position elements, which are rendered on every page in the specified location.

See a sample in this answer.

Community
  • 1
  • 1
BrianS
  • 13,284
  • 15
  • 62
  • 125
  • The problem is that I want to add a different header to the first page than the rest of the pages. `position:fixed` adds it to every page. I can achieve the desired effect with `add_object(...,'add')` and 'nexteven' and 'nextodd' if I throw everything at the start of the script, but I need HTML... – mpen Nov 10 '11 at 17:19
-1

Can't you just use:

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
samura
  • 4,375
  • 1
  • 19
  • 26