Forum How do I...?

Output to variable in PHP -- passthru not working

dwertheimer
Pass thru does not work for me when I follow the instructions exactly. The PDF shows up as blank in the browser whether I send it as inline or attachment.

To work around this, I am saving the file out to the filesystem and then sending it through as a file. That works (so I know Prince is running fine), but it is not ideal, because I don't want to write to the filesystem.

Ideally, I would like to save the output to a variable/string and then I could echo it AND send it as an attachment via email.

To work around the passthrough problem, I tried using ob_start() and ob_get_clean() etc. and put pass thru in the middle. That does capture the output in a variable, but when I try to output that to the browser, it fails also. Is it something in the encoding that is causing this?

mikeday
Have you tried calling setLog and checking the log file for errors/warnings?
dwertheimer
Yes. The logs look great.

Thu Oct 2 01:30:37 2014: ---- begin
Thu Oct 2 01:30:38 2014: Loading document...
Thu Oct 2 01:30:38 2014: loading HTML5 input: -
Thu Oct 2 01:30:38 2014: Applying style sheets...
Thu Oct 2 01:30:38 2014: loading style sheet: http://127.0.0.1:8888/ci/css/fb-main.css
Thu Oct 2 01:30:38 2014: loading style sheet: http://127.0.0.1:8888/ci/css/view-data.css
Thu Oct 2 01:30:38 2014: loading style sheet: http://127.0.0.1:8888/ci/css/tablesorter.css
Thu Oct 2 01:30:38 2014: loading style sheet: http://127.0.0.1:8888/ci/css/pdf-output.css
Thu Oct 2 01:30:38 2014: Preparing document...
Thu Oct 2 01:30:38 2014: Converting document...
Thu Oct 2 01:30:38 2014: used font: Lucida Grande, Bold
Thu Oct 2 01:30:38 2014: used font: Lucida Grande, Regular
Thu Oct 2 01:30:38 2014: used font: Times New Roman, Regular
Thu Oct 2 01:30:38 2014: used font: Times New Roman, Italic
Thu Oct 2 01:30:39 2014: Resolving cross-references...
Thu Oct 2 01:30:39 2014: finished: success
Thu Oct 2 01:30:39 2014: ---- end

The echo seems fine in the debugger:
Start: http://o7.no/1psuu8a
End: http://o7.no/ZunXns

But when I call it from a browser and send it via:
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment; filename=foobar.pdf');
echo $pdf;


It saves as a zero bytes and won't open. Tried in Safari and Chrome both. Same result.

I have tried inline, and I have tried lots of other headers. I have changed the filename.
I have tried sending it as binary...

I have spent hours troubleshooting. Have not figured it out. Do you have any clues?
mikeday
That is really very strange. And you get the same behaviour when you capture it with ob_start(), so it's not a passthru issue specifically.

Are you getting any PHP related errors or warnings in the web server error log?

What happens if you set the content-type header to application/pdf, and then write some regular text to the output? Obviously it won't be a valid PDF, but can you save the response, or is it still zero bytes in size?

Also, maybe try downloading using curl or wget, just for comparison?
dwertheimer
I am actually thinking that it is an Apache write permission issue.
Because when I run it from the debugger (it's running as a php script under my username), I can see the prince output.
But from a browser it comes out blank. Does prince write to a directory that might not be write-able by the apache process?
mikeday
Prince will not write to any files if you are using passthru, except the error log if you have called setLog().