n8n-nodes-pdfbro 0.1.3 → 0.1.6

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.
@@ -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,31 @@ 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 all inputs into one PDF
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 binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
93
- const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
94
- const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
95
- const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
96
- copiedPages.forEach((page) => mergedPdf.addPage(page));
149
+ const binaryProps = this.getNodeParameter('binaryProperties', i);
150
+ let propsToMerge = [];
151
+ if (binaryProps && binaryProps.properties) {
152
+ propsToMerge = binaryProps.properties.map((p) => p.property);
153
+ }
154
+ // Fallback: If no properties specified, try 'data' default
155
+ if (propsToMerge.length === 0) {
156
+ propsToMerge.push('data');
157
+ }
158
+ for (const propName of propsToMerge) {
159
+ // Check if binary data exists
160
+ if (!items[i].binary || !items[i].binary[propName]) {
161
+ continue;
162
+ }
163
+ const validBuffer = await this.helpers.getBinaryDataBuffer(i, propName);
164
+ const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
165
+ const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
166
+ copiedPages.forEach((page) => mergedPdf.addPage(page));
167
+ }
97
168
  }
98
169
  catch (error) {
99
170
  if (this.continueOnFail()) {
@@ -102,87 +173,99 @@ class PdfBro {
102
173
  throw error;
103
174
  }
104
175
  }
176
+ if (mergedPdf.getPageCount() === 0) {
177
+ throw new Error('No PDF pages found to merge! Please check your input binary fields.');
178
+ }
179
+ // Return single item
105
180
  const mergedPdfBuffer = await mergedPdf.save();
181
+ // Use the first item's JSON as a base, or empty if no items
182
+ const jsonOutput = items.length > 0 ? items[0].json : {};
183
+ const binaryOutput = items.length > 0 && items[0].binary ? items[0].binary : {};
106
184
  returnData.push({
107
- json: { success: true, pageCount: mergedPdf.getPageCount() },
185
+ json: { ...jsonOutput, success: true, pageCount: mergedPdf.getPageCount() },
108
186
  binary: {
109
- [binaryPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(mergedPdfBuffer), 'merged.pdf', 'application/pdf'),
187
+ ...binaryOutput,
188
+ [outputPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(mergedPdfBuffer), 'merged.pdf', 'application/pdf'),
110
189
  },
111
190
  });
112
191
  }
