2022-01-03 13:42:42 -08:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
const pdf = require('pdf-parse');
|
|
|
|
|
2022-06-20 07:54:01 -07:00
|
|
|
export class ReadPDF implements INodeType {
|
2019-06-23 03:35:23 -07:00
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Read PDF',
|
2020-05-12 06:08:19 -07:00
|
|
|
name: 'readPDF',
|
2019-07-26 02:27:46 -07:00
|
|
|
icon: 'fa:file-pdf',
|
2019-06-23 03:35:23 -07:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Reads a PDF and extracts its content',
|
|
|
|
defaults: {
|
|
|
|
name: 'Read PDF',
|
|
|
|
color: '#003355',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Binary Property',
|
|
|
|
name: 'binaryPropertyName',
|
|
|
|
type: 'string',
|
|
|
|
default: 'data',
|
|
|
|
required: true,
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Name of the binary property from which to read the PDF file',
|
2019-06-23 03:35:23 -07:00
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
2021-02-16 00:51:48 -08:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-02-16 00:51:48 -08:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-02-16 00:51:48 -08:00
|
|
|
let item: INodeExecutionData;
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2021-02-16 00:51:48 -08:00
|
|
|
for (let itemIndex = 0; itemIndex < length; itemIndex++) {
|
2022-08-17 08:50:24 -07:00
|
|
|
try {
|
2021-07-19 23:58:54 -07:00
|
|
|
item = items[itemIndex];
|
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', itemIndex) as string;
|
|
|
|
|
|
|
|
if (item.binary === undefined) {
|
|
|
|
item.binary = {};
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2022-01-03 13:42:42 -08:00
|
|
|
const binaryData = await this.helpers.getBinaryDataBuffer(itemIndex, binaryPropertyName);
|
2021-07-19 23:58:54 -07:00
|
|
|
returnData.push({
|
|
|
|
binary: item.binary,
|
|
|
|
json: await pdf(binaryData),
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-06-03 08:25:07 -07:00
|
|
|
returnData.push({
|
|
|
|
json: {
|
|
|
|
error: error.message,
|
|
|
|
},
|
|
|
|
pairedItem: {
|
|
|
|
item: itemIndex,
|
|
|
|
},
|
|
|
|
});
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
2021-02-16 00:51:48 -08:00
|
|
|
return this.prepareOutputData(returnData);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|