Search code examples
javascriptangularpdf-viewer

Invoke a method of ng2-pdf-viewer from angular component


I am new to angular 5. I am working on ng2-pdf-viewer. I need to invoke one of its method updateSize() in that plugin from my component. Can anyone tell me how can I access it from a component.

Here is the link of the plugin

https://www.npmjs.com/package/ng2-pdf-viewer


Solution

  • You can use a template reference variable to access the public methods of ng2-pdf-viewer

    Add a template variable named #pdfViewer in the html file like so

      <pdf-viewer
        #pdfViewer
        [src]="reportObject.src"
        [page]="reportObject.currentPage"
        [render-text]="true"
        >
      </pdf-viewer>
    

    Use ViewChild decorator to reference it inside your component.

      import { PdfViewerComponent } from 'ng2-pdf-viewer';
      import {ViewChild} from '@angular/core';
    
      @ViewChild('pdfViewer') pdfComponent: PdfViewerComponent;
    

    You can now access the methods of ng2-pdf-viewer using the pdfComponent variable like so

      this.pdfComponent.updateSize();