2

我正在使用ngx-extended-pdf-viewer ( npm ) 呈现 PDF,当我直接设置路径 + 文件名时它可以工作,如下所示:

 <ngx-extended-pdf-viewer [src]="'assets/example.pdf'" 
            [showPrintButton]="false" [showBookmarkButton]="false"
            [showOpenFileButton]="false" 
            [showSidebarOnLoad]="false"
            [showSidebarButton]="true" 
            delayFirstView="6000" useBrowserLocale="false">
          </ngx-extended-pdf-viewer>

我想在 .TS 中创建一个变量并将其绑定到 [src] 中,如下所示:

打字稿

...
ngOnInit() {
    this.filePathAndName = "'assets/example.pdf'";
...

HTML

 <ngx-extended-pdf-viewer [src]="{{filePathAndName}}" 
            [showPrintButton]="false" [showBookmarkButton]="false"
            [showOpenFileButton]="false" 
            [showSidebarOnLoad]="false"
            [showSidebarButton]="true" 
            delayFirstView="6000" useBrowserLocale="false">
          </ngx-extended-pdf-viewer>

但它不起作用。

主要问题是 [ src ] 需要有两个符号:引号 ( " ) 后跟撇号 ( ' ) ... " ' path+name ' " (没有空格)

我的问题是:如何在 Typescript 中的变量中放入有效值,以便在这种特定情况下在 HTML 中正确呈现?

4

2 回答 2

2

它不需要那些引号。这些仅用于文字字符串绑定。尝试这个:

this.filePathAndName = "assets/example.pdf";

<ngx-extended-pdf-viewer *ngIf="filePathAndName" [src]="filePathAndName" 
于 2019-03-19T22:28:40.627 回答
0

可能会迟到回复,将来会对某人有所帮助。

HTML 代码:

  <ngx-extended-pdf-viewer *ngIf="currentPdf"
    [src]="currentPdf" useBrowserLocale="false"
    style="height: 100%; width: 100%"
    [delayFirstView]="1000"
    [showHandToolButton]="true"
    [handTool] = false>
  </ngx-extended-pdf-viewer>

打字稿代码:

public currentPdf: string

displayPdf() {

    // setTimeout(() => {
      this.service.getPdfExtractedContent(this.id)
        .pipe(first())
        .subscribe(
          data => {
            this.currentPdf = URL.createObjectURL(this.b64toBlob(data.ExtractedByte,'data:application/pdf;base64', 1024));

          },
          error => {
            console.log(error);
          }
        );
    // }, 500);

  }


  b64toBlob(b64Data, contentType, sliceSize) {
    const byteCharacters = atob(b64Data);
    const byteArrays = [];

    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
      const slice = byteCharacters.slice(offset, offset + sliceSize);

      const byteNumbers = new Array(slice.length);
      for (let i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }

      const byteArray = new Uint8Array(byteNumbers);
      byteArrays.push(byteArray);
    }

    const blob = new Blob(byteArrays, {type: contentType});
    return blob;
  }
于 2019-07-10T14:52:45.870 回答