Sachin Endait
Sachin Endait

Reputation: 135

Why lines/images draw only on last page of pdf using dompdf in php

I am trying to draw line/image on every page of pdf using dompdf but it starts from second page, why this is so ? anyone has any idea ?
Here is my code

$dompdf = new DOMPDF();
$dompdf->load_html($message2);
$dompdf->set_paper('a4','portrait');
$dompdf->render();

$canvas = $dompdf->get_canvas();

//For Header
$header = $canvas->open_object();
$canvas->image($header_image1,'jpg',0, 0, 595, 100);
$canvas->line(0,100,595,100,array(0,0,0),1);
$canvas->close_object();
$canvas->add_object($header, "all");

//For Footer
$footer = $canvas->open_object();
$canvas->line(0,740,650,740,array(0,0,0),1);
$canvas->image($footer_image1,'jpg',0, 742, 595, 100);
$canvas->close_object();
$canvas->add_object($footer, "all");

$output = $dompdf->output();

this code draw line/image on pdf but it only display on last page.
I have two pages in pdf and my line/images are drawn on second page not on first page.

Please suggest any solution.

Upvotes: 2

Views: 2968

Answers (3)

Peter
Peter

Reputation: 11

The line is not displayed for me because embedded php was disabled. This solved the problem:

$dompdf->set_option("isPhpEnabled", true);

This line has to be placed before $dompdf->render().

Upvotes: 1

David van Driessche
David van Driessche

Reputation: 7046

Adding objects in DomPDF works from the current page onwards. In other words, your objects will get added, but only from the page you currently have and then onwards to any new pages you add.

In your code, you've already converted your HTML to PDF, so the current page is more than likely the last page in your document. So your header / footer are added there, but not to any previous pages.

To place content on every page, domPDF provides two methods: page_text and page_script.

In your case the following type of code should do the trick:

$canvas->page_script('
  $pdf->line(10,730,800,730,array(0,0,0),1);
');

Code in the page_script function is then executed for every PDF page.

Upvotes: 2

gopi
gopi

Reputation: 1

how to add image in all pdf page using header and footer.below is my code.

$pdf = App::make('dompdf');
$pdf->loadFile('invoice.html');
$pdf->output();
$dom_pdf = $pdf->getDomPDF();
$canvas = $dom_pdf ->get_canvas();
$image1="logo.png";
$canvas->image($image1,'png', 0, 0, 50, 25);
$canvas->page_text(10, 10, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, array(0, 0, 0));
$pdf->save('pdf_report/eft_payment-'.$RandomAccountNumber.'.pdf');

Upvotes: 0

Related Questions