-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf-loader.js
More file actions
23 lines (19 loc) · 743 Bytes
/
pdf-loader.js
File metadata and controls
23 lines (19 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const pdfParse = require('pdf-parse');
const fetch = require('node-fetch');
module.exports.loadPdfByUri = async (uri) => {
try {
const response = await fetch(uri).catch(error => { throw `Error - Can't fetch file. [${uri}]`; });
if (response.status !== 200) {
throw `Error - Can't fetch file. [${uri}] [status: ${response.status}]`;
}
// Check if file is PDF file
const responseText = await response.text();
if (responseText.substring(0, 5) !== '%PDF-') {
throw `Error - File is not a PDF file. [${uri}] [status: ${response.status}]`;
}
const data = await pdfParse(response);
return data;
} catch (error) {
throw error;
}
}