113
- else if (operation === 'split') {
114
- // Split each input PDF into single pages
115
- for (let i = 0; i < items.length; i++) {
116
- const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
117
- const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
118
- const numberOfPages = pdf.getPageCount();
119
- for (let j = 0; j < numberOfPages; j++) {
120
- const newPdf = await pdf_lib_1.PDFDocument.create();
121
- const [copiedPage] = await newPdf.copyPages(pdf, [j]);
122
- newPdf.addPage(copiedPage);
123
- const newPdfBuffer = await newPdf.save();
192
+ else {
193
+ // For other operations, get the single binaryPropertyName
194
+ const binaryPropertyName = this.getNodeParameter('binaryPropertyName', 0);
195
+ if (operation === 'split') {
196
+ // Split each input PDF into single pages
197
+ for (let i = 0; i < items.length; i++) {
198
+ const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
199
+ const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
200
+ const numberOfPages = pdf.getPageCount();
201
+ for (let j = 0; j < numberOfPages; j++) {
202
+ const newPdf = await pdf_lib_1.PDFDocument.create();
203
+ const [copiedPage] = await newPdf.copyPages(pdf, [j]);
204
+ newPdf.addPage(copiedPage);
205
+ const newPdfBuffer = await newPdf.save();
206
+ returnData.push({
207
+ json: { ...items[i].json, pageNumber: j + 1, totalPages: numberOfPages },
208
+ binary: {
209
+ [binaryPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(newPdfBuffer), `page_${j + 1}.pdf`, 'application/pdf'),
210
+ },
211
+ });
212
+ }
213
+ }
214
+ }
215
+ else if (operation === 'rotate') {
216
+ const degreesVal = this.getNodeParameter('rotationDegrees', 0);
217
+ for (let i = 0; i < items.length; i++) {
218
+ const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
219
+ const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
220
+ const pages = pdf.getPages();
221
+ pages.forEach(page => {
222
+ const currentRotation = page.getRotation().angle;
223
+ page.setRotation((0, pdf_lib_1.degrees)(currentRotation + degreesVal));
224
+ });
225
+ const rotatedPdfBuffer = await pdf.save();
124
226
  returnData.push({
125
- json: { ...items[i].json, pageNumber: j + 1, totalPages: numberOfPages },
227
+ json: items[i].json,
126
228
  binary: {
127
- [binaryPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(newPdfBuffer), `page_${j + 1}.pdf`, 'application/pdf'),
229
+ [binaryPropertyName]: await this.helpers.prepareBinaryData(Buffer.from(rotatedPdfBuffer), 'rotated.pdf', 'application/pdf'),
128
230
  },
129
231
  });
130
232
  }
131
233
  }
132
- }
133
- else if (operation === 'rotate') {
134
- const degreesVal = this.getNodeParameter('rotationDegrees', 0);
135
- for (let i = 0; i < items.length; i++) {
136
- const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
137
- const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
138
- const pages = pdf.getPages();
139
- pages.forEach(page => {
140
- const currentRotation = page.getRotation().angle;
141
- page.setRotation((0, pdf_lib_1.degrees)(currentRotation + degreesVal));
142
- });
143
- const rotatedPdfBuffer = await pdf.save();
144
- returnData.push({
145
- json: items[i].json,
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
- });
234
+ else if (operation === 'extractText') {
235
+ for (let i = 0; i < items.length; i++) {
236
+ const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
237
+ const data = await (0, pdf_parse_1.default)(validBuffer);
238
+ returnData.push({
239
+ json: {
240
+ ...items[i].json,
241
+ text: data.text,
242
+ numpages: data.numpages,
243
+ info: data.info,
244
+ },
245
+ binary: items[i].binary,
246
+ });
247
+ }
165
248
  }
166
- }
167
- else if (operation === 'metadata') {
168
- for (let i = 0; i < items.length; i++) {
169
- const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
170
- const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
171
- returnData.push({
172
- json: {
173
- ...items[i].json,
174
- title: pdf.getTitle(),
175
- author: pdf.getAuthor(),
176
- subject: pdf.getSubject(),
177
- creator: pdf.getCreator(),
178
- producer: pdf.getProducer(),
179
- keywords: pdf.getKeywords(),
180
- pageCount: pdf.getPageCount(),
181
- creationDate: pdf.getCreationDate(),
182
- modificationDate: pdf.getModificationDate(),
183
- },
184
- binary: items[i].binary,
185
- });
249
+ else if (operation === 'metadata') {
250
+ for (let i = 0; i < items.length; i++) {
251
+ const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
252
+ const pdf = await pdf_lib_1.PDFDocument.load(validBuffer);
253
+ returnData.push({
254
+ json: {
255
+ ...items[i].json,
256
+ title: pdf.getTitle(),
257
+ author: pdf.getAuthor(),
258
+ subject: pdf.getSubject(),
259
+ creator: pdf.getCreator(),
260
+ producer: pdf.getProducer(),
261
+ keywords: pdf.getKeywords(),
262
+ pageCount: pdf.getPageCount(),
263
+ creationDate: pdf.getCreationDate(),
264
+ modificationDate: pdf.getModificationDate(),
265
+ },
266
+ binary: items[i].binary,
267
+ });
268
+ }
186
269
  }
187
270
  }
188
271
  return [returnData];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-pdfbro",
3
- "version": "0.1.3",
3
+ "version": "0.1.6",
4
4
  "description": "Offline PDF utility node for n8n",
5
5
  "keywords": [
6
6
  "n8n-community-node"