Forum How do I...?

PHP output to browser

lindm
Trying a php implementation of Prince. I want the generated output to be opened in a new browser window and NOT stored on the server. Have tried a couple of temp solutions but no success yet. Any easy solutions?

Thanks
mikeday
Try the convert3 method, which takes an XML or HTML string (call setHTML first if it's HTML) and returns the PDF as the output of the current page. You will need to set the content-type to application/pdf before calling this function, so that the browser knows what it is getting, and you can try calling setLog if the PDF isn't being generated and you want to see if Prince is producing any error messages.
lindm
Seems some posts were erased so I post a follow up. Get this error when converting with convert3 in a php script:


Thu Nov 29 18:59:54 2007: print.css: warning: can't open input file: No such file or directory


Here is my php script:


<?php

error_reporting(E_ALL ^ E_NOTICE); 
ini_set("display_errors", 1); 

require_once("prince.php"); 


$oPrince = new Prince("/Library/WebServer/Documents/htdocs/prince/bin/prince"); 

$oPrince->addStyleSheet('http://localhost/htdocs/phptest2/print.css');

$oPrince->setHTML(true); 
$oPrince->setLog("/Library/WebServer/Documents/htdocs/prince/prince.log"); 

$iType = 0; 

if($iType == 0) 
{ 
   header("Content-Type: application/pdf"); 
   header("Content-Disposition: attachment; filename=ar.pdf"); 

   chdir("/Library/WebServer/Documents/htdocs/prince/bin/"); 

   $bSuccess = $oPrince->convert3(file_get_contents($_SERVER['HTTP_REFERER'])); 

   echo ($bSuccess ? "Success<br>\n" : "Failure<br>\n"); 
} 
else 
{ 
   $bSuccess = $oPrince->convert2($_SERVER['HTTP_REFERER'], "/tmp/test.pdf", $aMessages); 
header("Location: /tmp/test.pdf");

   echo ($bSuccess ? "Success<br>\n" : "Failure<br>\n"); 

   echo "<pre>"; 
   var_dump($aMessages); 
   echo "</pre>"; 
} 
   
?>



THe strange thing is that the css-file is used correct in the pdf conversion. Why do I then get this error? I run MacOSX.
mikeday
Looks like some forum posts were lost due to server maintenance, sorry about that.

Try calling setBaseURL and specifying the URL of the document so that relative links to images, style sheets, etc. can be resolved successfully.
lindm
Still get the logerror after specifying baseurl...everything works correct, the print.css i read correctly but I get the same error in the log...is the log perhaps wrong??

Part of my script now:

$oPrince->setBaseURL('http://localhost/htdocs/phptest2/');
$oPrince->addStyleSheet('print.css');

[/code]



Error I get in the log:

Sat Dec  1 19:41:22 2007: print.css: warning: can't open input file: No such file or directory
mikeday
Okay, now I'm getting a bit confused. Does the document you are converting refer to a print.css file, using the XHTML <link> tag, @import directive in <style>, or xml-stylesheet processing instruction?

If the document is linking to the style sheet, then you'll need to call setBaseURL with the complete URL of the document. If you are adding the style sheet yourself with the addStylesheet method then just ensure that you specify an absolute path.
lindm
I use the addstylesheet method and use the absolute path so the print.css is used correctly, no problem. I just wondered about why the log gives an error despite everything seems to be working fine...
mikeday
I don't know, that's really strange. Are you sure that the document or one of its style sheets doesn't refer to print.css somewhere?
lindm
Aa, got it I have a reference to print.css inside the html doc. I decide to keep the reference in the htmldoc and drop the addstylesheet in the php convert script. Guess this is no problem?
mikeday
No problem, just specify the URL of the document with setBaseURL to ensure that relative links are resolved correctly.