【问题标题】:Angular 5- PDF document , download and show in the new windowAngular 5- PDF 文档,下载并在新窗口中显示
【发布时间】:2018-12-19 03:01:49
【问题描述】:

我正在使用 Angular 作为前端,我正在从 REST 完整 Web 服务获取数据。 1) 我需要在 Web 浏览器的新窗口中显示 pdf 文件,我从 Web 服务获取数据为 base 64。

问题:我可以下载文件,但是当我尝试在新窗口中打开文件时,它会显示(错误:无法加载 pdf 内容)。这是我的代码:

服务:

this.http.get('https://localhost:44364/api/v1/source/GetImage/parameter1/animal.pdf', { responseType:'arraybuffer' })
    .subscribe(response => {
    var file = new Blob([response], {type: 'application/pdf'});
    var fileURL = URL.createObjectURL(file);

    this.content=this.sanitizer.bypassSecurityTrustResourceUrl(fileURL);
});

HTML:在 HTML 中我使用 IFRAME 来显示 pdf 数据

iframe class="e2e-iframe-trusted-src" width="640" height="390" [src]="content"

谁能帮帮我。谢谢

【问题讨论】:

    标签: angular pdf iframe


    【解决方案1】:

    为了下载 pdf 文件,你可以试试下面的代码。您无法在桌面上打开 pdf 文件。

      this.httpClient.get('../assets/sample.pdf', { responseType: 'arraybuffer' 
        }).subscribe(response => {
          console.log(response);
    
          const file = new Blob([response], {type: 'application/pdf'});
          const fileURL = window.URL.createObjectURL(file);
          /*const link = document.createElement('a');
          link.href = fileURL;
          link.download = 'sample.pdf';
          link.click();*/
          if (window.navigator.msSaveOrOpenBlob) {
               window.navigator.msSaveOrOpenBlob(file, fileURL.split(':')[1] + '.pdf');
          } else {
               window.open(fileURL);
          }
       });
    

    }

    【讨论】:

    • 嗨 Suresh,我想在浏览器的新窗口 (window.open()) 中打开它,而不是下载文件。
    • 嗨,suresh,我尝试了上面的代码,相同的 window.open(fileURL) 在新窗口中打开 pdf 文件,但它仍然抛出相同的错误(加载 PDF 文档失败),如果你有什么想法可以帮帮我。
    • 可能是 pdf 文件的问题。您可以尝试使用其他 pdf 文件
    • 嗨 Suresh,我认为我的 pdf 数据有问题。现在我可以纠正我的错误,但仍然无法将数据写入 pdf。形成网络服务,我得到了 base 64 字符串: const httpOptions = { 'responseType' : 'string' as 'json' }; this.http.get('https://..animal.pdf',httpOptions).subscribe((response)=>{ const a=atob(response); const file=new Blob([a], {type:'application/pdf'}) const fileURL=window.URL.createObjectURL(file); window.open(fileURL);
    • 处理 pdf 的更好方法是我必须从 web api 而不是 base 64 字符串或 byte[] 数组中获取 PDF 文件。你能推荐我吗
    【解决方案2】:

    如果您想显示 base 64 pdf,您可以使用我的组件 https://www.npmjs.com/package/ng2-image-viewer

    它适用于 angular 2+,它可以渲染 Base 64 图像、URL 图像和 Base 64 PDF,它很容易实现,您只需:

    1) 运行npm install ng2-image-viewer --save

    2) 在 angular-cli.json 文件中添加这些代码:

    "styles": [
         "../node_modules/ng2-image-viewer/imageviewer.scss"
    ],
    "scripts": [
         "../node_modules/ng2-image-viewer/imageviewer.js"
    ],
    

    3) 导入 ImageViewerModule

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
    
        // Specify your library as an import
        ImageViewerModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    4) 现在你可以随心所欲地使用它了:

    <!-- You can now use your library component in app.component.html -->
    <h1>
      Image Viewer
    </h1>
    <app-image-viewer [images]="['iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', 'https://picsum.photos/900/500/?random']"
    [idContainer]="'idOnHTML'"
    [loadOnInit]="true"></app-image-viewer>
    

    这是它的演示Ng2-Image-Viewer Showcase

    有关更多信息,您可以查看上面的链接:)

    【讨论】:

      【解决方案3】:

      如果在新窗口中打开文件没有问题,并且您使用的是现代浏览器,例如 chrome,它们可以自动处理 pdf,

      window.open(URL, '_blank');
      

      如果您需要手动完成所有这些工作,您可以使用 npm 包,例如,

      https://www.npmjs.com/package/ng2-pdf-viewer
      

      如果您对 pdf 查看器的要求不高,例如导出邮件下载等,您可以使用 HTML 元素

      <embed src="file_name.pdf" width="800px" height="2100px" />
      

      确保根据需要更改宽度和高度。祝你好运!

      【讨论】:

      • 您好 Akshay,感谢您的回复。首先,我尝试使用 window.open(URL,'_blank') 打开 pdf,它抛出相同的错误,例如(错误:无法加载 pdf 文档。在浏览器中)
      • 复制该 URL 并粘贴到新选项卡中,如果它应该打开一个有效的 pdf URL。例如 w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf 尝试将上面的链接粘贴到新标签中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 2017-12-05
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多