Doraemon

小叮当    2012 - 2023
Doraemon

Choose mode

  • dark
  • auto
  • light
首页
Category
  • 前端开发
  • 后端
  • 数据库
  • 运维
Tag
TimeLine
关于
  • 关于我
Contact
  • GitHub
author-avatar

小叮当

39

Article

25

Tag

首页
Category
  • 前端开发
  • 后端
  • 数据库
  • 运维
Tag
TimeLine
关于
  • 关于我
Contact
  • GitHub

Angular PDF 在线预览

小叮当    2012 - 2023

Angular PDF 在线预览


小叮当 2020-09-05 Angular前端开发

插件名称:ng2-pdfjs-viewer

GitHub 地址:ng2-pdfjs-viewer

此插件可以执行下载文件名([downloadFileName]="'mytestfile.pdf'") & 从新窗口打开([externalWindow]="true" & #pdfViewerAutoLoad)

 this.pdfViewerAutoLoad.pdfSrc = encodeURIComponent(pdfSrc); //js url编码 encodeURI()、encodeURIComponent()
 this.pdfViewerAutoLoad.refresh(); // Ask pdf viewer to load/refresh pdf
1
2

locale: pdf 多语言,en-US 英文、zh-CN 中文、zh-TW 繁体

在下面摘抄一些使用说明文档,如果遇到更新请以 GitHub 官网为准

# 1、安装 ng2-pdfjs-viewer,并且引入

npm install ng2-pdfjs-viewer --save
1
 







 






import { PdfJsViewerModule } from 'ng2-pdfjs-viewer'; // <-- Import PdfJsViewerModule module

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    PdfJsViewerModule // <-- Add to declarations
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 2、在组件中使用或者封装

简单使用

<!-- You can now use your library component in app.component.html -->
<h1>
  {{title}}
</h1>
<ng2-pdfjs-viewer pdfSrc="your pdf file path"></ng2-pdfjs-viewer>
1
2
3
4
5
<!-- your.component.html -->
<button (click)="openPdf();">Open Pdf</button>

<div style="width: 800px; height: 400px">
  <ng2-pdfjs-viewer
    #pdfViewerOnDemand
    [externalWindow]="true"
    [downloadFileName]="'mytestfile.pdf'"
    [openFile]="false"
    [viewBookmark]="false"
    [download]="false"></ng2-pdfjs-viewer>
</div>
<div>
<div style="width: 800px; height: 400px">
  <ng2-pdfjs-viewer #pdfViewerAutoLoad></ng2-pdfjs-viewer>
</div>
<div style="height: 600px">
  <ng2-pdfjs-viewer pdfSrc="gre_research_validity_data.pdf" viewerId="MyUniqueID" (onBeforePrint)="testBeforePrint()" (onAfterPrint)="testAfterPrint()" (onDocumentLoad)="testPagesLoaded($event)" (onPageChange)="testPageChange($event)">
  </ng2-pdfjs-viewer>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- your.component.ts-->
import { Component, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
...

export class MyComponent implements OnInit {
  @ViewChild('pdfViewerOnDemand') pdfViewerOnDemand;
  @ViewChild('pdfViewerAutoLoad') pdfViewerAutoLoad;
  ...

  constructor(private http: HttpClient) {
      let url = "api/document/getmypdf"; // Or your url
      this.downloadFile(url).subscribe(
          (res) => {
              this.pdfViewerAutoLoad.pdfSrc = res; // pdfSrc can be Blob or Uint8Array
              this.pdfViewerAutoLoad.refresh(); // Ask pdf viewer to load/refresh pdf
          }
      );
  }

  private downloadFile(url: string): any {
        return this.http.get(url, { responseType: 'blob' })
            .pipe(
                map((result: any) => {
                    return result;
                })
            );
    }

  public openPdf() {
    let url = "url to fetch pdf as byte array"; // E.g. http://localhost:3000/api/GetMyPdf
    // url can be local url or remote http request to an api/pdf file.
    // E.g: let url = "assets/pdf-sample.pdf";
    // E.g: https://github.com/intbot/ng2-pdfjs-viewer/tree/master/sampledoc/pdf-sample.pdf
    // E.g: http://localhost:3000/api/GetMyPdf
    // Please note, for remote urls to work, CORS should be enabled at the server. Read: https://enable-cors.org/server.html

    this.downloadFile(url).subscribe(
    (res) => {
        this.pdfViewerOnDemand.pdfSrc = res; // pdfSrc can be Blob or Uint8Array
        this.pdfViewerOnDemand.refresh(); // Ask pdf viewer to load/reresh pdf
      }
    );
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  • 1、安装 ng2-pdfjs-viewer,并且引入
  • 2、在组件中使用或者封装