浏览器内的 pdf 报告视图

发布于 2024-08-09 02:24:22 字数 1800 浏览 11 评论 0原文

我使用 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 中完美查看,因为它触发了 Adob​​e Acrobat 插件。问题是,当我在 Mozilla Firefox 3.5.4 和 Google Chrome 4.0.233 中打开报告时,它向我显示了 pdf 文件的二进制字符串内容,而不是触发 Adob​​e Acrobat 插件。

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

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

I have created this report using BIRT and 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());


?>

The report viewed perfectly inside Internet Explorer 8.0 as it triggered the Adobe Acrobat plugin. The problem is when I opened the report inside Mozilla Firefox 3.5.4 and Google Chrome 4.0.233 it showed me the binary string content of the pdf file, instead of triggering the Adobe Acrobat plugin.

I have checked this by putting a pdf file in the htdoc folder and call it from Firefox and Chrome, it worked just fine. But why the header wont work for the report?

*Also why the header only work for IE 8.0? I need the report to be viewed in all major browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光沙漏 2024-08-16 02:24:22

内容类型应该是“application/pdf”

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

我认为如果您更改

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

上面的代码,提到的浏览器应该开始正确渲染 pdf 文档(前提是它们如此配置),并且 IE 将继续正常工作(IE 有一些 自动 MIME 类型检测(基于内容的前几百字节),这是喜忧参半...)

Content-type should be "application/pdf"

(or "application/x-pdf", for more recent pdf formats)

I think that if you change

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

in the code above, the browsers mentioned should start rendering the pdf documents properly (provided they are so configured), and IE will continue working ok (IE has some Automatic MIME Type Detection (based on the first few hundred bytes of content), which is a mixed blessing...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文