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.
pdfreaderpackage is for serverside code, where it can read the file from the disk (viafs.readFileSync) Your code is running on the clientside in a browser, which has no direct access to the filesystem, thus cannot use node'sfsmodule ... 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 onpdf.js, which is available in the browser. Maybe you can use that ...