Skip to content

Instantly share code, notes, and snippets.

@tim-rohrer
Created July 22, 2020 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-rohrer/96dce87cbd0182d4706377ce6ebab845 to your computer and use it in GitHub Desktop.
Save tim-rohrer/96dce87cbd0182d4706377ce6ebab845 to your computer and use it in GitHub Desktop.
parsePDFStatement = async () => {
try {
const pdfPath = this.setPDFPath();
const loadingTask = this.pdf.getDocument(pdfPath);
await loadingTask.promise
.then((doc) => {
const { numPages } = doc;
console.log("# Document Loaded");
console.log(`Number of Pages: ${numPages}`);
console.log();
let lastPromise = doc.getMetadata().then((data) => {
console.log("# Metadata Is Loaded");
console.log("## Info");
console.log(JSON.stringify(data.info, null, 2));
console.log();
if (data.metadata) {
console.log("## Metadata");
console.log(JSON.stringify(data.metadata.getAll(), null, 2));
console.log();
}
const loadPage = (pageNum) => doc.getPage(pageNum).then((page) => {
console.log(`# Page ${pageNum}`);
const viewport = page.getViewport({ scale: 1.0 });
console.log(`Size: ${viewport.width}x${viewport.height}`);
console.log();
return page
.getTextContent()
.then((content) => {
// Content contains lots of information about the text layout and
// styles, but we need only strings at the moment
const strings = content.items.map((item) => item.str);
console.log("## Text Content");
console.log(strings.join(" "));
})
.then(() => {
console.log();
});
});
// Loading of the first page will wait on metadata and subsequent loadings
// will wait on the previous pages.
for (let i = 1; i <= numPages; i += 1) {
lastPromise = lastPromise.then(loadPage.bind(null, i));
}
return lastPromise;
});
}, (reason) => { throw new Error(reason); });
} catch (error) {
throw new Error("It be broken!");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment