Cómo analizar archivos PDF a escala en NodeJS: qué hacer y qué no hacer

Dé un paso en la arquitectura del programa y aprenda a crear una solución práctica para un problema empresarial real con NodeJS Streams con este artículo.

Un desvío: mecánica de fluidos

Una de las mayores fortalezas del software es que podemos desarrollar abstracciones que nos permiten razonar sobre el código y manipular datos de formas que podamos entender. Los arroyos son una de esas clases de abstracción.

En mecánica de fluidos simple, el concepto de línea de corriente es útil para razonar sobre la forma en que se moverán las partículas de fluido y las restricciones que se les aplican en varios puntos de un sistema.

Por ejemplo, digamos que tiene un poco de agua fluyendo a través de una tubería de manera uniforme. A mitad de la tubería, se ramifica. Generalmente, el flujo de agua se dividirá uniformemente en cada rama. Los ingenieros utilizan el concepto abstracto de una línea de corriente para razonar sobre las propiedades del agua, como su velocidad de flujo, para cualquier número de ramificaciones o configuraciones complejas de tuberías. Si le pregunta a un ingeniero cuál asumió que sería el caudal a través de cada rama, respondería acertadamente con "la mitad", intuitivamente. Esto se expande matemáticamente a un número arbitrario de líneas de flujo.

Las corrientes, conceptualmente, son para codificar las líneas de corriente que son mecánicas demasiado fluidas. Podemos razonar sobre los datos en cualquier punto dado considerándolos como parte de un flujo. En lugar de preocuparse por los detalles de implementación entre cómo se almacena. Podría decirse que podría generalizar esto a algún concepto universal de canalización que podamos utilizar entre disciplinas. Me viene a la mente un embudo de ventas, pero eso es tangencial y lo cubriremos más adelante. El mejor ejemplo de flujos, y uno con el que debe familiarizarse absolutamente si aún no lo ha hecho, son las tuberías UNIX:

cat server.log | grep 400 | less

Cariñosamente llamamos |pipa al personaje. Según su función, estamos canalizando la salida de un programa como la entrada de otro programa. Establecimiento efectivo de una tubería.

(Además, parece una pipa).

Si eres como yo y te preguntas en este momento por qué es necesario, pregúntate por qué usamos pipelines en la vida real. Básicamente, es una estructura que elimina el almacenamiento entre puntos de procesamiento. No tenemos que preocuparnos por almacenar barriles de petróleo si se bombea.

Piense en eso en el software. Los desarrolladores e ingenieros inteligentes que escribieron el código para la canalización de datos lo configuraron de manera que nunca ocupara demasiada memoria en una máquina. No importa qué tan grande sea el archivo de registro anterior, no colgará la terminal. El programa completo es un proceso que maneja puntos de datos infinitesimales en una secuencia, en lugar de contenedores de esos puntos. El archivo de registro nunca se carga en la memoria de una vez, sino en partes manejables.

No quiero reinventar la rueda aquí. Entonces, ahora que he cubierto una metáfora de las transmisiones y la justificación para usarlas, Flavio Copes tiene una excelente publicación de blog que cubre cómo se implementan en Node. Tómese el tiempo que necesite para cubrir los aspectos básicos allí, y cuando esté listo, regrese y repasaremos un caso de uso.

La situación

Entonces, ahora que tiene esta herramienta en su cinturón de herramientas, imagine esto:

Está en el trabajo y su gerente / legal / RR.HH. / su cliente / (inserte la parte interesada aquí) se ha acercado a usted con un problema. Pasan demasiado tiempo estudiando archivos PDF estructurados. Por supuesto, normalmente la gente no te dirá tal cosa. Oirá: "Paso 4 horas ingresando datos". O "miro las tablas de precios". O, "completo los formularios correctos para que obtengamos lápices con la marca de nuestra empresa cada trimestre".

Sea lo que sea, si su trabajo implica tanto (a) la lectura de documentos PDF estructurados como (b) el uso masivo de esa información estructurada. Luego, puede intervenir y decir: "Oye, es posible que podamos automatizar eso y liberar tu tiempo para trabajar en otras cosas".

