n8n-nodes-doc-fill-handwritten-finalyy
Version:
Custom n8n node to fill PDF forms with handwritten.ttf font
99 lines • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocCreateField = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const DocCreateFieldUtils_1 = require("./DocCreateFieldUtils");
const pdf_lib_1 = require("pdf-lib");
const nodeOperationOptions = [
{
displayName: 'Property Name',
name: 'dataPropertyName',
type: 'string',
default: 'data',
description: 'Name of the binary property which holds the document to be used',
},
{
displayName: 'Property Name Out',
name: 'dataPropertyNameOut',
type: 'string',
default: 'data',
description: 'Name of the binary property for the output',
},
{
displayName: 'Configuration JSON',
name: 'configurationJson',
type: 'json',
default: '',
description: 'JSON used to map the keys in the PDF form to the corresponding values',
},
];
class DocCreateField {
constructor() {
this.description = {
displayName: 'Doc Create Field',
name: 'docCreateField',
group: ['transform'],
version: 1,
description: 'Node made for creating fields in a pdf form.',
defaults: {
name: 'Doc Create Field',
},
inputs: ['main'],
inputNames: ['Document'],
outputs: ['main'],
properties: [...nodeOperationOptions],
};
}
async execute() {
const items = this.getInputData();
let itemBinaryData;
let dataPropertyName;
let dataPropertyNameOut;
let docBinaryData;
let docBuffer;
let pdfDoc;
const returnData = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
dataPropertyName = this.getNodeParameter('dataPropertyName', itemIndex, '');
dataPropertyNameOut = this.getNodeParameter('dataPropertyNameOut', itemIndex, '');
try {
itemBinaryData = items[itemIndex].binary;
docBinaryData = itemBinaryData[dataPropertyName];
if (!(0, DocCreateFieldUtils_1.isPDFDocument)(docBinaryData)) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Input (on binary property "${dataPropertyName}") should be a PDF file, was ${docBinaryData.mimeType} instead`, { itemIndex });
}
docBuffer = await this.helpers.getBinaryDataBuffer(itemIndex, dataPropertyName);
pdfDoc = await pdf_lib_1.PDFDocument.load(docBuffer);
let savedDoc = await pdfDoc.save();
const result = {
json: {
...items[itemIndex].json,
},
binary: {
...items[itemIndex].binary,
[dataPropertyNameOut]: await this.helpers.prepareBinaryData(Buffer.from(savedDoc), docBinaryData.fileName ? docBinaryData.fileName : dataPropertyNameOut + '.pdf', 'application/pdf'),
},
pairedItem: [items[itemIndex].pairedItem],
};
returnData.push(result);
}
catch (error) {
if (this.continueOnFail()) {
items.push({ json: this.getInputData(itemIndex)[0].json, error, pairedItem: itemIndex });
}
else {
if (error.context) {
error.context.itemIndex = itemIndex;
throw error;
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, {
itemIndex,
});
}
}
}
return this.prepareOutputData(returnData);
}
}
exports.DocCreateField = DocCreateField;
//# sourceMappingURL=DocCreateField.node.js.map