【发布时间】:2011-02-17 02:07:48
【问题描述】:
背景
尝试通过 PHP 将使用 iReport 编写的 PDF 报告流式传输到浏览器。一般的问题是:如何使用 PHP 将二进制数据写入浏览器?
工作代码
header('Cache-Control: no-cache private');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment, filename=climate-report.pdf');
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
$path = realpath( "." ) . "/output.pdf";
$em = java('net.sf.jasperreports.engine.JasperExportManager');
$result = $em->exportReportToPdf($pm);
header('Content-Length: ' . strlen( $result ) );
$fh = fopen( $path, 'w' );
fwrite( $fh, $result );
fclose( $fh );
readfile( $path );
无效代码
header('Cache-Control: no-cache private');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment, filename=climate-report.pdf');
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
$path = realpath( "." ) . "/output.pdf";
$em = java('net.sf.jasperreports.engine.JasperExportManager');
$result = $em->exportReportToPdf($pm);
header('Content-Length: ' . strlen( $result ) );
echo $result;
问题
如何去掉写入文件的中间步骤,直接写入浏览器,这样PDF才不会损坏?
更新
PDF 文件大小:
- 工作:594778 字节
- 非工作:1059365 字节
谢谢!
【问题讨论】:
-
是否添加了您从第一个工作中删除的所有标题?
-
@Casey:在这两种情况下,标题总是相同的。查看修改后的问题。
-
啊,这是气候报告!不用担心,这很正常。他们总是buggy ;)
-
我们一直在回显二进制数据,所以这不是您的问题。您从“Content-Length”哪里获得 PDF 文件大小?
-
@ZZ:将两个文件保存到磁盘,然后运行
ls。
标签: java php jasper-reports fopen php-java-bridge