Jeckky / dompdf

Automatically exported from code.google.com/p/dompdf
0 stars 0 forks source link

PDFLib requires that an images to be imported prior to creating a new PDF object #401

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Inline scripting allows for the creation of headers/footers using PDF objects, 
e.g.:

<html>
<body>
  <p>Hello World</p>
  <script type="text/php">
    if ( isset($pdf) ) {
      $header = $pdf->open_object();
      $pdf->image("someimage.jpg", "jpg", 1, 10, 570, 130);
      $pdf->close_object();
      $pdf->add_object($header, "all");
    }
  </script>
</body>
</html> 

The above example includes an image in the header, but this code will not work 
when using the PDFLib rendering engine. PDFLib requires import of images prior 
to switching context to a PDF object. Not meeting this requirement results in a 
fatal error:

Uncaught exception 'PDFlibException' with message 'Function must not be called 
in 'template' scope'

The work-around requires a bit of a kludge. Specifically, you have to create 
the image, place it invisibly, open the PDF object, and then place the image 
again, e.g.:

<html>
<body>
  <p>Hello World</p>
  <script type="text/php">
    if ( isset($pdf) ) {
      $pdf->image("someimage.jpg", "jpg", 0, 0, 0, 0);
      $header = $pdf->open_object();
      $pdf->image("someimage.jpg", "jpg", 1, 10, 570, 130);
      $pdf->close_object();
      $pdf->add_object($header, "all");
    }
  </script>
</body>
</html>

Possible solutions:
1) modify dompdf to require image import prior to object creation
2) parse inline scripts to catch any instance of image placement and prepend 
the necessary import operation
3) modify dompdf such that when an image placement operation is encountered the 
current object is closed, the image is imported, and the object is reopened.

For more information, see PDFLib 7.0.4 Tutorial section 3.2.4 and the API 
reference for load_image().

Originally reported on the support forum at 
https://groups.google.com/forum/#!topic/dompdf/IUBOEiRLqL0

Original issue reported on code.google.com by eclecticgeek on 27 Dec 2011 at 7:38

GoogleCodeExporter commented 8 years ago

Original comment by eclecticgeek on 24 May 2013 at 3:00