浏览器内的pdf报告视图

时间:2009-10-29 06:24:44

标签: pdf browser content-disposition

我使用BIRT和phpjavabridge创建了此报告

<?php

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=downloaded.pdf");

require_once("http://127.0.0.1:8080/JavaBridge/java/Java.inc");
header("Content-type: text/html");



// the report file to render
$myReport = "test.rptdesign";

// load resources, .rpt files and images from the current working dir
$here = getcwd();

$ctx = java_context()->getServletContext();
$birtReportEngine =        java("org.eclipse.birt.php.birtengine.BirtEngine")->getBirtEngine($ctx);
java_context()->onShutdown(java("org.eclipse.birt.php.birtengine.BirtEngine")->getShutdownHook());


// Create a HTML render context

try{

// Load the report design
$design = $birtReportEngine->openReportDesign("${here}/${myReport}");
$task = $birtReportEngine->createRunAndRenderTask( $design );
$task->setParameterValue("sample", new java("java.lang.String", "Hello world!"));

// Add HTML render options
$options = new java("org.eclipse.birt.report.engine.api.PDFRenderOption");
$options->setOutputFormat($options->OUTPUT_FORMAT_PDF);

// Create the output
$out = new java("java.io.ByteArrayOutputStream");
$options->setOutputStream($out);
$task->setRenderOption($options);
$task->run ();
$task->close();

} catch (JavaException $e) {
    echo $e; //"Error Calling BIRT";

}


// Return the generated output to the client
echo java_values($out->toByteArray());


?>

该报告完全在Internet Explorer 8.0中查看,因为它触发了Adobe Acrobat插件。问题是当我在Mozilla Firefox 3.5.4和Google Chrome 4.0.233中打开报告时,它向我展示了pdf文件的二进制字符串内容,而不是触发Adobe Acrobat插件。

我已经通过将一个pdf文件放在htdoc文件夹中并通过Firefox和Chrome调用它来检查这个,它运行得很好。但为什么标题不适用于报告呢?

*为什么标题仅适用于IE 8.0?我需要在所有主要浏览器中查看报告

1 个答案:

答案 0 :(得分:1)

内容类型应为“application / pdf”

(或“application / x-pdf”,更新的pdf格式)

我认为如果你改变了

header("Content-type: text/html");
to
header("Content-type: application/pdf");

在上面的代码中,提到的浏览器应该开始正确地呈现pdf文档(只要它们是这样配置的),并且IE将继续正常工作(IE有一些Automatic MIME Type Detection(基于前几百个字节)内容),这是一个喜忧参半的祝福...)