Reputation: 1
I have been trying to use pdf-parse plugin on cypress to validate the context of some pdfs but I get the error "Fs.readFileSync is not a function". I am on version 12.4.1 but I did try other cypress versions with the same results (6.0.0, 7.5.0, 8.5.0, 9.0.0, 10.0.0). How do I use cypress pdf-parse? If you know any other way to parse the pdf content, please guide me as I have used pdfjs-dist and pdf2json with no success. Bellow is my code on one of my tries:
/// \<reference types="Cypress" /\>
import PdfParse from 'pdf-parse';
import Loader from './loader.js';
import Login from './login.js'
import PageActions from './pageActions.js';
import Validations from './validations.js';
describe('', () =\> {
const login = new Login()
const loader = new Loader()
const pageActions = new PageActions()
const validations = new Validations()
it('', () =\> {
cy.visit('/')
login.userName()
login.password();
login.clickSignIn()
loader.waitForLoaderToFinish()
pageActions.clickView();
loader.waitForLoaderToFinish();
Actions.clickLibrary();
loader.waitForLoaderToFinish()
pageActions.clickDownload();
loader.waitForLoaderToFinish();
validations.validatePDFHasBeenDownloaded();
cy.readFile('./cypress/downloads/Application Form.pdf')
.then(pdfBuffer =\> {
const options = {}; // optional options object
return pdfParse.pdf2json(pdfBuffer, options);
}).then(pdfData =\> {
cy.log(pdfData)
});
});
})
The problem seems to be with the "import PdfParse from 'pdf-parse';" but I have no clue what to do. Thank you in advance!!
I have tried to install it on versions 6.0.0, 7.5.0, 8.5.0, 9.0.0, 10.0.0 and I also have tried to use pdf2json and pdf-dist
Upvotes: 0
Views: 750
Reputation: 11
I've been experiencing the same issue. It seems to occur due to browser restrictions, which prevent the use of fs.readFileSync. One option is to use Electron, but it appears that even its latest version is blocking such usage. In addition to that, we can modify the index.js file within the "node_modules/parse-pdf" folder with the following code:
const fs = require("fs");
const Pdf = require('./lib/pdf-parse.js');
module.exports = Pdf;
let isDebugMode = !module.parent;
//process.env.AUTO_KENT_DEBUG
// Check if the environment is the client (browser)
if (typeof window === 'undefined') {
//for testing purpose
if (isDebugMode) {
let PDF_FILE = './test/data/05-versions-space.pdf';
let dataBuffer = fs.readFileSync(PDF_FILE);
Pdf(dataBuffer).then(function(data) {
fs.writeFileSync(`${PDF_FILE}.txt`, data.text, {
encoding: 'utf8',
flag: 'w'
});
debugger;
}).catch(function(err) {
debugger;
});
}
}
Feel free to use this modified code to address the issue, especially in scenarios where the fs.readFileSync usage is restricted by browsers. Additionally, the modification of the index.js file should help in resolving the problem.
Upvotes: 1