Así que por el bien de este artículo, creemos una empresa ficticia. De donde yo vengo, el término "chupete" se refiere a un idiota o al chupete de un bebé. Así que imaginemos esta empresa falsa que fabrica chupetes. Ya que estamos en eso, saltemos el tiburón y digamos que están impresos en 3D. La empresa opera como un proveedor ético de chupetes para los necesitados que no pueden pagar los productos premium por sí mismos.

(Sé lo tonto que suena, suspenda su incredulidad por favor).

Todd obtiene los materiales de impresión que se incluyen en los productos de DummEth y debe asegurarse de que cumplan con tres criterios clave:

  • son de plástico de calidad alimentaria, para preservar la salud de los bebés,
  • son baratos, para una producción económica, y
  • se obtienen lo más cerca posible, para respaldar la copia de marketing de la empresa que indica que su cadena de suministro también es ética y contamina lo menos posible.

El proyecto

Para que sea más fácil de seguir, configuré un repositorio de GitLab que puede clonar y usar. Asegúrese de que sus instalaciones de Node y NPM también estén actualizadas.

Arquitectura básica: restricciones

Ahora, ¿qué estamos tratando de hacer? Supongamos que Todd trabaja bien en hojas de cálculo, como muchos trabajadores de oficina. Para que Todd separe de la paja el proverbial trigo de impresión 3D, es más fácil para él medir los materiales por grado alimenticio, precio por kilogramo y ubicación. Es hora de establecer algunas limitaciones al proyecto.

Supongamos que la calidad alimentaria de un material se califica en una escala de cero a tres. Con cero significado, plásticos ricos en BPA prohibidos en California. Tres significan materiales no contaminantes de uso común, como polietileno de baja densidad. Esto es simplemente para simplificar nuestro código. En realidad, tendríamos que asignar de alguna manera las descripciones textuales de estos materiales (por ejemplo: "LDPE") a un grado alimenticio.

El precio por kilogramo podemos suponer que es una propiedad del material dada por su fabricante.

Ubicación, vamos a simplificar y asumir que es una distancia relativa simple, en línea recta. En el extremo opuesto del espectro está la solución de ingeniería excesiva: usar alguna API (por ejemplo: Google Maps) para discernir la distancia aproximada de viaje que viajaría un material dado para llegar a los centros de distribución de Todd. De cualquier manera, digamos que nos lo dan como un valor (kilómetros a Todd) en los PDF de Todd.

Además, consideremos el contexto en el que estamos trabajando. Todd opera efectivamente como recolector de información en un mercado dinámico. Los productos entran y salen, y sus detalles pueden cambiar. Esto significa que tenemos una cantidad arbitraria de archivos PDF que pueden cambiar, o mejor dicho, actualizarse en cualquier momento.

Entonces, en base a estas restricciones, finalmente podemos averiguar qué queremos que logre nuestro código. Si desea probar su capacidad de diseño, haga una pausa aquí y considere cómo estructuraría su solución. Puede que no tenga el mismo aspecto que lo que voy a describir. Eso está bien, siempre y cuando le esté proporcionando una solución sana y viable a Todd, y algo que no se rasgaría el cabello más tarde tratando de mantener.

Arquitectura básica: Soluciones

Así que tenemos una cantidad arbitraria de archivos PDF y algunas reglas sobre cómo analizarlos. Así es como podemos hacerlo:

  1. Configure un objeto Stream que pueda leer desde alguna entrada. Como un cliente HTTP que solicita descargas de PDF. O un módulo que hemos escrito que lee archivos PDF de un directorio en el sistema de archivos.
  2. Configure un búfer intermediario. Esto es como el mesero en un restaurante que entrega un plato terminado a su cliente previsto. Cada vez que se pasa un PDF completo al flujo, descargamos esos fragmentos en el búfer para que se pueda transportar.
  3. El camarero (Buffer) entrega la comida (datos PDF) al cliente (nuestra función de análisis). El cliente hace lo que quiere (convertir a algún formato de hoja de cálculo) con él.
  4. Cuando el cliente (analizador) haya terminado, avísele al camarero (búfer) que están libres y que pueden trabajar en nuevos pedidos (PDF).

