Angular 13 PDF Viewer Example

Angular 13 pdf viewer example; Through this tutorial, i am going to show you how to create a pdf viewer in the angular 13 apps using ng2-pdf-viewer npm Package.

Angular 13 PDF Viewer Example Tutorial

Follow the below given steps to create pdf viewer in agnular 13 apps; as follows:

  • Step 1 – Create New Angular App
  • Step 2 – Install ng2-pdf-viewer npm Package
  • Step 3 – Import Modules in Module.ts File
  • Step 4 – Create HTML PDF Viewer on View File
  • Step 5 – Import Compnents in Component ts File
  • Step 6 – Start Angular App

Step 1 – Create New Angular App

Run the following command on command prompt to install angular app:

ng new my-new-app

Step 2 – Install ng2-pdf-viewer npm Package

Run the following command on command prompt to install ng2-pdf-viewer npm Package:

npm install ng2-pdf-viewer

Step 3 – Import Modules in Module.ts File

Go to src/app directory and open app.module.ts file. Then import 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 – Create HTML PDF Viewer on View File

Go to src/app/app.component.html and add the following code into it:

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

Step 5 – Import Components in Component ts File

Go to the src/app directory and open app.component.ts. Then import 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

Run the following commands on command prompt to start angular app:

ng serve

Recommended Angular Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.