2023-07-18 11:07:29 -07:00
|
|
|
import {
|
2024-01-03 03:08:16 -08:00
|
|
|
NodeOperationError,
|
2024-08-29 06:55:53 -07:00
|
|
|
NodeConnectionType,
|
2023-07-18 11:07:29 -07:00
|
|
|
type IExecuteFunctions,
|
|
|
|
type INodeExecutionData,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeDescription,
|
2023-01-27 03:22:44 -08:00
|
|
|
} from 'n8n-workflow';
|
2024-01-03 03:08:16 -08:00
|
|
|
import { extractDataFromPDF } from '@utils/binary';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2022-06-20 07:54:01 -07:00
|
|
|
export class ReadPDF implements INodeType {
|
2019-06-23 03:35:23 -07:00
|
|
|
description: INodeTypeDescription = {
|
2024-01-03 03:08:16 -08:00
|
|
|
hidden: true,
|
2019-06-23 03:35:23 -07:00
|
|
|
displayName: 'Read PDF',
|
2023-01-02 00:22:33 -08:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-name-miscased
|
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',
|
|
|
|
},
|
2024-08-29 06:55:53 -07:00
|
|
|
inputs: [NodeConnectionType.Main],
|
|
|
|
outputs: [NodeConnectionType.Main],
|
2019-06-23 03:35:23 -07:00
|
|
|
properties: [
|
|
|
|
{
|
2024-01-03 03:08:16 -08:00
|
|
|
displayName: 'Input Binary Field',
|
2019-06-23 03:35:23 -07:00
|
|
|
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
|
|
|
},
|
2023-07-18 11:07:29 -07:00
|
|
|
{
|
|
|
|
displayName: 'Encrypted',
|
|
|
|
name: 'encrypted',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Password',
|
|
|
|
name: 'password',
|
|
|
|
type: 'string',
|
|
|
|
typeOptions: { password: true },
|
|
|
|
default: '',
|
|
|
|
description: 'Password to decrypt the PDF file with',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
encrypted: [true],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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;
|
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 {
|
2023-01-06 06:09:32 -08:00
|
|
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', itemIndex);
|
2021-07-19 23:58:54 -07:00
|
|
|
|
2024-01-03 03:08:16 -08:00
|
|
|
let password;
|
2023-07-18 11:07:29 -07:00
|
|
|
if (this.getNodeParameter('encrypted', itemIndex) === true) {
|
2024-01-03 03:08:16 -08:00
|
|
|
password = this.getNodeParameter('password', itemIndex) as string;
|
2023-07-18 11:07:29 -07:00
|
|
|
}
|
2023-01-13 09:11:56 -08:00
|
|
|
|
2024-01-03 03:08:16 -08:00
|
|
|
const json = await extractDataFromPDF.call(
|
|
|
|
this,
|
|
|
|
binaryPropertyName,
|
|
|
|
password,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
itemIndex,
|
|
|
|
);
|
2023-07-18 11:07:29 -07:00
|
|
|
|
|
|
|
returnData.push({
|
|
|
|
binary: items[itemIndex].binary,
|
2024-01-03 03:08:16 -08:00
|
|
|
json,
|
2021-07-19 23:58:54 -07:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2024-06-19 22:45:00 -07:00
|
|
|
if (this.continueOnFail(error)) {
|
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;
|
|
|
|
}
|
2024-01-03 03:08:16 -08:00
|
|
|
throw new NodeOperationError(this.getNode(), error, { itemIndex });
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
}
|