gpt4 book ai didi

javascript - 显示 Jasper Reports Server 提供的 pdf

转载 作者:行者123 更新时间:2023-12-03 03:45:04 27 4
gpt4 key购买 nike

我的问题

我正在检索使用 Jasper Reports Server 生成的报告,目的是在新选项卡中向用户显示它。我可以很好地创建新选项卡,但 pdf 完全是空白的。

我的代码

我必须文件,一个 php 文件向 jasperserver 发出请求,另一个文件查询该文件并使用 javascript 创建新选项卡。JavaScript:

sap.ui.getCore().attachInit(function () {
var onPress = function(evt){
jQuery.ajax({
type: "GET",
url: "JasperReportAPICall.php",
xhrFields: {responseType: "text/pdf"},
data: {
functionname: "authenticate",
argument: "http://localhost:8080/jasperserver/rest_v2/reports/reports/interactive/Directives_Report.pdf?id=1&method=rules"
},
success: function(response){
var blob = new Blob([response], {type: "application/pdf"});
var pdfUrl = window.URL.createObjectURL(blob);
var newTab = window.open(pdfUrl);
newTab.addEventListener('load', function(pdfUrl){
window.URL.revokeObjectURL(pdfUrl);
}, false);
console.log("Report succesfully received");

}
});
};
new sap.m.Button({
text: "GNR",
press: onPress
}).placeAt("content");
});

PHP:

function authenticate($url){
global $username, $pwd;

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_GET, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$pwd");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);
if (result===false){
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = curl_error($curl);
echo "HTTPCODE: $httpcode";
echo "ERROR: $error";
}

curl_close($curl);
return $result;
}

if (isset($_GET['functionname']) && isset($_GET['argument'])){
if ($_GET['functionname'] === "authenticate"){

$result = authenticate($_GET['argument']);
echo $result;
}
}

我尝试过的

  • 将 xhr 响应类型设置为“blob”。 window.URL.createObjectURL 拒绝了
  • 将 xhr 响应类型设置为“arraybuffer”并使用所述 arraybuffer 创建一个新的 Blob。打开新选项卡后,pdf 查看器提示无法打开文件
  • 与上一步骤相同,但使用 File 对象。结果相同。

数据是什么样的

这是我从服务器收到的内容。

由于太大放不下,所以放在 pastebin 中.

最佳答案

我找到了修复方法。问题出在 PHP 方面。事实上,我检索了数据,但没有设置适当的 header ,因此当使用 xhr responseType 'blob' 时,它没有 mime 类型,导致使用时崩溃创建一个 url 对象。在将 pdf 返回到 ajax 调用之前,我像这样设置标题。

function authenticate($url){
...
curl_close($curl);
header('Cache-Control: public');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="new.pdf"');
header('Content-Length: '.strlen($result));
return $result;
}

这样,pdf blob 的服务就不会出现任何错误。

关于javascript - 显示 Jasper Reports Server 提供的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com