Notarás que este proceso no tiene un final claro. Como restaurante, nuestro combo Stream-Buffer-Parser nunca termina, hasta que, por supuesto, no hay más datos, no más pedidos, entrando.

Ahora sé que todavía no hay ni una pizca de código. Esto es crucial. Es importante poder razonar sobre nuestros sistemas antes de escribirlos. Ahora bien, no haremos todo bien la primera vez, incluso con un razonamiento a priori. Las cosas siempre se rompen en la naturaleza. Los errores deben corregirse.

Dicho esto, es un poderoso ejercicio de moderación y previsión planificar su código antes de escribirlo. Si puede simplificar sistemas de complejidad creciente en partes y analogías manejables, podrá aumentar su productividad de manera exponencial, ya que el estrés cognitivo de esas complejidades se desvanece en abstracciones bien diseñadas.

Entonces, en el gran esquema de las cosas, se ve así:

Introducción de dependencias

Now as a disclaimer, I should add that there is a whole world of thought around introducing dependencies into your code. I’d love to cover this concept in another post. In the meantime let me just say that one of the fundamental conflicts at play is the one between our desire to get our work done quickly (i.e.: to avoid NIH syndrome), and our desire to avoid third-party risk.

Applying this to our project, I opted to offload the bulk of our PDF processing to the pdfreader module. Here are a few reasons why:

  • It was published recently, which is a good sign that the repo is up-to-date.
  • It has one dependency — that is, it’s just an abstraction over another module — which is regularly maintained on GitHub. This alone is a great sign. Moreover, the dependency, a module called pdf2json, has hundreds of stars, 22 contributors, and plenty of eyeballs keeping a close eye on it.
  • The maintainer, Adrian Joly, does good bookkeeping in GitHub’s issue tracker and actively tends to users and developers’ questions.
  • When auditing via NPM (6.4.1), no vulnerabilities are found.

So all in all, it seems like a safe dependency to include.

Now, the module works in a fairly straightforward way, although its README doesn’t explicitly describe the structure of its output. The cliff notes:

  1. It exposes the PdfReader class to be instantiated
  2. This instance has two methods for parsing a PDF. They return the same output and only differ in the input: PdfReader.parseFileItems for a filename, and PdfReader.parseBuffer from data that we don’t want to reference from the filesystem.
  3. The methods ask for a callback, which gets called each time the PdfReader finds what it denotes as a PDF item. There are three kinds. First, the file metadata, which is always the first item. Second is page metadata. It acts as a carriage return for the coordinates of text items to be processed. Last is text items which we can think of as simple objects / structs with a text property, and floating-point 2D AABB coordinates on the page.
  4. It’s up to our callback to process these items into a data structure of our choice and also to handle any errors thrown to it.

Here’s a code snippet as an example:

const { PdfReader } = require('pdfreader');
// Initialise the readerconst reader = new PdfReader();
// Read some arbitrarily defined bufferreader.parseBuffer(buffer, (err, item) =>; {
 if (err) console.error(err);
 else if (!item) /* pdfreader queues up the items in the PDF and passes them to * the callback. When no item is passed, it's indicating that * we're done reading the PDF. */ console.log('Done.');
 else if (item.file) // File items only reference the PDF's file path. console.log(`Parsing $ 'a buffer'`)
 else if (item.page) // Page items simply contain their page number. console.log(`Reached page ${item.page}`);
 else if (item.text) {
 // Text items have a few more properties: const itemAsString = [ item.text, 'x: ' + item.x, 'y: ' + item.y, 'w: ' + item.width, 'h: ' + item.height, ].join('\n\t');
 console.log('Text Item: ', itemAsString);
 }
});

Todd’s PDFs

Let’s return to the Todd situation, just to provide some context. We want to store the data pacifiers based on three key criteria:

  • their food-grade, to preserve babies’ health,
  • their cost, for economical production, and
  • their distance to Todd, to support the company’s marketing copy stating that their supply chain is also ethical and pollutes as little as possible.

I’ve hardcoded a simple script that randomizes some dummy products, and you can find it in the /data directory of the companion repo for this project. That script writes that randomized data to JSON files.

