Forum Bugs

[Prince/PHP] generate several pdf in the same php script

Darathor
Hi,

I'm using Prince with to generate PDF files. When I generate a single PDF file, no problem, it is perfectly generated. But when I try to generate successively several PDF files, only the first is generated...

When several PDF files need to be generated, the content differs only by the name of the recipient and its postal adress... and the XHTML code is correctly generated. But when I call Prince to convert it, only the first is converted.

I looked in my PHP code for many hours and I didn't find any explication... I've tried many methods :
- with the 3 convert methods of the PHP interface.
- with a direct call to Prince with the exec function.
- using exec to call a separated php script wich call Prince for each single PDF.
- ...

In all cases it's the same : the first file is OK, but there is nothing for the followings...

When I call :
exec('/usr/bin/prince "/home/[...]/xhtmlSource" "/home/[...]/pdfDestination"', $results, $returnedValue);
In all case the $results array is empty. For the first call $returnedValue = 0 and for the following, $returnedValue = 1.

Is there a Prince or PHP bug ? Does anybody already faced (and
ideally solved :P ) a similar problem ?

Thanks
mikeday
That is very strange. I'm not sure how this can be a Prince bug, unless it can be replicated from the command-line as well. Is it possible that the first invocation of Prince is creating a PDF file that is read-only, then the second invocation of Prince is trying and failing to overwrite the same file?
Darathor
I think not : the two files has different names... and the first file is deleted before the second one is generated.

My script works like this :
1) xhtml code generation
2) conversion to pdf
3) send the mail whit the pdf attached
4) delete the files
5) next recipient : go back to the first point

I do not know either how it can be a Prince bug, but I didn't find yet any other explication, so...
Stephane
Did you check that your script didn't exceed php max execution time? It could be that the other pdf is never created because the script is always stopped before getting there.

If not, did you try to generate the 2 files without the mail/delete actions (using 2 different names)? Maybe the file is created too quickly by Prince, and gets deleted right away, due to a weird command stacking.
Darathor
Thanks for your answer but unfortunately I already tested this :?

The mails are all correctly sent but the pdf is just missing (or empty according to the generation method I use. The first method I tried was to use convert3 with the ob_content php methods to get the pdf content and write it in the file but the result is empty), so it can't be an execution time problem.
mikeday
You said that the first call to exec() succeeds and subsequent call fail. What if you call a different program than Prince, for example call "ls" or some other simple program that should produce some output -- does the second exec() fail with all programs, or just with Prince?
Darathor
It is a good question, I've tested it only with Prince... I'll test it with an other program.
Darathor
After a rapid test, calling "ls" with exec works on each iteration... So the problem is not here :(
mikeday
Well I guess that's good, at least it's a Prince problem that we may have some hope of tracking down. :)

If you are calling /usr/bin/prince, which is a shell script, perhaps you could add a line at the beginning of this shell script to help us figure out if it is really executing or not, like this:
#! /bin/sh

echo "Prince is running with these options: $*" >> /tmp/prince.log

This will append a message to a temporary log file every time Prince is run, so remember to remove this line once everything is working again. Make sure that the log file is empty to start with, and then try to exec() Prince twice. Do you get two lines in the log file, or only one?