Make PDF Viewer in Angular 12 with NG2 PDF Viewer

Angular 12 pdf viewer example; In this tutorial, i will show you how to install ng2-pdf-viewer npm package and create pdf viewer in angular 12.

The ng2-pdf-viewer is one of the most powerful library specially created for creating PDF viewer components in Angular applications. It supports Angular over 5 versions, it is a popular module for handling PDF related tasks in Angular; This plugin provides a myriad of features for PDF implementation in Angular.

PDF Viewer in Angular 12 Example

  • Step 1 – Create New Angular App
  • Step 2 – Install ng2-pdf-viewer npm Package
  • Step 3 – Import Modules on Module.ts File
  • Step 4 – Add Code on View File
  • Step 5 – Add Code On Component ts File
  • Step 6 – Start Angular App

Step 1 – Create New Angular App

Execute the following command on terminal to install angular app:

ng new my-new-app

Step 2 – Install ng2-pdf-viewer npm Package

Execute the following command on terminal to install ng2-pdf-viewer npm Package in our angular application:

npm install ng2-pdf-viewer

Step 3 – Import Modules on Module.ts File

Import pdf viewer module. So, visit src/app directory and open app.module.ts file. Then add the following code into it:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
  
import { AppComponent } from './app.component';
import { PdfViewerModule } from 'ng2-pdf-viewer';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    PdfViewerModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Step 4 – Add Code on View File

Create html component and for display pdf viewer in angular app. So, visit src/app/app.component.html and update the following code into it:

<h1>Angular 12 PDF File Viewer Example - laratutorials.com</h1>
  
<pdf-viewer [src]="pdfFilePath" 
              [render-text]="true"
              style="display: block;"
  ></pdf-viewer>

Step 5 – Add Code On Component ts File

Go to src/directory and open app.component.ts. Then add the following code into component.ts file:

import { Component } from '@angular/core';
  
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
    pdfFilePath = "https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf";
}

Step 6 – Start Angular App

Execute the following commands on terminal to start angular app:

ng serve

Recommended Angular Tutorials

Leave a Comment