There’s also a template document in there. If you’re familiar with templating engines like Handlebars, then you’ll understand this. There are online services — or if you’re feeling adventurous, you can roll your own — that take JSON data and fill in the template, and give it back to you as a PDF. Maybe for completeness’ sake, we can try that out in another project. Anyway: I’ve used such a service to generate the dummy PDFs we’ll be parsing.

Here’s what one looks like (extra whitespace has been cropped out):

We’d like to yield from this PDF some JSON that gives us:

  • the requisition ID and date, for bookkeeping purposes,
  • the SKU of the pacifier, for unique identification, and
  • the pacifier’s properties (name, food grade, unit price, and distance), so Todd can actually use them in his work.

How do we do this?

Reading the Data

First let’s set up the function for reading data out of one of these PDFs, and extracting pdfreader’s PDF items into a usable data structure. For now, let’s have an array representing the document. Each item in the array is an object representing a collection of all text elements on the page at that object’s index. Each property in the page object has a y-value for its key, and an array of the text items found at that y-value for its value. Here’s the diagram, so it’s simpler to understand:

The readPDFPages function in /parser/index.js handles this, similarly to the example code written above:

/* Accepts a buffer (e.g.: from fs.readFile), and parses * it as a PDF, giving back a usable data structure for * application-specific, second-level parsing. */function readPDFPages (buffer) { const reader = new PdfReader();
 // We're returning a Promise here, as the PDF reading // operation is asynchronous. return new Promise((resolve, reject) =>; {
 // Each item in this array represents a page in the PDF let pages = [];
 reader.parseBuffer(buffer, (err, item) =>; {
 if (err) // If we've got a problem, eject! reject(err)
 else if (!item) // If we're out of items, resolve with the data structure resolve(pages);
 else if (item.page) // If the parser's reached a new page, it's time to // work on the next page object in our pages array. pages.push({});
 else if (item.text) 
 }); });
}

So now passing a PDF buffer into that function, we’ll get some organized data. Here’s what I got from a test run, and printing it to JSON:

[ { '3.473': [ 'PRODUCT DETAILS REQUISITION' ], '4.329': [ 'Date: 23/05/2019' ], '5.185': [ 'Requsition ID: 298831' ], '6.898': [ 'Pacifier Tech', 'Todd Lerr' ], '7.754': [ '123 Example Blvd', 'DummEth Pty. Ltd.' ], '8.61': [ 'Timbuktu', '1337 Leet St' ], '12.235': [ 'SKU', '6308005' ], '13.466': [ 'Product Name', 'Square Lemon Qartz Pacifier' ], '14.698': [ 'Food Grade', '3' ], '15.928999999999998': [ '$ / kg', '1.29' ], '17.16': [ 'Location', '55' ] } ]

If you look carefully you’ll notice that there’s a spelling error in the original PDF. “Requisition” is misspelled as “Requsition”. The beauty of our parser is that we don’t particularly care for errors like these in our input documents. As long as they’re structured correctly, we can extract data from them accurately.

Now we just need to organize this into something a bit more usable (as if we’d expose it via API). The structure we’re looking for is something along the lines of this:

