Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dompdf draw line on every page

I'm trying to generate nice pdf from html file with Dompdf but I'm unable to draw a line on every page. Here is an exemple of the php file.

<?php 
require_once("dompdf/dompdf_config.inc.php");

$html = file_get_contents("test.txt");
$html = utf8_decode($html);

$dompdf = new DOMPDF();
$dompdf->load_html($html);  

$dompdf->render();

$font = Font_Metrics::get_font("helvetica", "bold");
$canvas = $dompdf->get_canvas();
$footer = $canvas->open_object();

    $canvas->line(10,730,800,730,array(0,0,0),1);
    $canvas->page_text(555, 750, "{PAGE_NUM}/{PAGE_COUNT}",
                   $font, 10, array(0,0,0));

$canvas->close_object();
$canvas->add_object($footer, "all");

$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
?>

This produce a pdf where each page contain the page number, but only the last page have a line.

So how can i draw line/images on every page with Dompdf ?

like image 517
elnabo Avatar asked Jun 17 '13 08:06

elnabo


2 Answers

Certainly we recommend using HTML+CSS where possible, and your above answer does that. You're missing one thing, which is the page number. You can get the page number on each page by using CSS counters. e.g.

<style>
#header { position: fixed; border-bottom:1px solid gray;}
#footer { position: fixed; border-top:1px solid gray;} .pagenum:before { content: counter(page); } </style>

<div id="header">
  <img src="logo.gif" style="margin-top:10px;"/>
</div>
<div id="footer">
  Page <span class="pagenum"></span>
</div>

One value you can't access yet via CSS is the total number of pages. So for that you will still need to use script-generated text.

First, a little explanation. Most of the direct access methods add content only to the current page (actually, currently active object ... which is normally a page). So drawing a line or shape, adding an image, or adding text using the text() method all draw to a single page. You can get around that restriction by rendering to a detached object which you then add to each page (open_object()/close_object()/add_object()). It's important to note that detached objects are generated starting with the current page (i.e. all previous pages will not see the object) which is why you generally want to work with objects via inline script.

The page_text() method is different. It is specifically designed to add content to all pages after the PDF has been rendered. It works outside and separate from the detached object container. You have similar functionality in the page_script() method, which handles script in a manner similar to how page_text() handles text.

All this is to say that based on the sample in your question your code is probably not doing what you think it is.

Second, a warning. You should not use utf8_decode() unless you intend for your text to only be encoded with iso-8859-1 start to finish. utf8_decode() causes a lossy conversion of utf8 into iso-8859-1. dompdf is fully able to work with utf8-encoded text (as of v0.6.0) and will handle any necessary conversions. You should familiarize yourself with the information in the Unicode How-To, since there are some requirements related to your PHP/dompdf configuration.

With all this information in mind you could use your original code with some minor modifications:

<?php 
require_once("dompdf/dompdf_config.inc.php");

$html = file_get_contents("test.txt");

$dompdf = new DOMPDF();
$dompdf->load_html($html);  
$dompdf->render();

$font = Font_Metrics::get_font("helvetica", "bold");
$canvas = $dompdf->get_canvas();
$canvas->page_text(555, 750, "{PAGE_NUM}/{PAGE_COUNT}", $font, 10, array(0,0,0));
$canvas->page_script('
  // $pdf is the variable containing a reference to the canvas object provided by dompdf
  $pdf->line(10,730,800,730,array(0,0,0),1);
');

$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
?>
like image 97
BrianS Avatar answered Oct 30 '22 20:10

BrianS


Solved using html/css. Not optimal but at least it works.

  <div id="header">
    <img src="logo.gif" style="margin-top:10px;"/>
  </div>
  <div id="footer">
    Footer
  </div>

 #header { position: fixed; border-bottom:1px solid gray;}
 #footer { position: fixed; border-top:1px solid gray;}
like image 44
elnabo Avatar answered Oct 30 '22 21:10

elnabo