0

I am viewing a pdf file using v9 of ng2-pdf-viewer to display a local pdf file. everything works fine until I tried to print. I get an compile error trying to use the print() on the pdf object. apparently it doesn't exist anymore. How do I print the pdf file I am viewing. This is my code:

.html:

<div class="pdf-container" >
    <pdf-viewer *ngIf="!pdfLoaded"
      [src]="pdfSrc"
      [render-text]="true"
      [original-size]="false"
      style="width: 100%; height: 500px"
      (after-load-complete)="onPdfLoad($event)"
      >
    </pdf-viewer>

    <mat-dialog-actions>
        <button class="mat-raised-button" (click)="close()">Close</button>
        <!-- <button class="print-button" (click)="printPdf()">Print PDF</button> -->
        <button class="print-button" (click)="printPdf()">Print PDF</button>
      </mat-dialog-actions>
</div>

.ts

import { Component, Inject } from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
import { PDFDocumentProxy } from 'ng2-pdf-viewer';
import { from } from 'rxjs';

@Component({
  selector: 'app-docu-modal',
  templateUrl: './docu-modal.component.html',
  styleUrls: ['./docu-modal.component.scss']
})
export class DocuModalComponent {
 pdfLoaded: boolean = false; 
 isPrinting: boolean = false;
 pdfSrc: string = ''
 
 pdf!: PDFDocumentProxy;


  constructor( private dialogRef: MatDialogRef<DocuModalComponent>,
              @Inject(MAT_DIALOG_DATA) data: any) {
                   this.pdfSrc = data.pdfSrc;
                       }
 

   onPdfLoad(pdf:PDFDocumentProxy ) {
      this.pdf = pdf;
      console.log("Total pages:", pdf.numPages);
    }
   printPdf() {
      console.log(`PDF DATA ${this.pdf } `);
      if (this.pdf) {
        const printOptions = {
          paperWidth: 8.5,
          paperHeight: 11,
          orientation: 'portrait',
        };
        this.pdf.print(printOptions);  **/THIS LINE FAIL ON COMPILE/**
      }
    }

    close() {
      this.dialogRef.close();
   }

}

error TS2339: Property 'print' does not exist on type 'PDFDocumentProxy'.

42         this.pdf.print(printOptions);

also, if I use pdf: any; instead of pdf: PDFDocumentProxy I still get this.pdf.print() is not a function. ~~~~~

MichaelE
  • 757
  • 14
  • 42

0 Answers0