背景
尝试将使用iReport通过PHP编写的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文件大小:
谢谢!
答案 0 :(得分:2)
我之前遇到过用Java编写的问题,因为它会使用UTF-16。来自http://zeronine.org/lab/index.php的outputPDF
函数使用java_set_file_encoding("ISO-8859-1");
。因此:
java_set_file_encoding("ISO-8859-1");
$em = java('net.sf.jasperreports.engine.JasperExportManager');
$result = $em->exportReportToPdf($pm);
header('Content-Length: ' . strlen( $result ) );
echo $result;
答案 1 :(得分:0)
您只需编写输出。确保: