0

The scenario is, user will choose a .pdf file and we extract data from the PDF. We are using ionic3 with angular 5.

In HTML, we write

<ion-input type="file" accept="pdf/*" (change)="changeListener($event.target.files)">
<button ion-button large block type="submit" (click)="UploadCertificate()">Get Data</button>

In the .ts file,

declare var require: any;
fileToUpload: File;

changeListener(files: FileList): void {
    this.fileToUpload = files.item(0);
  }

UploadCertificate() {
    var pdfReader = require("pdfreader");
    new pdfReader.PdfReader().parseFileItems(this.fileToUpload.name, function (err, item) {
      if (item && item.text)
        console.log(item.text);
    });
  }

We are getting a error like, fs.readFileSync is not a function. we are using https://www.npmjs.com/package/pdfreader. Please suggest.

2
  • 1
    the pdfreader package is for serverside code, where it can read the file from the disk (via fs.readFileSync) Your code is running on the clientside in a browser, which has no direct access to the filesystem, thus cannot use node's fs module ... It's also written in the very beginning of the package description "This module is meant to be run using Node.js only. It does not work from a web browser." Furthermore, citing the package's docs, it's based on pdf.js, which is available in the browser. Maybe you can use that ... Commented Sep 9, 2021 at 12:56
  • Thanks for the help. Will Pdf.js run in cordova? Commented Sep 9, 2021 at 12:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.