Angular2 Displaying PDF

前端 未结 4 937
暗喜
暗喜 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 21:52

    ANGULAR 5

    I had the same problem which I lost few days on that.

    Here my answer may help others, which helped to render pdf.

    For me even though if i mention as responseType : 'arraybuffer', it was unable to take it.

    For that you need to mention as responseType : 'arraybuffer' as 'json'.(Reference)

    Working code

    service.ts

    downloadPDF(): any {
    return this._httpClient.get(url, { responseType: 'blob' as 'json' })
            .map(res => {
            return new Blob([res], { type: 'application/pdf', });
        });
    

    }

    component.ts

    this.myService.downloadPDF().subscribe(
        (res) => {
        var fileURL = URL.createObjectURL(res);
        window.open(fileURL);
        }
    );
    

    Referred from the below link

    https://github.com/angular/angular/issues/18586

    0 讨论(0)
  • 2020-12-07 21:59

    Angular v.5.2.1 answer:

    In *.services.ts

    downloadPDF(): any {
        return this._httpClient.get(url, { responseType: 'blob'})
                .map(res => {
                return new Blob([res], { type: 'application/pdf', });
            });
      }
    

    And then in *.component.ts

    downloadPDF() {
        let tab = window.open();
        this._getPdfService
          .downloadPDF()
          .subscribe(data => {
            const fileUrl = URL.createObjectURL(data);
            tab.location.href = fileUrl;
          });
      }
    

    Its important to initiate and open a new tab before calling the service. Then set this tabs url to the fileurl.

    0 讨论(0)
  • 2020-12-07 22:00

    First of all, you need to set options for your http request - set responseType to ResponseContentType.Blob, use blob() to read response as blob and set its type to application/pdf:

    downloadPDF(): any {
        return this._http.get(url, { responseType: ResponseContentType.Blob }).map(
        (res) => {
                return new Blob([res.blob()], { type: 'application/pdf' })
            }
    }
    

    Then, in order to get the file, you need to subscribe to it and you can create new ObjectURL and use window.open() to open PDF in new tab:

    this.myService.downloadPDF().subscribe(
            (res) => {
            var fileURL = URL.createObjectURL(res);
            window.open(fileURL);
            }
        );
    
    0 讨论(0)
  • 2020-12-07 22:00

    Angular 2+ Example PDF-viewer

    Page.html

    <div class="container">
    <div>
        <label>PDF src</label>
        <input type="text" placeholder="PDF src" [(ngModel)]="pdfSrc">
    </div>
    <div>
        <label>Page:</label>
        <input type="number" placeholder="Page" [(ngModel)]="page">
    </div>
    <div class="row">
        <div class="col-md-5">
            <div class="thumbnail" style="width: 150px;height: 200px;" (click)="pdfShow=!pdfShow">
                <p>click me to view PDF in Iframe!</p>
                <pdf-viewer [src]="pdfSrc"
                            [page]="page"
                            [original-size]="false"
                            style="display: block;"
                 ></pdf-viewer>
            </div>
            <br>
        </div>
        <div class="col-md-7">
            <div *ngIf="!pdfShow">
                <h4>PDF in Iframe</h4>
                <iframe width="500" height="600" [src]="pageurl" type="application/pdf"></iframe>
            </div>
        </div>
    </div>
    <br><br>
    <h2> PDF in Object Tag</h2>
    <object width="500" height="600"  data="https://vadimdez.github.io/ng2-pdf-viewer/pdf-test.pdf" type="application/pdf"></object>
    

    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)
    ],
    
    0 讨论(0)
提交回复
热议问题