从角度为 5 的字节数组中打开 pdf [英] Open pdf from bytes array in angular 5

查看:24
本文介绍了从角度为 5 的字节数组中打开 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照以下链接在我的 angular 5 应用程序的新选项卡中显示 pdf 页面.但无法达到结果.

我正在使用 spring 控制器 api 中的字节数组.

用于理解发布 java spring 控制器代码.

控制器.java

@GetMapping(value = "/pdf")公共 ResTest generatePDF(HttpServletResponse response) 抛出 IOException {ResTest 测试 = new ResTest();ByteArrayOutputStream baos = docTypeService.createPdf();test.setByteArray(baos.toByteArray());test.setByteString(new String(baos.toByteArray()));返回测试;}

解决方案

最后,我能够渲染 pdf.我这边有两个小错误.

第一个问题是,我在 HttpHeaders 中给出了responseType",这是错误的.它应该在外面,如下所示.

第二个问题是,即使您提到 responseType : 'arraybuffer',它也无法接受.为此,您需要提及 responseType : 'arraybuffer' 为 'json'.(参考)

以下更正且有效的代码.

试用 3

component.ts(无变化)

clickEvent(){this.service.getPDF().subscribe((响应)=>{let file = new Blob([response], { type: 'application/pdf' });var fileURL = URL.createObjectURL(file);window.open(fileURL);})

service.ts

getPDF(){const url = `${this.serviceUrl}/pdf`;const httpOptions = {'responseType' : 'arraybuffer' 为 'json'//'responseType' : 'blob' as 'json'//这也有效};返回 this.http.get(url, httpOptions);}

参考以下链接

https://github.com/angular/angular/issues/18586

I was following the below links for displaying pdf page in new tab in my angular 5 application. But unable to achieve the result.

I am consuming the bytes array from spring controller api.

PDF Blob is not showing content, Angular 2

PDF Blob - Pop up window not showing content

Angular2 Displaying PDF

I tried the below options but none of them is working.

Trial 1

Consumed the response as json

component.ts

clickEvent(){
this.service.getPDF().subscribe((response)=>{

  let file = new Blob([response.byteString], { type: 'application/pdf' });            
  var fileURL = URL.createObjectURL(file);
  window.open(fileURL);
})

}

service.ts

getPDF(){
const url = `${this.serviceUrl}/pdf`;

const httpOptions = {
  headers: new HttpHeaders(
    {
      'Accept': 'application/json',
      'responseType':'blob'
    }
  )
};

return this.http.get<any>(url, httpOptions);

}

Trial 2

Consumed the response as json

component.ts

clickEvent(){
this.service.getPDF().subscribe((response)=>{

  let file = new Blob([response.byteArray], { type: 'application/pdf' });            
  var fileURL = URL.createObjectURL(file);
  window.open(fileURL);
})

}

service.ts

getPDF(){
const url = `${this.serviceUrl}/pdf`;

const httpOptions = {
  headers: new HttpHeaders(
    {
      'Accept': 'application/json',
      'responseType':'arraybuffer'
    }
  )
};

return this.http.get<any>(url, httpOptions);

}

Trial 3

Consumed the response as bytes

component.ts

clickEvent(){
this.service.getPDF().subscribe((response)=>{

  let file = new Blob([response], { type: 'application/pdf' });            
  var fileURL = URL.createObjectURL(file);
  window.open(fileURL);
})

}

service.ts

getPDF(){
const url = `${this.serviceUrl}/pdf`;

const httpOptions = {
  headers: new HttpHeaders(
    {
      'responseType':'blob'                 //both combination
      //'responseType'  : 'arraybuffer'
    }
  )
};

return this.http.get<any>(url, httpOptions);

}

By all the combination I am only getting two results. Empty pdf document or Failed to load PDF document.

For understanding posting java spring controller code.

controller.java

@GetMapping(value = "/pdf")
public ResTest generatePDF(HttpServletResponse response) throws IOException {
    ResTest test = new ResTest();
    ByteArrayOutputStream baos = docTypeService.createPdf();
    test.setByteArray(baos.toByteArray());
    test.setByteString(new String(baos.toByteArray()));
    return test;
}

解决方案

At last, I was able to render pdf. There were two small mistakes from my side.

1 st Problem was, I gave 'responseType' inside HttpHeaders which was wrong. It should be outside as below.

2 nd Problem was, even though if you mention as responseType : 'arraybuffer', it was unable to take it. For that you need to mention as responseType : 'arraybuffer' as 'json'.(Reference)

The corrected and working code below.

Trial 3

component.ts (nochanges)

clickEvent(){
  this.service.getPDF().subscribe((response)=>{

  let file = new Blob([response], { type: 'application/pdf' });            
  var fileURL = URL.createObjectURL(file);
  window.open(fileURL);
})

service.ts

getPDF(){
const url = `${this.serviceUrl}/pdf`;

const httpOptions = {
  'responseType'  : 'arraybuffer' as 'json'
   //'responseType'  : 'blob' as 'json'        //This also worked
};

return this.http.get<any>(url, httpOptions);

}

Referred from the below link

https://github.com/angular/angular/issues/18586

这篇关于从角度为 5 的字节数组中打开 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