使用 PHP 将二进制流写入浏览器
背景
尝试通过 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 字节
谢谢!
Background
Trying to stream a PDF report written using iReport through PHP to the browser. The general problem is: how do you write binary data to the browser using PHP?
Working Code
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 );
Non-working Code
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;
Question
How can I take out the middle step of writing to the file and write directly to the browser so that the PDF is not corrupted?
Update
PDF file sizes:
- Working: 594778 bytes
- Non-working: 1059365 bytes
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前在使用 Java 进行编写时遇到过问题,因为它使用 UTF-16。来自 http://zeronine.org/lab/index.php 的函数
outputPDF
使用java_set_file_encoding("ISO-8859-1");
。因此:I've previously experienced problems writing from Java because it'll use UTF-16. The function
outputPDF
from http://zeronine.org/lab/index.php usesjava_set_file_encoding("ISO-8859-1");
. Thus:您只需编写输出即可。确保:
You just write the output. Make sure that: