バックグラウンド
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バイト
ありがとうございました!