Forum How do I...?

[solved] PHP code evaluation

seawolf
Hi all,

First off, a big thanks for this software, it's a fantastic tool. I've been using it with Rails and am very happy with it. I'm starting to now experiment with it in PHP (via XAMPP) and have run into a problem.
Getting Prince together, I can create a PDF of a document without troubles; within a PHP block I can instansiate a $prince object by referenceing Prince.php and later get it to create a PDF of the page itself.


However, when the HTML document includes a <?php ... ?> tag, it gets ignored instead of being processed by PHP. To illustrate:

<h1>Hello! I'm <?php $title='PHPrince' ; echo $title ?>!</h1>

returns:
<h1>Hello! I'm !</h1>


It's fine in the browser so I'm assuming that Prince's rendering isn't calling Apache/PHP to provide the code but an internal processor? I'm confused by this as with Rails I've been happily using in the same way, just variables -- <%=h @variable %> -- and such.

I've seen many posts about getting a PHP block to talk to Prince, but my problem is the other way round, Prince isn't talking with my webserver/PHP!

Thanks for any help, I don't mind further questions at all!

Edited by seawolf

seawolf
Update: I went round in circles and it works.

Having moved it to a) a Linux box, b) where Apache serves it's files, the following code is okay:

  $prince = new Prince('/usr/bin/prince');
  $prince->setLog('prince.log');
  $fopen = fopen("http://my.web.server/php_file.php", "r");
  $xmlString = fread($fopen, filesize("php_file.php")); // -< Local file...!
  $prince->convert_string_to_file($xmlString, "generated.pdf", $messages= array ());
  dump ($messages);
  fclose($fopen);


I'm assuming it's because the files are now being accessed correctly and also processed through PHP/Apache.

Any more insights still welcome!
David J Prokopetz
When you use fread on a file opened via a local path, it just grabs whatever's on the disk - no parsing or inline code execution takes place. You'll have to use the eval() function to parse the string for inline PHP before passing it to Prince.

http://php.net/manual/en/function.eval.php

Without that step, you're passing a string containing raw PHP code to Prince, which (correctly) ignores it. The code you've got works because PHP can't "see" the file's disk path when requesting it via HTTP, even though you're actually hitting localhost.
seawolf
Ah, what I was getting Apache to do by serving the HTML (and in turn PHP) instead of eval'ing it directly and defining the xmlString.
Thanks!