{ reqID: '000000', date: 'DD/MM/YYYY', // Or something else based on geography sku: '000000', name: 'Some String We Have Trimmed', foodGrade: 'X', unitPrice: 'D.CC', // D for Dollars, C for Cents location: 'XX',}

An Aside: Data Integrity

Why are we including the numbers as strings? It’s based on the risk of parsing. Let’s just say that we coerced all of our numbers to strings:

The unit price and location would be fine — they are supposed to be countable numbers after all.

The food grade, for this very limited project, technically is safe. No data gets lost when we coerce it — but if it’s effectively a classifier, like an Enum, so it’s better off kept as a string.

The requisition ID and SKU however, if coerced to strings, could lose important data. If the ID for a given requisition starts with three zeros and we coerce that to a number, well, we’ve just lost those zeros and we’ve garbled the data.

So because we want data integrity when reading the PDFs, we just leave everything as a String. If the application code wants to convert some fields to numbers to make them usable for arithmetic or statistical operations, then we’ll let the coercion occur at that layer. Here we just want something that parses PDFs consistently and accurately.

Restructuring the Data

So now we’ve got Todd’s information, we just need to organize it in a usable way. We can use a variety of array and object manipulation functions, and here MDN is your friend.

This is the step where everyone has their own preferences. Some prefer the method that just gets the job done and minimizes dev time. Others prefer to scout for the best algorithm for the job (e.g.: cutting down iteration time). It’s a good exercise to see if you can come up with a way to do this and compare it to what I got. I’d love to see better, simpler, faster, or even just different ways to accomplish the same goal.

Anyway, here’s how I did it: the parseToddPDF function in /parser/index.js.

function parseToddPDF (pages) {
 const page = pages[0]; // We know there's only going to be one page
 // Declarative map of PDF data that we expect, based on Todd's structure const fields = { // "We expect the reqID field to be on the row at 5.185, and the // first item in that array" reqID: { row: '5.185', index: 0 }, date: { row: '4.329', index: 0 }, sku: { row: '12.235', index: 1 }, name: { row: '13.466', index: 1 }, foodGrade: { row: '14.698', index: 1 }, unitPrice: { row: '15.928999999999998', index: 1 }, location: { row: '17.16', index: 1 }, };
 const data = {};
 // Assign the page data to an object we can return, as per // our fields specification Object.keys(fields) .forEach((key) =>; {
 const field = fields[key]; const val = page[field.row][field.index];
 // We don't want to lose leading zeros here, and can trust // any application / data handling to worry about that. This is // why we don't coerce to Number. data[key] = val;
 });
 // Manually fixing up some text fields so they're usable data.reqID = data.reqID.slice('Requsition ID: '.length); data.date = data.date.slice('Date: '.length);
 return data;
}

The meat and potatoes here is in the forEach loop, and how we’re using it. After retrieving the Y positions of each text item previously, it’s simple to specify each field we want as a position in our pages object. Effectively providing a map to follow.

All we have to do then is declare a data object to output, iterate over each field we specified, follow the route as per our spec, and assign the value we find at the end to our data object.

After a few one-liners to tidy up some string fields, we can return the data object and we’re off to the races. Here’s what it looks like:

{ reqID: '298831', date: '23/05/2019', sku: '6308005', name: 'Square Lemon Qartz Pacifier', foodGrade: '3', unitPrice: '1.29', location: '55' }

Putting it all together

Now we’ll move on to building out some concurrency for this parsing module so we can operate at scale, and recognize some important barriers to doing so. The diagram above is great for understanding the context of the parsing logic. It doesn’t do much for understanding how we’re going to parallelize it. We can do better:

Trivial, I know, and arguably way too textbook-y generalized for us to practically use, but hey, it’s a fundamental concept to formalize.

Now first and foremost we need to think about how we’re going to handle the input and output of our program, which will essentially be wrapping the parsing logic and then distributing it amongst parser worker processes. There are many questions we can ask here and many solutions:

  • is it going to be a command line application?
  • Is it going to be a consistent server, with a set of API endpoints? This has its own host of questions — REST or GraphQL, for example?
  • Maybe it’s just a skeleton module in a broader codebase — for example, what if we generalized our parsing across a suite of binary documents and wanted to separate the concurrency model from the particular source file type and parsing implementation?

For simplicity’s sake, I’m going to wrap the parsing logic in a command-line utility. This means it’s time to make a bunch of assumptions:

  • does it expect file paths as input, and are they relative or absolute?
  • Or instead, does it expect concatenated PDF data, to be piped in?
  • Is it going to output data to a file? Because if it is, then we’re going to have to provide that option as an argument for the user to specify…

Handling Command Line Input

Again, keeping things as simple as possible: I’ve opted for the program to expect a list of file paths, either as individual command line arguments:

node index file-1.pdf file-2.pdf … file-n.pdf

Or piped to standard input as a newline-separated list of file paths:

# read lines from a text file with all our pathscat files-to-parse.txt | node index# or perhaps just list them from a directoryfind ./data -name “*.pdf” | node index

