从Java重新创建PDF文件到PHP [英] Recreate PDF File From Java To PHP
问题描述
我有一个Java的WebService(使用Apache Axis)获取文档的id并调用JasperReports创建一个PDF文件(以前在java app - server side中创建的报告)来创建报告我是使用方法:JasperManager.fillReport和JasperExportManager.exportReportToPdf。最后一个返回一个字节数组。我的webservice获取数组并将其编码为Base64字符串,PHP接收此字符串作为WebService的响应。
I have a WebService in Java (Using Apache Axis) that get's the id of a document and this call to JasperReports to create a PDF File (Report previously created in java app - server side), to create the report i'm using the methods: JasperManager.fillReport and JasperExportManager.exportReportToPdf. The last one returns an array of bytes. My webservice takes the array and encodes it into a Base64 String, PHP receives this string as response from the WebService.
我想用PHP重新创建文件,但我不知道我不知道这是否可行。我正在尝试使用以下代码段执行此操作:
I want to recreate the file in PHP, but i don't know exactly if this is possible. I'm trying to do this with the following snippet:
private function createFileFromString($stringWithFile){
header('Content-Description: File Transfer');
header("Content-Type: application/pdf");
header('Content-Disposition: attachment; filename=remesa.pdf');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_start();
ob_clean();
ob_flush();
flush();
$fp = file_put_contents("document.pdf", "w");
fwrite($fp, base64_decode($stringWithFile));
readfile($fp);
ob_get_contents();
fclose($fp);
exit();
}
WebService返回一个字符串,如下所示:
The WebService Returns a String Like this:
JVBERi0xLjQKJeLjz9MKNCAwIG9iaiA8PC9UeXBlL1hPYmplY3QvQ29sb3JTcGFjZS9EZXZpY2VS
R0IvU3VidHlwZS9JbWFnZS9CaXRzUGVyQ29tcG9uZW50IDgvV2lkdGggMjg0L0xlbmd0aCAzNjc0
L0hlaWdodCA1MC9GaWx0ZXIvRENURGVjb2RlPj5zdHJlYW0K / 9J / 4AAQSkZJRgABAgAAAQABAAD /
2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0
Hyc5PTgyPC4zNDL / 2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy
MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL / wAARCAAyARwDASIAAhEBAxEB / 8QAHwAAAQUBAQEBAQEA ...
JVBERi0xLjQKJeLjz9MKNCAwIG9iaiA8PC9UeXBlL1hPYmplY3QvQ29sb3JTcGFjZS9EZXZpY2VS R0IvU3VidHlwZS9JbWFnZS9CaXRzUGVyQ29tcG9uZW50IDgvV2lkdGggMjg0L0xlbmd0aCAzNjc0 L0hlaWdodCA1MC9GaWx0ZXIvRENURGVjb2RlPj5zdHJlYW0K/9j/4AAQSkZJRgABAgAAAQABAAD/ 2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0 Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyARwDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEA...
此字符串包含PDF标题,如下所示:
This string has the "PDF Header", begins like this:
%PDF-1.4%ãÏÓ40 obj
<> streamÿØÿà
%PDF-1.4 %âãÏÓ 4 0 obj <>stream ÿØÿà
当我尝试下载文件时,它的大小约为4-5KB(并且文件已损坏),但响应字符串大约为180KB。
When i try to download the file, it's size is around 4-5KB (and, the file is damaged), but the Response String is around 180KB.
这是pos锡布尔赫丁?我做错了什么?
Is this possible? What i'm doing wrong?
编辑:我读过关于解包功能,也许这个功能可以帮到我吗?
I've read about unpack function, maybe this function can help me?
提前致谢
推荐答案
好的,我解决了。我基本上有两个错误:
Ok, i solved it. I have, basically, two errors:
1。来自Java:我用来编码PDF文件内容( org.w3c.tools.codec.Base64Encoder
)的类正在破坏字符串文件内容,而不是这个类,我已切换到Apache Axis工具( org.apache.axis.encoding.Base64
)。我这样说是因为我比较了用我的Java App生成的PDF的内容和从WebService返回的内容,而最后一个的内容有点小。
1. From Java: The class that i've used to encode the content of PDF file (org.w3c.tools.codec.Base64Encoder
) was corrupting the String with the file content, instead of this class, i've switched to Apache Axis Tools (org.apache.axis.encoding.Base64
). I say this because i've compared the content of a PDF generated with my Java App and the content returned from the WebService and the last one is a bit smaller.
<强> 2。从PHP:构建文件:在这部分我试图这样做:
2. From PHP: Building The File: In this part i've trying to do this:
$fp = file_put_contents("document.pdf", "w");
fwrite($fp, base64_decode($stringWithFile));
readfile($fp);
但是,只有使用file_put_contents我已经将内容写入PDF文件,fwrite it wasn'在这一点上必然。我正在使用此代码段生成pdf:
But, only with file_put_contents i've done writing the content to a PDF File, fwrite it wasn't necessarily at this point. I'm generating the pdf with this snippet:
private function createFileFromString($stringWithFile){
header('Content-Description: File Transfer');
header("Content-Type: application/pdf");
header('Content-Disposition: attachment; filename=document.pdf');
header('Cache-Control: must-revalidate');
header('Pragma: public');
flush();
file_put_contents("document.pdf", base64_decode($stringWithFile));
readfile("document.pdf");
exit();
}
我希望这个答案对每个人都有帮助。
I hope that this answer helps to everyone.
这篇关于从Java重新创建PDF文件到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!