Angular2 Displaying PDF

前端 未结 4 945
暗喜
暗喜 2020-12-07 21:19

I have an angular2 project with an ASP.Net Web API. I have code to retrieve a file path from my database which goes to a document on my server. I then want to display this d

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 22:00

    Angular 2+ Example PDF-viewer

    Page.html

    click me to view PDF in Iframe!


    PDF in Iframe



    PDF in Object Tag

    page.ts

    import { Component } from '@angular/core';
    import { BrowserModule, DomSanitizer, SafeResourceUrl } from '@angular/platform- 
    browser'
    
    @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
     })
     export class AppComponent {
    
      pdfSrc = 'https://vadimdez.github.io/ng2-pdf-viewer/pdf-test.pdf';
      page: number = 1;
      pageurl: SafeResourceUrl;
    
      constructor(private domSanitizer: DomSanitizer) {
         }
          ngOnInit() {
          this.pageurl = this.domSanitizer.bypassSecurityTrustResourceUrl(this.pdfSrc);
         }
         title = 'Mohsen';
         }
    

    app.module.ts

    add

    import { FormsModule } from '@angular/forms'; 
    import { PdfViewerComponent } from 'ng2-pdf-viewer';
    import { HttpModule } from '@angular/http';.
    import {HttpClientModule} from '@angular/common/http';
    import { BrowserModule } from '@angular/platform-browser';
    

    import the

    imports: [
        BrowserModule,
        HttpClientModule,
        HttpModule,
        FormsModule,
        RouterModule.forRoot(appRoutes)
    ],
    

提交回复
热议问题