From 3ad925aa358e340d098946f5666b34dfd32e3294 Mon Sep 17 00:00:00 2001 From: Daniel Manzke Date: Wed, 12 Apr 2023 16:53:41 +0200 Subject: [PATCH 1/3] extract attachments from pdf --- lib/index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/index.js b/lib/index.js index 48939c5..a7752c8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -76,6 +76,19 @@ class PDFExtract { const firstPage = (options && options.firstPage) ? options.firstPage : 1; const lastPage = Math.min((options && options.lastPage) ? options.lastPage : doc.numPages, doc.numPages); pdf.pdfInfo = doc.pdfInfo; + + pdf.attachments = []; + doc.getAttachments().then(attachments => {; + for (const attachment of Object.entries(attachments) ) { + pdf.attachments.push({ + filename: attachment[0], + data: attachment[1].content + }); + } + }).catch(error => { + console.log(`Failed to extract attachments from PDF: ${error}`); + }); + const promises = [ doc.getMetadata().then(data => { pdf.meta = {info: data.info, metadata: data.metadata ? data.metadata.getAll() || null : null}; From 46375645bd27e18b512b5b64d8e0faf806f2cee3 Mon Sep 17 00:00:00 2001 From: Daniel Manzke Date: Fri, 14 Apr 2023 10:12:01 +0200 Subject: [PATCH 2/3] added handling if no attachments exist --- lib/index.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index a7752c8..4be4cc4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -78,12 +78,14 @@ class PDFExtract { pdf.pdfInfo = doc.pdfInfo; pdf.attachments = []; - doc.getAttachments().then(attachments => {; - for (const attachment of Object.entries(attachments) ) { - pdf.attachments.push({ - filename: attachment[0], - data: attachment[1].content - }); + doc.getAttachments().then(attachments => { + if (attachments) { + for (const attachment of Object.entries(attachments) ) { + pdf.attachments.push({ + filename: attachment[0], + data: attachment[1].content + }); + } } }).catch(error => { console.log(`Failed to extract attachments from PDF: ${error}`); From 9ed3bbd1f0caa772d617abd386855a8089c97b24 Mon Sep 17 00:00:00 2001 From: Daniel Manzke Date: Fri, 14 Apr 2023 10:15:02 +0200 Subject: [PATCH 3/3] moved get attachment into the promises --- lib/index.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4be4cc4..c967bc1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -78,22 +78,21 @@ class PDFExtract { pdf.pdfInfo = doc.pdfInfo; pdf.attachments = []; - doc.getAttachments().then(attachments => { - if (attachments) { - for (const attachment of Object.entries(attachments) ) { - pdf.attachments.push({ - filename: attachment[0], - data: attachment[1].content - }); - } - } - }).catch(error => { - console.log(`Failed to extract attachments from PDF: ${error}`); - }); - const promises = [ doc.getMetadata().then(data => { pdf.meta = {info: data.info, metadata: data.metadata ? data.metadata.getAll() || null : null}; + }), + doc.getAttachments().then(attachments => { + if (attachments) { + for (const attachment of Object.entries(attachments) ) { + pdf.attachments.push({ + filename: attachment[0], + data: attachment[1].content + }); + } + } + }).catch(error => { + console.log(`Failed to extract attachments from PDF: ${error}`); }) ]; const loadPage = pageNum => doc.getPage(pageNum).then(page => {