n8n-nodes-pdfbro 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/nodes/PdfUtils/PdfBro.node.js +150 -75
- package/package.json +1 -1
|
@@ -56,14 +56,71 @@ class PdfBro {
|
|
|
56
56
|
],
|
|
57
57
|
default: 'merge',
|
|
58
58
|
},
|
|
59
|
+
/*
|
|
60
|
+
displayName: 'Input Binary Field',
|
|
61
|
+
name: 'binaryPropertyName',
|
|
62
|
+
type: 'string',
|
|
63
|
+
default: 'data',
|
|
64
|
+
required: true,
|
|
65
|
+
description: 'The name of the binary field containing the PDF',
|
|
66
|
+
*/
|
|
67
|
+
// Replaced with fixedCollection for merge, simple string for others
|
|
68
|
+
{
|
|
69
|
+
displayName: 'Input Binary Fields',
|
|
70
|
+
name: 'binaryProperties',
|
|
71
|
+
type: 'fixedCollection',
|
|
72
|
+
typeOptions: {
|
|
73
|
+
multipleValues: true,
|
|
74
|
+
},
|
|
75
|
+
displayOptions: {
|
|
76
|
+
show: {
|
|
77
|
+
operation: ['merge'],
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
default: {},
|
|
81
|
+
options: [
|
|
82
|
+
{
|
|
83
|
+
name: 'properties',
|
|
84
|
+
displayName: 'Binary Property',
|
|
85
|
+
values: [
|
|
86
|
+
{
|
|
87
|
+
displayName: 'Property Name',
|
|
88
|
+
name: 'property',
|
|
89
|
+
type: 'string',
|
|
90
|
+
default: 'data',
|
|
91
|
+
description: 'Name of the binary property to merge',
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
description: 'List of binary properties to merge (in order)',
|
|
97
|
+
},
|
|
59
98
|
{
|
|
60
99
|
displayName: 'Input Binary Field',
|
|
61
100
|
name: 'binaryPropertyName',
|
|
62
101
|
type: 'string',
|
|
63
102
|
default: 'data',
|
|
64
103
|
required: true,
|
|
104
|
+
displayOptions: {
|
|
105
|
+
hide: {
|
|
106
|
+
operation: ['merge'],
|
|
107
|
+
},
|
|
108
|
+
},
|
|
65
109
|
description: 'The name of the binary field containing the PDF',
|
|
66
110
|
},
|
|
111
|
+
{
|
|
112
|
+
displayName: 'Output Binary Field',
|
|
113
|
+
name: 'outputPropertyName',
|
|
114
|
+
type: 'string',
|
|
115
|
+
default: 'merged',
|
|
116
|
+
required: true,
|
|
117
|
+
displayOptions: {
|
|
118
|
+
show: {
|
|
119
|
+
operation: ['merge'],
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
description: 'The name of the binary field to put the merged PDF in',
|
|
123
|
+
},
|
|
67
124
|
{
|
|
68
125
|
displayName: 'Rotation Degrees',
|
|
69
126
|
name: 'rotationDegrees',
|
|
@@ -83,17 +140,26 @@ class PdfBro {
|
|
|
83
140
|
const items = this.getInputData();
|
|
84
141
|
const returnData = [];
|
|
85
142
|
const operation = this.getNodeParameter('operation', 0);
|
|
86
|
-
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0);
|
|
87
143
|
if (operation === 'merge') {
|
|
88
|
-
// Merge
|
|
144
|
+
// Merge multiple binary properties FROM ALL ITEMS into ONE SINGLE PDF
|
|
145
|
+
const outputPropertyName = this.getNodeParameter('outputPropertyName', 0);
|
|
89
146
|
const mergedPdf = await pdf_lib_1.PDFDocument.create();
|
|
90
147
|
for (let i = 0; i < items.length; i++) {
|
|
91
148
|
try {
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
149
|
+
const binaryProps = this.getNodeParameter('binaryProperties', i);
|
|
150
|
+
if (binaryProps && binaryProps.properties) {
|
|
151
|
+
for (const propItem of binaryProps.properties) {
|
|
152
|
+
const propName = propItem.property;
|
|
153
|
+
// Check if binary data exists
|
|
154
|
+
if (!items[i].binary || !items[i].binary[propName]) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const validBuffer = await this.helpers.getBinaryDataBuffer(i, propName);
|
|
158
|
+
const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
|
|
159
|
+
const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
|
|
160
|
+
copiedPages.forEach((page) => mergedPdf.addPage(page));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
97
163
|
}
|
|
98
164
|
catch (error) {
|
|
99
165
|
if (this.continueOnFail()) {
|
|
@@ -102,87 +168,96 @@ class PdfBro {
|
|
|
102
168
|
throw error;
|
|
103
169
|
}
|
|
104
170
|
}
|
|
171
|
+
// Return single item
|
|
105
172
|
const mergedPdfBuffer = await mergedPdf.save();
|
|
173
|
+
// Use the first item's JSON as a base, or empty if no items
|
|
174
|
+
const jsonOutput = items.length > 0 ? items[0].json : {};
|
|
175
|
+
const binaryOutput = items.length > 0 && items[0].binary ? items[0].binary : {};
|
|
106
176
|
returnData.push({
|
|
107
|
-
json: { success: true, pageCount: mergedPdf.getPageCount() },
|
|
177
|
+
json: { ...jsonOutput, success: true, pageCount: mergedPdf.getPageCount() },
|
|
108
178
|
binary: {
|
|
109
|
-
|
|
179
|
+
...binaryOutput,
|
|
180
|
+
[outputPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(mergedPdfBuffer), 'merged.pdf', 'application/pdf'),
|
|
110
181
|
},
|
|
111
182
|
});
|
|
112
183
|
}
|
|
113
|
-
else
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
184
|
+
else {
|
|
185
|
+
// For other operations, get the single binaryPropertyName
|
|
186
|
+
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0);
|
|
187
|
+
if (operation === 'split') {
|
|
188
|
+
// Split each input PDF into single pages
|
|
189
|
+
for (let i = 0; i < items.length; i++) {
|
|
190
|
+
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
191
|
+
const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
|
|
192
|
+
const numberOfPages = pdf.getPageCount();
|
|
193
|
+
for (let j = 0; j < numberOfPages; j++) {
|
|
194
|
+
const newPdf = await pdf_lib_1.PDFDocument.create();
|
|
195
|
+
const [copiedPage] = await newPdf.copyPages(pdf, [j]);
|
|
196
|
+
newPdf.addPage(copiedPage);
|
|
197
|
+
const newPdfBuffer = await newPdf.save();
|
|
198
|
+
returnData.push({
|
|
199
|
+
json: { ...items[i].json, pageNumber: j + 1, totalPages: numberOfPages },
|
|
200
|
+
binary: {
|
|
201
|
+
[binaryPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(newPdfBuffer), `page_${j + 1}.pdf`, 'application/pdf'),
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else if (operation === 'rotate') {
|
|
208
|
+
const degreesVal = this.getNodeParameter('rotationDegrees', 0);
|
|
209
|
+
for (let i = 0; i < items.length; i++) {
|
|
210
|
+
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
211
|
+
const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
|
|
212
|
+
const pages = pdf.getPages();
|
|
213
|
+
pages.forEach(page => {
|
|
214
|
+
const currentRotation = page.getRotation().angle;
|
|
215
|
+
page.setRotation((0, pdf_lib_1.degrees)(currentRotation + degreesVal));
|
|
216
|
+
});
|
|
217
|
+
const rotatedPdfBuffer = await pdf.save();
|
|
124
218
|
returnData.push({
|
|
125
|
-
json:
|
|
219
|
+
json: items[i].json,
|
|
126
220
|
binary: {
|
|
127
|
-
[binaryPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(
|
|
221
|
+
[binaryPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(rotatedPdfBuffer), 'rotated.pdf', 'application/pdf'),
|
|
128
222
|
},
|
|
129
223
|
});
|
|
130
224
|
}
|
|
131
225
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
binary: {
|
|
147
|
-
[binaryPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(rotatedPdfBuffer), 'rotated.pdf', 'application/pdf'),
|
|
148
|
-
},
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
else if (operation === 'extractText') {
|
|
153
|
-
for (let i = 0; i < items.length; i++) {
|
|
154
|
-
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
155
|
-
const data = await (0, pdf_parse_1.default)(validBuffer);
|
|
156
|
-
returnData.push({
|
|
157
|
-
json: {
|
|
158
|
-
...items[i].json,
|
|
159
|
-
text: data.text,
|
|
160
|
-
numpages: data.numpages,
|
|
161
|
-
info: data.info,
|
|
162
|
-
},
|
|
163
|
-
binary: items[i].binary,
|
|
164
|
-
});
|
|
226
|
+
else if (operation === 'extractText') {
|
|
227
|
+
for (let i = 0; i < items.length; i++) {
|
|
228
|
+
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
229
|
+
const data = await (0, pdf_parse_1.default)(validBuffer);
|
|
230
|
+
returnData.push({
|
|
231
|
+
json: {
|
|
232
|
+
...items[i].json,
|
|
233
|
+
text: data.text,
|
|
234
|
+
numpages: data.numpages,
|
|
235
|
+
info: data.info,
|
|
236
|
+
},
|
|
237
|
+
binary: items[i].binary,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
165
240
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
241
|
+
else if (operation === 'metadata') {
|
|
242
|
+
for (let i = 0; i < items.length; i++) {
|
|
243
|
+
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
244
|
+
const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
|
|
245
|
+
returnData.push({
|
|
246
|
+
json: {
|
|
247
|
+
...items[i].json,
|
|
248
|
+
title: pdf.getTitle(),
|
|
249
|
+
author: pdf.getAuthor(),
|
|
250
|
+
subject: pdf.getSubject(),
|
|
251
|
+
creator: pdf.getCreator(),
|
|
252
|
+
producer: pdf.getProducer(),
|
|
253
|
+
keywords: pdf.getKeywords(),
|
|
254
|
+
pageCount: pdf.getPageCount(),
|
|
255
|
+
creationDate: pdf.getCreationDate(),
|
|
256
|
+
modificationDate: pdf.getModificationDate(),
|
|
257
|
+
},
|
|
258
|
+
binary: items[i].binary,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
186
261
|
}
|
|
187
262
|
}
|
|
188
263
|
return [returnData];
|