Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils.converters.PDF2Text (new) convert PDF to text #347

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/appmixer/utils/converters/PDF2Text/PDF2Text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const converters = require('../converters');
const path = require('path');
const { Readable } = require('stream');

module.exports = {

async receive(context) {

const { fileId, outputTextContent } = context.messages.in.content;
const fileInfo = await context.getFileInfo(fileId);
const newFileName = path.parse(fileInfo.filename).name + '.txt';
const textContent = await converters.pdfToText(context, fileId);
const savedFile = await context.saveFileStream(newFileName, Readable.from(textContent));
const out = { ...savedFile };
if (outputTextContent) {
out.textContent = (await context.loadFile(savedFile.fileId)).toString('utf8');
}
return context.sendJson(out, 'out');
}
};
49 changes: 49 additions & 0 deletions src/appmixer/utils/converters/PDF2Text/component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "appmixer.utils.converters.PDF2Text",
"author": "Appmixer <info@appmixer.com>",
"description": "Extract text from a PDF file.",
"private": false,
"inPorts": [
{
"name": "in",
"schema": {
"properties": {
"fileId": { "type": "string" },
"outputTextContent": { "type": "boolean" }
},
"required": [ "fileId" ]
},
"inspector": {
"inputs": {
"fileId": {
"type": "filepicker",
"label": "File ID",
"index": 1,
"tooltip": "The ID of the PDF file to convert."
},
"outputTextContent": {
"type": "toggle",
"defaultValue": false,
"label": "Output Text Content",
"index": 2,
"tooltip": "By default, the actual PDF text content is not returned, allowing very large files to be processed without working with the entire file data directly. For convenience, if your PDF files are smaller than 10 MB and you want to work with the pdf text directly, you can set this option. (Note that you can use the 'Files.Load File' utility to read any file data when needed)."
}
}
}
}
],
"outPorts": [
{
"name": "out",
"options": [
{ "label": "File ID", "value": "fileId" },
{ "label": "File Name", "value": "fileName" },
{ "label": "File Size (Bytes)", "value": "length" },
{ "label": "File Upload Date", "value": "uploadDate" },
{ "label": "File MD5", "value": "md5" },
{ "label": "Text Content", "value": "textContent" }
]
}
],
"icon": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAQAAAD2e2DtAAAHyklEQVR42u3dX2hWdRzH8c+GCDq1YVPbRRFqcyiFlnqVbDlZkloMmrWyqGtJGDHyQhdRwZZU5DRcdZHKpJQCbyzTsAJJU0fqLsz/0Y1TIZ2VTN0Ww4yp27Pz73me8/v+3p/vnTd7zvm+POc83/M7z5EIIYQQQgghhBBCCCEkmYxRuSpUa6yWaaXWaI1Watl//1KpUpo9MA9ouXbpivo8qg41qJjWF+oVHfKq8QOrU3V+t/8pHfG2+beq2dfml2in982/We/62P6HdZrW/1/1vrX/Gc8u+IarHj3nU/vnqZum31HXVO1L+x/UeRo+SHXpUR/aP1ZHafYQdV4P2QewgUZnqOOaYLv903SdNmesX1RkGcDXtHjY2q2RVts/V700OEBtVIFNAC00N2A12QRwhtb6PBucRVtDVK9esgZgNW0NVd2abwtAG00NWZc10xKAH2ip37PB32hohDqpSVYAdNHOSLXfymww+CYvdXxLtzIbBECy1WZhNggAz2eDAPB8NggAz2eDAPB83SAAPF83CADPZ4MA8Hw2CICk6oDGAMAGgCs+zQYBcHft0Dl/ZoMAuLu2ak7ko0ATACwAkKoiPylZDwALAKQXIy6Xd2w2CIChAEgrfZgNAmBoANKH9meDAMgEoECbrM8GAZAJgDRS39meDQIgMwBpnNotzwYBMBwAaaKO250NAmB4ANIUu7NBAAQBILuzQQAEA2B2NgiAoACMzgYBEByAydkgAMIAiDMbnAUACwDMzQYBEA6AudkgAMICMDYbBEB4AKZmgwCIAsDQbBAAnv/eIADyUY0A8BtAn94BgN8A+rQCAH4D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E4DKNM67dNZ/a5j2qftKRsJf65Vmq+RAMhOqnTGiTuB57RaowGQdL5yai3AaT0GgCRz0LnlIP/oCQAklW1OLgnrUhkAkki1s2uCf87fy+ctATjr8LLwGgDEzRSnnwv6BgBx0+I0gGsaBYB42e/4s4FzARAvpxwHsAQA8XLScQCLAcApAABcBAKAr4EAYBAEAEbBAOBmEADC5xC3g/0GUKAfWRDiM4B+Aq78ZCRLwrJGYG2MtpxiUaj7q4LjEfgkf9fkAIAAACAAAAgAAAK5SZdD35k79ZMaNRkCSeaYc5OzHm3WfRBIKnucnJ53al4oAutj/K11tgm0OXr/7G89njMCTZYBrHb2FmqnJuXsRPC8XQAzHV5EsTnk5WD0o8AFjbNL4IyzAHo0NWcE3rAL4COHjwFvhp4LRCXQYRfAHPU6C2BPDkdDk+wS2ObwhWCU6WC0o0ClXQBlTrxeYfDV9MoZgRoZzseOAriUw3sE1ZYBjNVRJwEcjnGbKOxRYJrtkfBkXXAQwIZYdwrDELiYvzf75SqVDl4JLIh5szg4gVZ5kMVO3RzuU3vs2zRBrwVuaLq8yAyHnrLvUUUCWxzsKLBW3uRe7XAEQGNCWzw8gQP5enQ7X1moX1Pf/uYEt7dAazK2v0TepVAv62BqR8Sdqkt8i+vUOei5v8W3//0Dc7+Wa5cup6r5HWpQcVa2tlgN6hjwl86rVTNEJI3WNFWoNu9VqdKsb2upKlWrRSq3/72fEEIIIYQQQgghhBDiSv4FeV1otCGtp+0AAAAASUVORK5CYII="
}
5 changes: 4 additions & 1 deletion src/appmixer/utils/converters/bundle.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "appmixer.utils.converters",
"version": "1.3.2",
"version": "1.4.0",
"changelog": {
"1.1.0": [
"FileId inputs changed from 'text' to 'filepicker'. Requires Appmixer 4.4.4 or higher."
Expand All @@ -17,6 +17,9 @@
"1.3.2": [
"improve CSV2HTML, CSV2JSON and JSON2CSV, JSON2HTML converters to use streams.",
"JSON2HTML, CSV2HTML: added support for custom HTML renderers."
],
"1.4.0": [
"PDF2Text: new component to extract text from a PDF file."
]
}
}
23 changes: 23 additions & 0 deletions src/appmixer/utils/converters/converters.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { pipeline } = require('stream');
const customHtmlTransforms = require('./htmlTransforms');
const JSONStream = require('JSONStream');
const { arrayBuffer } = require('node:stream/consumers');
let pdfjslib;
const {
getCSVReadStream,
csvToJsonTransform,
Expand Down Expand Up @@ -95,5 +97,26 @@ module.exports = {
.then(resolve)
.catch(reject);
});
},

