Inline PHP

763 views
Skip to first unread message

Nick

unread,
Apr 27, 2011, 9:15:12 AM4/27/11
to dompdf
Hi,

i have questions about inline PHP in DOMPDF as there is no good
documentation about it.

when and how often gets the script part called, once or every page ?

because with add_object i have the possibility to add something just
to the current page for example
(and not every page). therefore it should be called every page ??

BUT:
when i do this:
if ($PAGE_NUM > 1)
{
create and add the object
}
it doesn't work, while
if ($PAGE_NUM > 0) WORKS ! (but with every page, i wanna skip the
first page (it's a coverpage))

WHY ?

Second question is:
i have made a footer.
when i don't call add_object, i just open and close it.
the footer appears, why ??
i think i have to add the object in order to appear.

as these are general question i have not posted any sourcecode.

thanks,
nick

Fabien Ménager

unread,
May 2, 2011, 5:32:01 PM5/2/11
to dom...@googlegroups.com
In fact, Inline PHP is evaluated at the very end of the rendering process, so this code may not work, like you pointed out.

Could you give the full script inside the inline PHP ?

Nick

unread,
May 3, 2011, 5:15:38 AM5/3/11
to dom...@googlegroups.com
Hi Fabien,
 
this is the script:
( as you can see, the only thing i wanna get working is: SKIP the first Page, but then the footer
doesn’t get rendered at all. it seems like dompdf is only looking at the script one time, then it sees
that $PAGE_NUM is 0 so it doesn’t go into the if-statement.
but next time when $PAGE_NUM is 1 ot should, but it doesn’t get called for every page as it should
because the “add” statement should only add the object to the current page)
( the other problem is the encoding of special character from german: üö etc. inside the footer)
 
 

<script type="text/php">

    if ( isset($pdf) )

    {

        $font = Font_Metrics::get_font("verdana");

        // If verdana isn't available, we'll use sans-serif.

        if (!isset($font)) { Font_Metrics::get_font("sans-serif"); }

        $size = 14;

        $color = array(0,0,0);

        $text_height = Font_Metrics::get_font_height($font, $size);

        $w = $pdf->get_width();

        $h = $pdf->get_height();

        // ------------------ FOOTER ------------------------------------------

 

        if ($PAGE_NUM > 1)

        {

            $footer = $pdf->open_object();

            $y = $h - 3 * $text_height - 24;

            $text = "Seite {PAGE_NUM}"; //"Page {PAGE_NUM} of {PAGE_COUNT}";

            $width = Font_Metrics::get_text_width("Seite 100", $font, $size);

            $pdf->page_text($w / 2 - $width / 2, $y, $text, $font, $size, $color);

            // Copyright line

            $y += $text_height * 2;

            $text = $pdf->get_page_number() . "Urheberrechtlich geschützt. Geschrieben und         veröffentlicht auf www.wrible.com";

            // Center the text

            $width = Font_Metrics::get_text_width($text, $font, $size);

            $pdf->page_text($w / 2 - $width / 2, $y, $text, $font, $size, $color);

            // Add a logo

            $img_w = 0.5 * 72; // 0,5 inches, in points

            $img_h = 0.5 * 72; // 0,5 inch, in points -- change these as required

            //$pdf->image("http://localhost/elgg-1.7.4/mod/writersdesk/templates/print_logo.png",             "png", 0, $y, $img_w, $img_h);

            $pdf->close_object();

            $pdf->add_object($footer, "all");

        }

        // ------------------ FOOTER END ------------------------------------------

    }

</script>

 

--
You received this message because you are subscribed to the Google Groups "dompdf" group.
To post to this group, send email to dom...@googlegroups.com.
To unsubscribe from this group, send email to dompdf+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/dompdf?hl=en.

Martin Thomas Schrott

unread,
May 3, 2011, 7:22:12 AM5/3/11
to dom...@googlegroups.com
Hi!
 
if you try to write special Characters like the Umlaute with an inline script into headers or footers, you will have to encode them utf-8 instead of html!
 
In the body you have to use html encoded characters!
 
This works if you can do the coding separately.
As I found this bug a while ago, Fabien is also aware of it and I am sure they will try to fix this in future.
 
cheers

Nick

unread,
May 3, 2011, 4:00:59 PM5/3/11
to dom...@googlegroups.com
hi,
 
thanks for the hint !
 
 
Fabien:
 
another thing about the inline PHP script i sent.
$pdf->get_page_number() is always 1
only the variable $PAGE_NUM gets updated.
 
maybe a hint for the bug ?
 
cheers,
nick

Martin Thomas Schrott

unread,
May 4, 2011, 3:35:29 AM5/4/11
to dom...@googlegroups.com
Hi again,

something additional... If you did not realice it in the meantime.

Inline scripts have two different options:

place it on the end of your body tag and it only will be rendered for the
last page.
Place it anywhere between the beginning of your body tag and the end and it
will be rendered from its position on all following pages. e.g. on the
beginning of the body tag it will be rendered on all pages beginning with
page 1.

this in mind you will be more flexible I guess.

cheers


Hi,

WHY ?

thanks,
nick

--

Nick

unread,
May 4, 2011, 3:45:09 AM5/4/11
to dom...@googlegroups.com
hi.

thanks for this, but my problem is actually more complex i guess.

i want to explicitly say on which pages the script gets rendered not on ALL
pages
or just the LAST page.

having $PAGE_NUM and an if statement and the option "ADD" with the
add_object
you should be able to just
SKIP PAGE 1

which is what i need to do.

thanks,
nick

-----Urspr�ngliche Nachricht-----
From: Martin Thomas Schrott
Sent: Wednesday, May 04, 2011 9:35 AM
To: dom...@googlegroups.com
Subject: Re: [dompdf] Inline PHP

Fabien Ménager

unread,
May 4, 2011, 5:39:07 AM5/4/11
to dom...@googlegroups.com
In fact, the PHP script is evaluated only once : when it is "found" by the renderer. To see this, put the script at the end of the document, the $PAGE_NUM will then be the page where it is found (the last one).

The evaluation only puts all the actions in a stack, that is processed once the HTML is rendered. I think I'll add a way to specify a range of pages where an object must be inserted. I'll try to do it ASAP as it looks like you need it quickly.

By the way, what version of DOMPDF do you use ? If it's the trunk, it's perfect, if not, you'll need to checkout the SVN trunk.

Nick

unread,
May 4, 2011, 7:29:55 AM5/4/11
to dompdf
Hi Fabien,

thank you for the effort, actually i found a slick workaround for my
problem,
here is the inline php script for skipping only the first (cover)
page :-)

<script type="text/php">
if ( isset($pdf) ) {
$php_code = '
$font = Font_Metrics::get_font("verdana");
if (!isset($font)) { Font_Metrics::get_font("sans-serif"); }
$size = 14;
$color = array(0,0,0);
$text_height = Font_Metrics::get_font_height($font, $size);
$w = $pdf->get_width();
$h = $pdf->get_height();
$y = $h - 3 * $text_height - 24;

$page_nr = $PAGE_NUM - 1;
$text = "Seite " . $page_nr;

$width = Font_Metrics::get_text_width("Seite 100", $font, $size);
if ($PAGE_NUM > 1)
$pdf->text($w / 2 - $width / 2, $y, $text, $font, $size, $color);';

$pdf->page_script($php_code);
}
</script>

if you add the script as a script inside "the script" then it gets
executed as a script on every page
(not as a prerendered script)

however the method $pdf->get_page_number() ist still not working,
there must be a bug with
add_object("add"), and the scope in which the pdf-script gets executed
for every page.

i'm using the svn trunk.

thanks,
nick

BrianS

unread,
May 13, 2011, 2:08:32 PM5/13/11
to dom...@googlegroups.com
It looks like you've already fixed this, but just to confirm, the $pdf->text() and $pdf->page_text() functions operate differently. The former adds text to the current object (or page if an object is not open) while the latter adds text to all pages.

I believe there are some problems with the object functionality in the CPDF library (or maybe the adapter?). We'll try to spend some time addressing any issues for the next release.

Nick

unread,
May 18, 2011, 8:58:06 AM5/18/11
to dom...@googlegroups.com
hi,
 
thank you for the explanation about the different use of text() and page_text().
 
however the one thing that’s not working with the inline script is that it only gets called one time
(first or last page)
is you have a text() inside the php script this gets only called one time.
 
in my understanding it should be called every page (inside the renderer class) or am i missing something ?
 
i fixed the issue for me but a strange thing i’m encountering is that when i add an image with my technique
the whole text on every page where the image is, gets blurry and somehow bolder.
 
it seems that reopening the pages and adding the script to every page (adding an image) somehow breaks
the nice looking of the font of the page, can that be ?
 
is so it seems a bug with the cpdf lib or what do you think ?
 
thanks,
nick
 
 
From: BrianS
Sent: Friday, May 13, 2011 8:08 PM

BrianS

unread,
May 18, 2011, 2:46:32 PM5/18/11
to dom...@googlegroups.com
On Wednesday, May 18, 2011 8:58:06 AM UTC-4, Nick wrote:
however the one thing that’s not working with the inline script is that it only gets called one time
(first or last page)
is you have a text() inside the php script this gets only called one time.
 
in my understanding it should be called every page (inside the renderer class) or am i missing something ?

To be called on every page you have to put the text() call inside an object. Otherwise it will only affect the current page. See the question "How can I add an image to a header or footer on every page?" on the FAQ.

Also, the script should be at the top of the document, because objects are added from the current page onwards.
 
i fixed the issue for me but a strange thing i’m encountering is that when i add an image with my technique
the whole text on every page where the image is, gets blurry and somehow bolder.
 
it seems that reopening the pages and adding the script to every page (adding an image) somehow breaks
the nice looking of the font of the page, can that be ?

I'm not sure what would cause something like that. Can you post a sample HTML document that displays the problem? Seeing the PDF may help in this case as well.

Nick

unread,
May 19, 2011, 2:37:43 AM5/19/11
to dom...@googlegroups.com
hi,
 
thanks for the quick reply.
 
the image error could be solved by converting the png file into an gif, seems like a problem with png.
maybe only png with dpi 300x300 and RGB 32bit color space.
 
i haven’t tried yet converting the png to 72dpi indexed color, maybe then it works...
 
actually you are right with the add_object, if you specify “all” for the second parameter it gets added to every page.
 
at the beginning of my post i mentioned that i only want to draw on specific pages using the “add” parameter
for add_object. but this only outputs on the first page, because the renderer looks up the script and executes
it only one time.
 
(the page_script reopens every page and processes the script on every page, there you can check with PAGE_NUM
on which page to draw)
 
however it would be nice if the add_object($pid, “add”) would also work, but it’s not necessary for me right now.
 
i think you would have to pick up the script in renderer and then when there is a new page, call every script that you
have found.
 
cheers
nick
 
 
From: BrianS
Sent: Wednesday, May 18, 2011 8:46 PM

Nick

unread,
May 19, 2011, 5:16:24 AM5/19/11
to dom...@googlegroups.com
hi,
 
first of all the image problem is not there with pdflib (pdflib currently not working i have posted issue).
 
but:
both cpdf and pdflib produce the following problem with my script for footer and header. (or page_text)
 
if for example i have a green color in the text on 2 pages. (end of first page beginning of second page)
then the text in between (the header or footer for example) also gets green, although i specified setcolor black
in the script, so actually as the header or footer seems to be inside the “span” of the green text, the color
gets overriden, which of course is not what i want.
 
the problem is with reopening the object after rendering the pages ?
 
you can try it very quick and simple, make html text green over 2 pages, add a php/script with page_text
(appears on every page) colored black –> but it will get green.
 
Reopening Objects VS Adding new ones:
if you use just $pdf->text instead of $pdf->page_text,
create an object and add it to every page (add_object($pid, “all”)), then it gets colored black, so then it is working.
 
So I cannot use my technique no more, i really have to put the footer and header rendering inside the render
process where objects gets added to pages. (but not to all pages, to just specific ones (script has to get
executed on every page ( if i specifiy add_object($pid, “add”))
 
“we need a way to dynamically (script) add objects to specific pages.”
 
i will try to fix that in the dompdf code, can you tell my what a frame  is ? it’s not a page in the rendering process ?
i need to know the place where pages are generated.
i will dig into the code...
 
 
cheers
nick
 
 
 
From: BrianS
Sent: Wednesday, May 18, 2011 8:46 PM
Subject: Re: [dompdf] Inline PHP
 

BrianS

unread,
May 19, 2011, 2:31:21 PM5/19/11
to dom...@googlegroups.com
On Thursday, May 19, 2011 2:37:43 AM UTC-4, Nick wrote:
the image error could be solved by converting the png file into an gif, seems like a problem with png.
maybe only png with dpi 300x300 and RGB 32bit color space.
 
i haven’t tried yet converting the png to 72dpi indexed color, maybe then it works...

The bold/blurry problem appears to have an effect regardless of the resolution of the image. It does appear to be specific to rendering a PNG image on the page. Something was introduced between beta 1 and beta 2 to cause the issue. Feel like posting a bug report?
 
 actually you are right with the add_object, if you specify “all” for the second parameter it gets added to every page.
 
at the beginning of my post i mentioned that i only want to draw on specific pages using the “add” parameter
for add_object. but this only outputs on the first page, because the renderer looks up the script and executes
it only one time.
(the page_script reopens every page and processes the script on every page, there you can check with PAGE_NUM
on which page to draw)
 
however it would be nice if the add_object($pid, “add”) would also work, but it’s not necessary for me right now.
 
i think you would have to pick up the script in renderer and then when there is a new page, call every script that you
have found.

I know there have been problems in the past with the object parsing in CPDF. I'm not sure if they have all been addressed. If you can supply a sample of what you want we'll take a closer look at how it should be done in CPDF and whether or not it works as expected. And also if an enhancement needs to be made to support your needs.

BrianS

unread,
May 19, 2011, 2:50:44 PM5/19/11
to dom...@googlegroups.com
On Thursday, May 19, 2011 5:16:24 AM UTC-4, Nick wrote:
first of all the image problem is not there with pdflib (pdflib currently not working i have posted issue).

Addressed the issue in the nightly. Thanks for pointing it out.
 
but:
both cpdf and pdflib produce the following problem with my script for footer and header. (or page_text)
 
if for example i have a green color in the text on 2 pages. (end of first page beginning of second page)
then the text in between (the header or footer for example) also gets green, although i specified setcolor black
in the script, so actually as the header or footer seems to be inside the “span” of the green text, the color
gets overriden, which of course is not what i want.
 
the problem is with reopening the object after rendering the pages ?
 
you can try it very quick and simple, make html text green over 2 pages, add a php/script with page_text
(appears on every page) colored black –> but it will get green.
 
Reopening Objects VS Adding new ones:
if you use just $pdf->text instead of $pdf->page_text,
create an object and add it to every page (add_object($pid, “all”)), then it gets colored black, so then it is working.
 
So I cannot use my technique no more, i really have to put the footer and header rendering inside the render
process where objects gets added to pages. (but not to all pages, to just specific ones (script has to get
executed on every page ( if i specifiy add_object($pid, “add”))
 
“we need a way to dynamically (script) add objects to specific pages.”

I see this with add_object, but not with page_text. Maybe the text state (color/stroke/etc) is not being saved with the object. I wonder if this is maybe also the reason for problems with PNGs.
Reply all
Reply to author
Forward
0 new messages