n8n/packages/nodes-base/nodes/ReadPdf.node.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import {
BINARY_ENCODING,
2021-02-16 00:51:48 -08:00
IExecuteFunctions,
2019-06-23 03:35:23 -07:00
} from 'n8n-core';
import {
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
const pdf = require('pdf-parse');
export class ReadPdf implements INodeType {
description: INodeTypeDescription = {
displayName: 'Read PDF',
2020-05-12 06:08:19 -07:00
name: 'readPDF',
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,
description: 'Name of the binary property from which to<br />read the PDF file.',
},
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[] = [];
const length = items.length as unknown as number;
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++) {
2021-03-29 02:20:10 -07:00
2021-02-16 00:51:48 -08:00
item = items[itemIndex];
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', itemIndex) as string;
2019-06-23 03:35:23 -07:00
2021-02-16 00:51:48 -08:00
if (item.binary === undefined) {
item.binary = {};
}
2019-06-23 03:35:23 -07:00
2021-02-16 00:51:48 -08:00
const binaryData = Buffer.from(item.binary[binaryPropertyName].data, BINARY_ENCODING);
returnData.push({
binary: item.binary,
json: await pdf(binaryData),
});
2021-03-29 02:20:10 -07:00
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
}
2021-02-16 00:51:48 -08:00
2019-06-23 03:35:23 -07:00
}