Forum How do I...?

i'm really rusty with PHP, so...

alyda
can anyone show me examples of how to implement prince through php? i have uploaded prince to http://jestercom.net/prince/prince.php, i guess i need a second php file (runPrince.php) that calls on the functions inside prince.php


here's what i have inside the second php file:


<?php

$prince = new Prince('/prince');

$xmlPath = "healthcareanywhere.businesscatalyst.com/CaseRetrieve.aspx?ID=478760&E=1";
$pdfPath = "/prince/output";

function convert_file($xmlPath, $pdfPath, &$msgs = array());

function setHttpUser(alyda);

function setHttpPassword(password);

?>

Edited by alyda

mikeday
Try:
$prince = new Prince('/prince');

$xmlPath = "http://hcemr.com/CaseRetrieve.aspx?ID=478760&E=1";
$pdfPath = "/prince/output";

$prince->setHttpUser(alyda);
$prince->setHttpPassword(password);

$prince->convert_file($xmlPath, $pdfPath);

Also, check that '/prince' is really the correct path to the Prince executable (try running it from the command-line).

If things aren't working, use $prince->setLog() to specify a log file for Prince to save any error or warning messages.
petitg
You can also convert html to pdf without using files:

<?php
include 'prince.php';

$prince = new Prince('/usr/local/bin/prince');

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="hello_world.pdf"');

$html = "<html><body>Hello World!</body></html>";

echo $prince->convert_string_to_passthru($html);
?>
alyda
i have a php file making a request through SOAP (and storing xml in a variable called $response, i think my code is correct but i probably need to wait to run this script until after php has actually completed the call. I'm not getting any error messages

here's my code though:

// command-line: prince -s style.css onlinetest.xml -o output/test.pdf
$prince = new Prince('prince');
$pdfPath = "output/".$emailadd.".pdf";
$cssPath = "style.css";

$prince->addStyleSheet($cssPath);
$prince->convert_string_to_file($response, $pdfPath, $msgs = array());
//$prince->convert_file($response, $msgs = array());

$prince->setLog("princelog");

also, does it really matter if i use "convert_string_to_file" or "convert_file"? just curious...
mikeday
You need to call setLog() before calling a convert method in order to receive log messages. Whether you use convert_string_to_file or convert_file depends on whether $response is a document (eg. "<html>...") or a filename (eg. "/foo/bar/mydoc.html").

One more thing: it might be better to specify the full path to the Prince binary, such as "/usr/local/bin/prince" or wherever it is installed, in case it is not in the $PATH.
alyda
ok, moved setLog() above convert method.

and i believe i have installed prince in the /httpdocs/prince/ folder;
when i tested prince from terminal this is what i've done:


ssh root@jestercom.net

root@jestercom.net's password:

[root@jestercom jestercom.net]# cd /var/www/vhosts/jestercom.net/httpdocs/prince

[root@jestercom prince]# prince

[root@jestercom prince]# -s style.css onlinetest.xml -o output/onlinetest.pdf

// file successfully created in /output folder
mikeday
The reason I suggest using absolute paths (eg. /var/www/vhosts/jestercom.net/httpdocs/prince, rather than just "prince") is because the $PATH environment variable may be set differently for your PHP scripts compared with when you are using the Terminal, resulting in PHP being unable to find the Prince executable.
alyda
ok, i logged in through Terminal and did this:

[root@jestercom prince]# which prince
/usr/bin/prince


so prince is installed in '/usr/bin/prince'

now my php is:

<?php
include('prince.php5');

$prince = new Prince('/usr/bin/prince'); 

$xmlPath = "http://jestercom.net/prince/2356197/A.Identification(Adult).xml"; 

$prince->setLog("princelog"); 

$prince->convert_file($xmlPath, $msgs = array()); 

print_r($msgs);
?>


the file is located here: http://www.jestercom.net/prince/phptest.php5

prince must be .php5 because of my server (trying to fix that, but anything with .php is php4.3.9, see http://jestercom.net/phpinfo.php and http://jestercom.net/phpinfo.php5 )

still getting nothing, the msgs array returns:

Array ( ) 


and this may be a stupid question but my logfile should have an extension of ".log" correct? (http://www.jestercom.net/prince/princelog.log) what am i still doing wrong?
mikeday
Specify an absolute path for the log file, and make sure that it's in a directory that your PHP script has permission to write to. For debugging, it might be easiest to specify something in /tmp, which should be writable. For example, "/tmp/prince.log". (The exact name of the file doesn't matter).