tags:

views:

205

answers:

3

Hello guys, I often check StackOverflow and help me so much usually :) so i guessed why don't ask for help on this one ?

I'm using PHP on a web-based application and i need to use Apache FOP to generate a PDF from a pre-formatted FO file. ATM I'm using the command line exec('fop...') for this purpose, but i have several troubles when changing the server's os.

I've seen on the web that FOP can be called directly from a Javaclass using a PHP Java bridge, but after trying and trying unsuccessfully I'd like to know if some of you have a better tutorial then this HowTo/PHPJavaBridge.

PS : the bridge is installed and working, the FopWrapper.jar built and set, but when i call it from PHP i have an error "ClassNotFound". Some ideas ?

Thanks.

A: 

If you're using shared hosting this is probably a bad idea ("i have several troubles when changing the server's OS"). The PHP/Java bridge is a Java EE application communicating with your PHP application via a local socket. Java EE support is not very common on shared hosting accounts.

If the host has JVM installed (assuming shell access this can be tested with "java -v") I would do as follows:

Write a simple Java SE application accepting two parameters:

"java -jar MySimpleInterfaceToFOP.jar "/path/to/inputFile" "/path/to/outputFile" "

Run this via the system() or exec() interface in PHP, use random() or mktime() to get random filenames.

matiasf
I actually use dedicated server and Java EE is always supported. Your idea make sens but I often have a permission trouble while passing by exec() or system() methods, the Java commands allowed in shell / prompt cannot always be launched by php.half of the time i have an error telling me that the "java" command isn't recognized, and if i type it directly in shell / prompt it runs well. That's why i was trying to access that java command without using exec().EDIT : i've just suceeded on accessing java.lang.System class with the bridge, so i guess the trouble come from the *java_require()* :(
aoi onore
A: 

Finally i found a solution on this mailing list Fop from PHP Java so that works great with the last released version of FOP 0.9x. the only problem now it's that when tomcat finished his works on the generated pdf, that last stay locked and no way to open it without restarting the Server.

Any ideas ? thanks previously :)

EDIT : Ok my bad being a really noob in Java didn't helped me. In fact the trouble was that i opened a FileOutputStream and forgot to close it at the very end. If this can help someone, here how i did it :

require_once("java/Java.inc");

$input      = 'D:/wamp/www/test/fo2pdf';
$output     = 'D:/wamp/www/test/fo2pdf';
$sourcefile = 'test.fo';
$destfile   = 'trys.pdf';

$pdffile    = new Java("java.io.FileOutputStream", $output . "\\" . $destfile);
$mimes      = new Java("org.apache.fop.apps.MimeConstants");
$fopfactory = new Java("org.apache.fop.apps.FopFactory");

$fopf       = $fopfactory->newInstance();
$fopf->setUserConfig( new Java("java.io.File", "D:/wamp/www/myvisitV3/outils/FOP/conf/fop.xconf") );
$userf      = $fopf->newFOUserAgent();
$fop        = $fopf->newFop($mimes->MIME_PDF, $userf, $pdffile);


$transformerclass   = new Java("javax.xml.transform.TransformerFactory");
$transformerfactory = $transformerclass->newInstance();
$transformerf       = $transformerfactory->newTransformer();

$src        = new Java("javax.xml.transform.stream.StreamSource", new Java("java.io.File", $input . "\\" . $sourcefile ));
$res        = new Java("javax.xml.transform.sax.SAXResult", $fop->getDefaultHandler());

$transformerf->transform($src, $res);
$pdffile->close();
aoi onore
A: 

In our company we have solved your problem by creating a simple FOP-Server to do all the work. You can connect to the FOP-Server by using PHP curl or all other methods to connect to a host. We send the .fo file content to server and will receive the generated PDF. The advantage is, that are not depended on which server the FOP-Server is running. The only problem we had is that's not easy to run a simple Java programm as a Linux deamon. You can probably use a Java Servlet container (in Tomcat) to run your software in. For our use a single Linux 'screen' does the job.

To write a FOP-Server will take you a few ours, depending on your optional settings. You can implement a single protocol with header informations and then the content of the .fo file.

DrDol