我有一个应用程序,我在其中显示我之前创建的pdf:
服务器端
public ResponseEntity<InputStreamResource>
getMsgboxSummaryPdf(HttpServletRequest request, HttpServletResponse response){...
InputStream stream = new ByteArrayInputStream(report.getBytes(StandardCharsets.UTF_8));
response = new ResponseEntity<InputStreamResource>(new InputStreamResource(stream),
new ReportsService().createPdfHeader(report), HttpStatus.OK);
return response;
}
/**
* Create PDF header.
*
* @param report
* report to write PDF
* @return PDF header
*/
public HttpHeaders createPdfHeader(String report) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, POST, PUT");
headers.add("Access-Control-Allow-Headers", "Content-Type");
headers.add("Content-Disposition", "filename=sysparamspdf");
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.setContentLength(report.getBytes(StandardCharsets.UTF_8).length);
return headers;
}
客户端
success : function(response) {
var data = this.toBinaryString(response.responseText);
data = "data:application/pdf;base64,"+btoa(data);
var window = this.createWindowPdf(data)
...
toBinaryString: function(data) {
var ret = [];
var len = data.length;
var byte;
for (var i = 0; i < len; i++) {
byte=( data.charCodeAt(i) & 0xFF )>>> 0;
ret.push(String.fromCharCode(byte) );
}
return ret.join('');
},
该报告工作正常,但我不知道如何更改pdfs查看器标题中出现的奇怪字符。
有人能给我一个想法吗?
提前致谢。
答案 0 :(得分:0)
尝试通过标头设置文件名:
{{1}}
然后将responseHeaders设置为responseEntity。