Converting the acquired PDF to JSON - This was made easy after using the pdf-tables-parser package.
The code below gets the downloaded PDF from the input folder, extracts the data, and writes it to a report.json file in the out directory.
const { PdfDocument } = require('@pomgui/pdf-tables-parser');
const fs = require('fs');
async function PDFToJSON() {
const pdf = new PdfDocument();
await pdf.load('input/FOREX_CARD_RATES.pdf')
.then(() => fs.writeFileSync('out/report.json', JSON.stringify(pdf, null, 2), 'utf8'))
.catch((err) => console.error(err));
}
Here are the dependencies in the package.json for reference -
"dependencies": {
"@pomgui/pdf-tables-parser": "^0.1.0",
"download": "^8.0.0",
"path": "^0.12.7",
"pdfjs": "^2.4.7",
"pdfjs-dist": "^3.2.146"
}
The Issue
This code was inefficient as the JSON was not uniform. No doubt that all the contents of the PDF are scraped but it is too ugly and variable to look at.
Here is how the JSON looked
{
"tableNumber": 7,
"numrows": 1,
"numcols": 1,
"data": [
[
"UNITED STATES DOLLAR USD/INR "
]
]
},
{
"tableNumber": 8,
"numrows": 2,
"numcols": 9,
"data": [
[
"81.25",
" 82.75",
" 81.18",
" 82.92",
" 80.5",
" 83.2",
" 80.2",
" 83.4",
" 81.12"
],
[
"EURO EUR/INR "
]
]
},
The Python Fix
This amazing GitHub repository solved my entire issue of scraping the said PDF - https://github.com/sahilgupta/sbi_forex_rates
Thanks to Sahil's solution the task of scraping had been achieved, leaving me with modifying the existing code to scrape page 1 and upload it to Amazon S3 Bucket.