This allows the Node process to manipulate the order of those paths in any way it sees fit, which allows us to scale the processing code later. To do this, we’re going to read the list of file paths, whichever way they were provided, and divvy them up by some arbitrary number into sub-lists. Here’s the code, the getTerminalInput method in ./input/index.js:

function getTerminalInput (subArrays) {
 return new Promise((resolve, reject) =>; {
 const output = []; if (process.stdin.isTTY) {
 const input = process.argv.slice(2);
 const len = Math.min(subArrays, Math.ceil(input.length / subArrays));
 while (input.length) { output.push(input.splice(0, len)); }
 resolve(output);
 } else { let input = ''; process.stdin.setEncoding('utf-8');
 process.stdin.on('readable', () => { let chunk; while (chunk = process.stdin.read()) input += chunk; });
 process.stdin.on('end', () => { input = input.trim().split('\n');
 const len = Math.min(input.length, Math.ceil(input.length / subArrays));
 while (input.length) { output.push(input.splice(0, len)); }
 resolve(output); }) } });
}

Why divvy up the list? Let’s say that you have an 8-core CPU on consumer-grade hardware, and 500 PDFs to parse.

Unfortunately for Node, even though it handles asynchronous code fantastically thanks to its event loop, it only runs on one thread. To process those 500 PDFs, if you’re not running multithreaded (i.e.: multiple process) code, you’re only using an eighth of your processing capacity. Assuming that memory efficiency isn’t a problem, you could process the data up to eight times faster by taking advantage of Node’s built-in parallelism modules.

Splitting up our input into chunks allows us to do that.

As an aside, this is essentially a primitive load balancer and clearly assumes that the workloads presented by parsing each PDF are interchangeable. That is, that the PDFs are the same size and hold the same structure.

This is obviously a trivial case, especially since we’re not taking into account error handling in worker processes and which worker is currently available to handle new loads. In the case where we would have set up an API server to handle incoming parsing requests, we would have to consider these extra needs.

Clustering our code

Now that we have our input split into manageable workloads, admittedly in a contrived way — I’d love to refactor this later — let’s go over how we can cluster it. So it turns out Node has two separate modules for setting up parallel code.

The one we’re going to use, the cluster module, basically allows a Node process to spawn copies of itself and balance processing between them as it sees fit.

This is built on top of the child_process module, which is less tightly coupled with parallelizing Node programs themselves and allows you to spawn other processes, like shell programs or another executable binary, and interface with them using standard input, output, et cetera.

I highly recommend reading through the API docs for each module, since they’re fantastically written, and even if you’re like me and find purposeless manual reading boring and total busy-work, at least familiarise yourself with the introductions to each module will help you ground yourself in the topic and expand your knowledge of the Node ecosystem.

So let’s walk through the code. Here it is in bulk:

const cluster = require('cluster');const numCPUs = require('os').cpus().length;
const { getTerminalInput } = require('./input');
(async function main () {
 if (cluster.isMaster) {
 const workerData = await getTerminalInput(numCPUs);
 for (let i = 0; i < workerData.length; i++) {
 const worker = cluster.fork(); const params = { filenames: workerData[i] };
 worker.send(params);
 }
 } else {
 require('./worker');
 }
})();

So our dependencies are pretty simple. First, there’s the cluster module as described above. Second, we’re requiring the os module for the express purpose of figuring out how many CPU cores there are on our machine — which is a fundamental parameter of splitting up our workload. Finally, there’s our input handling function which I’ve outsourced to another file for completeness’ sake.

Now the main method is actually rather simple. In fact, we could break it down into steps:

  1. If we’re the main process, split up the input sent to us evenly per the number of CPU cores for this machine
  2. For each worker-to-be’s load, spawn a worker by cluster.fork and set up an object which we can send to it by the [cluster] module’s inter-process RPC message channel, and send the damn thing to it.
  3. If we’re not in fact the main module, then we must be a worker — just run the code in our worker file and call it a day.

Nothing crazy is going on here, and it allows us to focus on the real lifting, which is figuring out how the worker is going to use the list of filenames we give to it.

Messaging, Async, and Streams, all the elements of a nutritious diet

First, as above let me dump the code for you to refer to. Trust me, looking through it first will let you skip through any explanation you’d consider trivial.

