【发布时间】:2018-01-08 00:59:47
【问题描述】:
我的问题
我正在检索使用 Jasper Reports Server 生成的报告,目的是在新选项卡中将其显示给用户。我可以很好地创建新标签,但 pdf 是完全空白的。
我的代码
我必须创建文件,一个向 jasperserver 发出请求的 php 文件,以及另一个查询该文件并使用 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 responseType 设置为“arraybuffer”并使用所述arraybuffer 创建一个新的Blob。打开新标签后,pdf 查看器抱怨无法打开文件
- 与上一步相同,但使用文件对象。结果相同。
数据是什么样子的
这是我从服务器收到的。
由于太大放在这里,我把它放在pastebin中。
【问题讨论】:
标签: javascript php sapui5 jasperserver