PHP Java Bridge # 从 PHP 调用 FOP

发布于 2024-09-08 01:14:31 字数 497 浏览 0 评论 0原文

我经常查看 StackOverflow,并且通常会为我提供很多帮助:) 所以我猜为什么不在这个问题上寻求帮助呢?

我在基于 Web 的应用程序上使用 PHP,并且需要使用 Apache FOP 从预格式化的 FO 文件生成 PDF。 ATM 我使用命令行 exec('fop...') 来实现此目的,但在更改服务器操作系统时遇到了一些麻烦。

我在网上看到可以使用 PHP Java 桥直接从 Java 类调用 FOP,但是在多次尝试失败后,我想知道你们中的一些人是否有比这个更好的教程 HowTo/PHPJavaBridge

PS:桥已安装并工作,FopWrapper.jar 已构建并设置,但当我从 PHP 调用它时,出现错误“ClassNotFound”。一些想法?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城追梦 2024-09-15 01:14:31

如果您使用共享托管,这可能是一个坏主意(“我在更改服务器操作系统时遇到了一些麻烦”)。 PHP/Java 桥是一个 Java EE 应用程序,通过本地套接字与 PHP 应用程序进行通信。 Java EE 支持在共享托管帐户上并不常见。

如果主机安装了 JVM(假设可以使用“java -v”测试 shell 访问),我将执行以下操作:

编写一个接受两个参数的简单 Java SE 应用程序:

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

通过 system() 或 exec() 接口运行此应用程序PHP,使用 random() 或 mktime() 来获取随机文件名。

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.

我不是你的备胎 2024-09-15 01:14:31

最后我在这个邮件列表上找到了一个解决方案 Fop from PHP Java,因此与最新发布的 FOP 0.9x 版本配合得很好。现在唯一的问题是,当 tomcat 完成生成的 pdf 的工作时,最后一个文件保持锁定状态,如果不重新启动服务器就无法打开它。

有什么想法吗?之前谢谢:)

编辑:好吧,我的错作为一个Java菜鸟并没有帮助我。事实上,问题是我打开了一个FileOutputStream并忘记在最后关闭它。如果这可以帮助某人,我是这样做的:

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();

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();
仙女 2024-09-15 01:14:31

在我们公司,我们通过创建一个简单的 FOP 服务器来完成所有工作来解决您的问题。您可以使用 PHP curl 或所有其他连接主机的方法来连接到 FOP-Server。我们将 .fo 文件内容发送到服务器,并将收到生成的 PDF。优点是,不依赖于 FOP-Server 运行的服务器。
我们遇到的唯一问题是作为 Linux 守护进程运行简单的 Java 程序并不容易。您可以使用 Java Servlet 容器(在 Tomcat 中)来运行您的软件。对于我们的使用,单个 Linux“屏幕”即可完成这项工作。

编写 FOP 服务器需要您一些时间,具体取决于您的可选设置。您可以使用标头信息和 .fo 文件的内容来实现单个协议。

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.

德意的啸 2024-09-15 01:14:31

如果您不想参与 Java,还有另一种选择。这里有一个免费的 FOP 服务器:

FOP Server ,它只是一个包含运行 Apache FOP 所需的所有内容的捆绑包。
您可以使用 HTTP post 从 PHP 调用服务器,该页面中有一个示例

there is an alternative if you do not want to be involved with Java. There is a free FOP server here:

FOP Server , it is just a bundle that includes all you need to run Apache FOP.
You can call the server from PHP using a HTTP post, in that page there is an example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文