const Bufferer = require('../bufferer');const Parser = require('../parser');const { createReadStream } = require('fs');
process.on('message', async (options) =>; {
 const { filenames } = options; const parser = new Parser();
 const parseAndLog = async (buf) => console.log(await parser.parse(buf) + ',');
 const parsingQueue = filenames.reduce(async (result, filename) =>; {
 await result;
 return new Promise((resolve, reject) =>; {
 const reader = createReadStream(filename); const bufferer = new Bufferer({ onEnd: parseAndLog });
 reader .pipe(bufferer) .once('finish', resolve) .once('error', reject) }); }, true);
 try { await parsingQueue; process.exit(0); } catch (err) { console.error(err); process.exit(1); }
});

Now there are some dirty hacks in here so be careful if you’re one of the uninitiated (only joking). Let’s look at what happens first:

Step one is to require all the necessary ingredients. Mind you, this is based on what the code itself does. So let me just say we’re going to use a custom-rolled Writable stream I’ve endearingly termed Bufferer, a wrapper for our parsing logic from last time, also intricately named, Parser, and good old reliable createReadStream from the fs module.

Now here’s where the magic happens. You’ll notice that nothing’s actually wrapped in a function. The entire worker code is just waiting for a message to come to the process — the message from its master with the work it has to do for the day. Excuse the medieval language.

So we can see first of all that it’s asynchronous. First, we extract the filenames from the message itself — if this were production code I’d be validating them here. Actually, hell, I’d be validating them in our input processing code earlier. Then we instantiate our parsing object — only one for the whole process — this is so we can parse multiple buffers with one set of methods. A concern of mine is that it’s managing memory internally, and on reflection, this is a good thing to review later.

Then there’s a simple wrapper, parseAndLog around parsing that logs the JSON-ified PDF buffer with a comma appended to it, just to make life easier for concatenating the results of parsing multiple PDFs.

Finally the meat of the matter, the asynchronous queue. Let me explain:

This worker’s received its list of filenames. For each filename (or path, really), we need to open a readable stream through the filesystem so we can get the PDF data. Then, we need to spawn our Bufferer, (our waiter, following along from the restaurant analogy earlier), so we can transport the data to our Parser.

The Bufferer is custom-rolled. All it really does is accept a function to call when it’s received all the data it needs — here we’re just asking it to parse and log that data.

So, now we have all the pieces, we just pipe them together:

  1. The readable stream — the PDF file, pipes to the Bufferer
  2. The Bufferer finishes and calls our worker-wide parseAndLog method

This entire process is wrapped in a Promise, which itself is returned to the reduce function it sits inside. When it resolves, the reduce operation continues.

This asynchronous queue is actually a really useful pattern, so I’ll cover it in more detail in my next post, which will probably be more bite-sized than the last few.

Anyway, the rest of the code just ends the process based on error-handling. Again, if this were production code, you can bet there’d be more robust logging and error handling here, but as a proof of concept, this seems alright.

So it works, but is it useful?

So there you have it. It was a bit of a journey, and it certainly works, but like any code, it’s important to review what its strengths and weaknesses are. Off the top of my head:

  • Streams have to be piled up in buffers. This, unfortunately, defeats the purpose of using streams, and memory efficiency suffer accordingly. This is a necessary duct-tape-fit to work with the pdfreader module. I’d love to see if there’s a way to stream PDF data and parse it on a finer-grained level. Especially if modular, functional parsing logic can still be applied to it.
  • In this baby stage, the parsing logic is also annoyingly brittle. Just think, what if I have a document that’s longer than a page? A bunch of assumptions fly out the window and make the need for streaming PDF data even stronger.
  • Finally, it would be great to see how we could build out this functionality with logging and API endpoints to provide to the public — for a price, or pro bono, depending on the contexts in which it’s used.

If you’ve got any specific criticisms or concerns I’d love to hear them too, since spotting weaknesses in the code are the first step to fixing them. And, if you’re aware of any better method to streaming and parsing PDFs concurrently, let me know so I can leave it here for anyone reading through this post for an answer. Either way — or for any other purpose — send me an email or get in touch on Reddit.