简体   繁体   English

在 Angular2 组件中嵌入 pdf

[英]Embed pdf in Angular2 component

In angular 4.3 I want to open a modal popup and show an embedded pdf file, like shown here: https://pdfobject.com/static.html在 angular 4.3 中,我想打开一个模态弹出窗口并显示一个嵌入的 pdf 文件,如下所示: https : //pdfobject.com/static.html

I used the modal popup component from this answer which pops up nicely.我使用了这个答案中模态弹出组件,它可以很好地弹出。 My test html for the modal looks like this:我的模态测试 html 如下所示:

<a class="btn btn-default" (click)="setContent();modal.show()">Show</a>
<app-modal #modal>
  <div class="app-modal-header">
    Testing pdf embedding
  </div>
  <div class="app-modal-body">
    <div class="embed-responsive" *ngIf="ContentUrl">
      <object [attr.data]="ContentUrl"
              type="application/pdf"
              class="embed-responsive-item">
        <embed [attr.src]="ContentUrl"
               type="application/pdf" />
      </object>
    </div>
    <p><a [href]="ContentUrl">PDF Download</a></p>
  </div>
  <div class="app-modal-footer">
    <button type="button" class="btn btn-default" (click)="modal.hide()">Close</button>
  </div>
</app-modal>

In my component I set ContentUrl like this:在我的组件中,我像这样设置ContentUrl

public ContentUrl: SafeUrl;
public setContent() {
    this.ContentUrl = this._sanitizer.bypassSecurityTrustResourceUrl("/img/test.pdf");
}

The popup opens nicely, but it does not show the embedded pdf.弹出窗口很好地打开,但它没有显示嵌入的 pdf。 It does not even try to load the url from the service.它甚至不会尝试从服务加载 url。 The download link works nicely and asks if the pdf should be saved to disc or opened.下载链接运行良好,并询问是否应将 pdf 保存到光盘或打开。
I tried to embed the pdf outside of the popup to no avail, too.我也尝试将 pdf 嵌入到弹出窗口之外也无济于事。
I tried Chrome, Edge and IE.我尝试了 Chrome、Edge 和 IE。 None displays the embedded pdf.无显示嵌入的 pdf。

So how do I show/embed a pdf in an angular component?那么如何在角度组件中显示/嵌入 pdf 呢?

I ended up creating the object using innerHtml.我最终使用innerHtml创建了object

In the template:在模板中:

<div *ngIf="innerHtml"
  [innerHTML]="innerHtml">
</div>

In the code behind:在后面的代码中:

public innerHtml: SafeHtml;
public setInnerHtml(pdfurl: string) {
    this.innerHtml = this._sanitizer.bypassSecurityTrustHtml(
        "<object data='" + pdfurl + "' type='application/pdf' class='embed-responsive-item'>" +
        "Object " + pdfurl + " failed" +
        "</object>");
}

This way the object already has it's data attribute set upon creation.这样,对象在创建时就已经设置了它的数据属性。 At least now the PDF is embedded like I wanted to.至少现在 PDF 是像我想要的那样嵌入的。

Try to use a module for opening a PDF inside your app, for eg. 尝试使用模块在应用程序内打开PDF,例如。 https://www.npmjs.com/package/ng2-pdf-viewer . https://www.npmjs.com/package/ng2-pdf-viewer

Check this answer: Angular 2 how to display .pdf file . 检查这个答案: Angular 2如何显示.pdf文件

this code worked for me in angular 9这段代码在 angular 9 中对我有用

import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser'

public innerHtml: any;

constructor(
private domSanitizer: DomSanitizer
) { }

const fileURL = URL.createObjectURL(result);

  this.innerHtml = this.domSanitizer.bypassSecurityTrustHtml(
    "<object data='" + fileURL + "' type='application/pdf' class='embed-responsive-item'>" +
    "Object " + fileURL + " failed" +
    "</object>");

image形象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM