Generate a new Angular application, if you do not have one already:

ng new PdfReader

Download pdfjs-dist from npm:

npm install pdfjs-dist

Create a service under src folder using the following command:

ng generate service PdfReader

Replace the contents of the generated service with the following:

import { Injectable } from '@angular/core';
import * as pdfjsLib from 'pdfjs-dist';

@Injectable({
  providedIn: 'root'
})
export class PdfReaderService {

  constructor() {
    pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';
  }

  public async readPdf(pdfUrl: string): Promise<string> {
    const pdf = await pdfjsLib.getDocument(pdfUrl);
    const countPromises = []; // collecting all page promises

    for (let i = 1; i <= pdf._pdfInfo.numPages; i++) {
      const page = await pdf.getPage(i);
      const textContent = await page.getTextContent();
      countPromises.push(textContent.items.map((s) => s.str).join(''));
    }

    const pageContents = await Promise.all(countPromises);
    return pageContents.join('');
  }
}

It is technically possible to use the pdf reader service from any typescript file in Angular.

For demonstration, I am using the app.component.ts to read the contents of the pdf file:

import { Component, OnInit } from '@angular/core';
import { PdfReaderService } from './pdf-reader.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  constructor(private pdfReader: PdfReaderService) { }

 ngOnInit() {
    this.pdfReader.readPdf('./assets/sample.pdf')
      .then(text => alert('PDF parsed: ' + text), reason => console.error(reason));
  }
}

In the example above, I happen to have an example pdf document under src/assets, called sample.pdf.

Run the application with ng serve and the file contents appear in an alert dialog:

Umut Esen

Umut is a certified Microsoft certified developer and has an MSc in Computer Science. He is currently working as a senior software developer in Edinburgh, UK. He is the primary author and the founder of onthecode.

This Post Has 3 Comments

  1. Igor

    Hi! How can I get the file in the form?

    1. Umut Esen

      You would need to save it somewhere accessible via a URL, for example to a backend server. Then pass the URL to pdfjs to read, good luck!

  2. Ciprian

    Hi,
    I tried to use the code in the article, and ng serve throws an error related to _pdfInfo and getPage, saying that they do not exist on type PDFDocumentLoadingTask.

    Could you give any suggestions on how to fix that?

    Thanks

Leave a Reply