pdfToText: async function(context, sourceFileId) {

if (!pdfjslib) {
pdfjslib = await import('pdfjs-dist/legacy/build/pdf.mjs');
}
const readStream = await context.getFileReadStream(sourceFileId);
let text = '';
// Unfortunately, `data` can only be a string, buffer or TypedArray.
// getDocument() does not work over streams.
const loadingTask = pdfjslib.getDocument({ data: await arrayBuffer(readStream) });
const pdfDoc = await loadingTask.promise;
for (let i = 0; i < pdfDoc.numPages; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on this code, it seems that the entire PDF file is not loaded into memory all at once; instead, it is processed page by page. Is that correct?

However, the entire output text is stored in memory. I believe we should consider sending the output text chunks into a stream, or we should introduce a strict limit on the input size of the PDF file. With larger PDF files containing a lot of text, there's a possibility of reaching the memory limit on the instance.

const page = await pdfDoc.getPage(i + 1);
const textContent = await page.getTextContent();
if (textContent.items.length) {
text += textContent.items.map(item => item.str || '').join(' ');
}
}
return text;
}
};
218 changes: 218 additions & 0 deletions src/appmixer/utils/converters/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/appmixer/utils/converters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"bluebird": "3.7.2",
"csv-parse": "5.5.6",
"JSONStream": "1.3.5",
"pdfjs-dist": "4.10.38",
"tar": "6.1.12",
"unzipper": "0.10.11",
"xlsx": "0.18.5"
Expand Down
Loading