Recreate PDF File From Java To PHP

2019-02-26 10:06发布

问题:

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.

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

The WebService Returns a String Like this:

JVBERi0xLjQKJeLjz9MKNCAwIG9iaiA8PC9UeXBlL1hPYmplY3QvQ29sb3JTcGFjZS9EZXZpY2VS R0IvU3VidHlwZS9JbWFnZS9CaXRzUGVyQ29tcG9uZW50IDgvV2lkdGggMjg0L0xlbmd0aCAzNjc0 L0hlaWdodCA1MC9GaWx0ZXIvRENURGVjb2RlPj5zdHJlYW0K/9j/4AAQSkZJRgABAgAAAQABAAD/ 2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0 Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyARwDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEA...

This string has the "PDF Header", begins like this:

%PDF-1.4 %âãÏÓ 4 0 obj <>stream ÿØÿà

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.

Is this possible? What i'm doing wrong?

EDIT: I've read about unpack function, maybe this function can help me?

Thanks in advance

回答1:

Ok, i solved it. I have, basically, two errors:

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

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.