n8n-nodes-pdfops 0.2.1 → 0.3.0

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.
@@ -2,43 +2,41 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Pdfops = void 0;
4
4
  const n8n_workflow_1 = require("n8n-workflow");
5
+ const decodeApiError_1 = require("./decodeApiError");
5
6
  const BASE_URL = 'https://pdfops.dev';
6
- const NODE_PACKAGE_VERSION = '0.2.1';
7
- function decodeApiError(error) {
7
+ const NODE_PACKAGE_VERSION = '0.3.0';
8
+ function withApiMessage(wrapped, apiError, itemIndex) {
8
9
  var _a;
9
- const raw = error === null || error === void 0 ? void 0 : error.response;
10
- const payload = (_a = raw === null || raw === void 0 ? void 0 : raw.data) !== null && _a !== void 0 ? _a : raw === null || raw === void 0 ? void 0 : raw.body;
11
- if (payload === undefined || payload === null)
12
- return undefined;
13
- let parsed;
14
- if (typeof payload === 'object' && !Buffer.isBuffer(payload) && !(payload instanceof Uint8Array)) {
15
- parsed = payload;
10
+ if (itemIndex !== undefined)
11
+ wrapped.context = { ...wrapped.context, itemIndex };
12
+ if (apiError) {
13
+ wrapped.message = apiError.message;
14
+ wrapped.description = (_a = apiError.details) !== null && _a !== void 0 ? _a : null;
16
15
  }
17
- else {
18
- const text = Buffer.isBuffer(payload)
19
- ? payload.toString('utf8')
20
- : payload instanceof Uint8Array
21
- ? Buffer.from(payload).toString('utf8')
22
- : String(payload);
23
- try {
24
- parsed = JSON.parse(text);
25
- }
26
- catch {
27
- return undefined;
28
- }
29
- }
30
- return (parsed === null || parsed === void 0 ? void 0 : parsed.error) ? { message: parsed.error, details: parsed.details } : undefined;
16
+ return wrapped;
31
17
  }
18
+ const DOC_OPS = {
19
+ extractPages: { path: 'extract-pages', output: 'pdf', defaultFile: 'extracted.pdf' },
20
+ deletePages: { path: 'delete-pages', output: 'pdf', defaultFile: 'pages-deleted.pdf' },
21
+ rotate: { path: 'rotate', output: 'pdf', defaultFile: 'rotated.pdf' },
22
+ split: { path: 'split', output: 'parts', defaultFile: 'split' },
23
+ watermark: { path: 'watermark', output: 'pdf', defaultFile: 'watermarked.pdf' },
24
+ pageNumbers: { path: 'page-numbers', output: 'pdf', defaultFile: 'numbered.pdf' },
25
+ unlock: { path: 'unlock', output: 'pdf', defaultFile: 'unlocked.pdf' },
26
+ protect: { path: 'protect', output: 'pdf', defaultFile: 'protected.pdf' },
27
+ metadata: { path: 'metadata', output: 'json', defaultFile: '' },
28
+ setMetadata: { path: 'set-metadata', output: 'pdf', defaultFile: 'metadata-updated.pdf' },
29
+ };
32
30
  class Pdfops {
33
31
  constructor() {
34
32
  this.description = {
35
- displayName: 'PDFops',
33
+ displayName: 'PDFops: Fill PDF Forms, Merge, Invoice',
36
34
  name: 'pdfops',
37
35
  icon: { light: 'file:pdfops.svg', dark: 'file:pdfops.dark.svg' },
38
36
  group: ['transform'],
39
37
  version: 1,
40
38
  subtitle: '={{$parameter["operation"]}}',
41
- description: 'Deterministic PDF operations via the PDFops API — inspect and fill AcroForm fields, merge PDFs, generate invoice PDFs',
39
+ description: 'Fill, merge, split, rotate, watermark, number, protect, unlock and read PDFs and generate invoices. Works without an API key (100 calls/month).',
42
40
  defaults: {
43
41
  name: 'PDFops',
44
42
  },
@@ -58,6 +56,30 @@ class Pdfops {
58
56
  type: 'options',
59
57
  noDataExpression: true,
60
58
  options: [
59
+ {
60
+ name: 'Add Page Numbers',
61
+ value: 'pageNumbers',
62
+ action: 'Add page numbers to a PDF',
63
+ description: 'Stamp page numbers (for example "3 / 12") onto the pages of a PDF and output the numbered PDF',
64
+ },
65
+ {
66
+ name: 'Add Watermark',
67
+ value: 'watermark',
68
+ action: 'Add a text watermark to a PDF',
69
+ description: 'Stamp diagonal or horizontal text (DRAFT, CONFIDENTIAL, a name) over the pages of a PDF and output the watermarked PDF',
70
+ },
71
+ {
72
+ name: 'Delete Pages',
73
+ value: 'deletePages',
74
+ action: 'Delete pages from a PDF',
75
+ description: 'Remove the listed pages from a PDF and output the remaining pages in original order',
76
+ },
77
+ {
78
+ name: 'Extract Pages',
79
+ value: 'extractPages',
80
+ action: 'Extract pages from a PDF',
81
+ description: 'Keep only the listed pages of a PDF, in the order given, and output them as a new PDF (also reorders and duplicates pages)',
82
+ },
61
83
  {
62
84
  name: 'Fill Form',
63
85
  value: 'fill',
@@ -70,6 +92,12 @@ class Pdfops {
70
92
  action: 'Generate an invoice PDF',
71
93
  description: 'Generate a complete invoice PDF from structured JSON — no template needed',
72
94
  },
95
+ {
96
+ name: 'Get Metadata',
97
+ value: 'metadata',
98
+ action: 'Get PDF metadata',
99
+ description: 'Read page count, page sizes, title, author, dates, encryption and form-field count of a PDF as JSON',
100
+ },
73
101
  {
74
102
  name: 'Get Usage',
75
103
  value: 'usage',
@@ -88,6 +116,36 @@ class Pdfops {
88
116
  action: 'Merge many PDF files into one',
89
117
  description: 'Merge the PDFs from all incoming items (in item order) into a single PDF',
90
118
  },
119
+ {
120
+ name: 'Protect PDF',
121
+ value: 'protect',
122
+ action: 'Password protect a PDF',
123
+ description: 'Encrypt a PDF with an open password and/or an owner password that restricts printing, copying and editing',
124
+ },
125
+ {
126
+ name: 'Rotate Pages',
127
+ value: 'rotate',
128
+ action: 'Rotate pages of a PDF',
129
+ description: 'Rotate all or selected pages of a PDF by 90, 180 or 270 degrees',
130
+ },
131
+ {
132
+ name: 'Set Metadata',
133
+ value: 'setMetadata',
134
+ action: 'Set PDF metadata',
135
+ description: 'Set the title, author, subject, keywords or creator of a PDF and output the updated PDF',
136
+ },
137
+ {
138
+ name: 'Split PDF',
139
+ value: 'split',
140
+ action: 'Split a PDF into several pdfs',
141
+ description: 'Split a PDF by page ranges or fixed chunk size; outputs one item per part, each with its own PDF binary',
142
+ },
143
+ {
144
+ name: 'Unlock PDF',
145
+ value: 'unlock',
146
+ action: 'Remove password protection from a PDF',
147
+ description: 'Remove encryption and print/copy/edit restrictions from a PDF you can already open (give the password if it needs one to open)',
148
+ },
91
149
  ],
92
150
  default: 'fill',
93
151
  },
@@ -100,7 +158,7 @@ class Pdfops {
100
158
  hint: 'The name of the input item’s binary field containing the PDF',
101
159
  displayOptions: {
102
160
  show: {
103
- operation: ['fill', 'inspect', 'merge'],
161
+ operation: ['fill', 'inspect', 'merge', 'extractPages', 'deletePages', 'rotate', 'split', 'watermark', 'pageNumbers', 'unlock', 'protect', 'metadata', 'setMetadata'],
104
162
  },
105
163
  },
106
164
  },
@@ -117,6 +175,291 @@ class Pdfops {
117
175
  },
118
176
  },
119
177
  },
178
+ {
179
+ displayName: 'Input PDF Password',
180
+ name: 'password',
181
+ type: 'string',
182
+ typeOptions: { password: true },
183
+ default: '',
184
+ description: 'Password needed to open the input PDF. Leave empty unless the input is encrypted with an open password.',
185
+ displayOptions: {
186
+ show: {
187
+ operation: [
188
+ 'extractPages',
189
+ 'deletePages',
190
+ 'rotate',
191
+ 'split',
192
+ 'watermark',
193
+ 'pageNumbers',
194
+ 'unlock',
195
+ 'protect',
196
+ 'metadata',
197
+ 'setMetadata',
198
+ ],
199
+ },
200
+ },
201
+ },
202
+ {
203
+ displayName: 'Pages',
204
+ name: 'pages',
205
+ type: 'string',
206
+ default: '',
207
+ required: true,
208
+ placeholder: 'e.g. 1-3,5,8-',
209
+ description: '1-based page spec: comma-separated pages and ranges, e.g. "1-3,5,8-" (8- means page 8 to the end). Keeps exactly these pages in the order given, so "3,1" reorders and "1,1" duplicates.',
210
+ displayOptions: { show: { operation: ['extractPages'] } },
211
+ },
212
+ {
213
+ displayName: 'Pages to Delete',
214
+ name: 'pages',
215
+ type: 'string',
216
+ default: '',
217
+ required: true,
218
+ placeholder: 'e.g. 2,4-6,9-',
219
+ description: '1-based page spec of the pages to remove, e.g. "2,4-6,9-" (9- means page 9 to the end). Deleting every page is an error.',
220
+ displayOptions: { show: { operation: ['deletePages'] } },
221
+ },
222
+ {
223
+ displayName: 'Pages',
224
+ name: 'pages',
225
+ type: 'string',
226
+ default: '',
227
+ placeholder: 'e.g. 1-3,5 (empty = all pages)',
228
+ description: '1-based page spec such as "1-3,5". Leave empty to apply to every page.',
229
+ displayOptions: { show: { operation: ['rotate', 'watermark', 'pageNumbers'] } },
230
+ },
231
+ {
232
+ displayName: 'Degrees',
233
+ name: 'degrees',
234
+ type: 'options',
235
+ options: [
236
+ { name: '90 (Clockwise)', value: 90 },
237
+ { name: '180', value: 180 },
238
+ { name: '270 (Clockwise)', value: 270 },
239
+ { name: '-90 (Counter-Clockwise)', value: -90 },
240
+ ],
241
+ default: 90,
242
+ description: 'How far to turn the pages clockwise. Added to each page’s existing rotation, so rotating a page already at 90 by 270 ends at 0.',
243
+ displayOptions: { show: { operation: ['rotate'] } },
244
+ },
245
+ {
246
+ displayName: 'Split By',
247
+ name: 'splitMode',
248
+ type: 'options',
249
+ options: [
250
+ {
251
+ name: 'Page Ranges',
252
+ value: 'ranges',
253
+ description: 'Each comma-separated group becomes one part, e.g. "1-2,3-5" gives two parts',
254
+ },
255
+ {
256
+ name: 'Chunk Size',
257
+ value: 'every',
258
+ description: 'Cut into parts of N pages each (the last part may be shorter)',
259
+ },
260
+ ],
261
+ default: 'ranges',
262
+ displayOptions: { show: { operation: ['split'] } },
263
+ },
264
+ {
265
+ displayName: 'Ranges',
266
+ name: 'ranges',
267
+ type: 'string',
268
+ default: '',
269
+ required: true,
270
+ placeholder: 'e.g. 1-2,3-5',
271
+ description: '1-based page ranges; every comma-separated group becomes its own part, e.g. "1-2,3-5" gives two PDFs and "1,2,3" gives three single-page PDFs',
272
+ displayOptions: { show: { operation: ['split'], splitMode: ['ranges'] } },
273
+ },
274
+ {
275
+ displayName: 'Pages per Part',
276
+ name: 'every',
277
+ type: 'number',
278
+ typeOptions: { minValue: 1 },
279
+ default: 1,
280
+ required: true,
281
+ description: 'Number of pages in each part; for example 2 on a 5-page PDF gives parts 1-2, 3-4 and 5',
282
+ displayOptions: { show: { operation: ['split'], splitMode: ['every'] } },
283
+ },
284
+ {
285
+ displayName: 'Watermark Text',
286
+ name: 'text',
287
+ type: 'string',
288
+ default: 'DRAFT',
289
+ required: true,
290
+ description: 'Text to stamp on the pages, at most 200 characters. Latin (WinAnsi) characters only; other scripts are rejected.',
291
+ displayOptions: { show: { operation: ['watermark'] } },
292
+ },
293
+ {
294
+ displayName: 'Watermark Options',
295
+ name: 'watermarkOptions',
296
+ type: 'collection',
297
+ placeholder: 'Add Option',
298
+ default: {},
299
+ displayOptions: { show: { operation: ['watermark'] } },
300
+ options: [
301
+ {
302
+ displayName: 'Color',
303
+ name: 'color',
304
+ type: 'color',
305
+ default: '#808080',
306
+ description: 'Text colour as hex, #rgb or #rrggbb',
307
+ },
308
+ {
309
+ displayName: 'Opacity',
310
+ name: 'opacity',
311
+ type: 'number',
312
+ typeOptions: { minValue: 0.05, maxValue: 1, numberPrecision: 2 },
313
+ default: 0.15,
314
+ description: 'From 0.05 (faint) to 1 (solid)',
315
+ },
316
+ {
317
+ displayName: 'Position',
318
+ name: 'position',
319
+ type: 'options',
320
+ options: [
321
+ { name: 'Bottom', value: 'bottom' },
322
+ { name: 'Center', value: 'center' },
323
+ { name: 'Top', value: 'top' },
324
+ ],
325
+ default: 'center',
326
+ },
327
+ {
328
+ displayName: 'Rotation',
329
+ name: 'rotation',
330
+ type: 'number',
331
+ default: 45,
332
+ description: 'Degrees counter-clockwise as the viewer sees it; 0 is horizontal',
333
+ },
334
+ {
335
+ displayName: 'Size',
336
+ name: 'size',
337
+ type: 'number',
338
+ typeOptions: { minValue: 6, maxValue: 200 },
339
+ default: 48,
340
+ description: 'Font size in points (6-200)',
341
+ },
342
+ ],
343
+ },
344
+ {
345
+ displayName: 'Page Number Options',
346
+ name: 'pageNumberOptions',
347
+ type: 'collection',
348
+ placeholder: 'Add Option',
349
+ default: {},
350
+ displayOptions: { show: { operation: ['pageNumbers'] } },
351
+ options: [
352
+ {
353
+ displayName: 'Format',
354
+ name: 'format',
355
+ type: 'string',
356
+ default: '{n} / {total}',
357
+ description: 'Label template. {n} is the page number, {total} the page count of the document, e.g. "Page {n} of {total}". At most 100 Latin characters.',
358
+ },
359
+ {
360
+ displayName: 'Margin',
361
+ name: 'margin',
362
+ type: 'number',
363
+ typeOptions: { minValue: 0, maxValue: 200 },
364
+ default: 24,
365
+ description: 'Distance from the page edge in points (0-200)',
366
+ },
367
+ {
368
+ displayName: 'Position',
369
+ name: 'position',
370
+ type: 'options',
371
+ options: [
372
+ { name: 'Bottom Center', value: 'bottom-center' },
373
+ { name: 'Bottom Left', value: 'bottom-left' },
374
+ { name: 'Bottom Right', value: 'bottom-right' },
375
+ { name: 'Top Center', value: 'top-center' },
376
+ { name: 'Top Left', value: 'top-left' },
377
+ { name: 'Top Right', value: 'top-right' },
378
+ ],
379
+ default: 'bottom-center',
380
+ },
381
+ {
382
+ displayName: 'Size',
383
+ name: 'size',
384
+ type: 'number',
385
+ typeOptions: { minValue: 4, maxValue: 72 },
386
+ default: 10,
387
+ description: 'Font size in points (4-72)',
388
+ },
389
+ {
390
+ displayName: 'Start Number',
391
+ name: 'start',
392
+ type: 'number',
393
+ typeOptions: { minValue: 0, maxValue: 100000 },
394
+ default: 1,
395
+ description: 'Number shown on page 1 of the document. Page i shows start+i-1, even when Pages selects only some pages.',
396
+ },
397
+ ],
398
+ },
399
+ {
400
+ displayName: 'Open Password',
401
+ name: 'userPassword',
402
+ type: 'string',
403
+ typeOptions: { password: true },
404
+ default: '',
405
+ description: 'Password required to open the output PDF (at most 128 characters). Set this and/or the Owner Password; at least one is required.',
406
+ displayOptions: { show: { operation: ['protect'] } },
407
+ },
408
+ {
409
+ displayName: 'Owner Password',
410
+ name: 'ownerPassword',
411
+ type: 'string',
412
+ typeOptions: { password: true },
413
+ default: '',
414
+ description: 'Password that lifts the permission restrictions (at most 128 characters). Restrictions only bind a viewer when an owner password is set.',
415
+ displayOptions: { show: { operation: ['protect'] } },
416
+ },
417
+ {
418
+ displayName: 'Protect Options',
419
+ name: 'protectOptions',
420
+ type: 'collection',
421
+ placeholder: 'Add Option',
422
+ default: {},
423
+ displayOptions: { show: { operation: ['protect'] } },
424
+ options: [
425
+ {
426
+ displayName: 'Allowed Permissions',
427
+ name: 'permissions',
428
+ type: 'multiOptions',
429
+ options: [
430
+ { name: 'Annotate', value: 'annotate' },
431
+ { name: 'Copy', value: 'copy' },
432
+ { name: 'Fill Forms', value: 'fill-forms' },
433
+ { name: 'Modify', value: 'modify' },
434
+ { name: 'Print', value: 'print' },
435
+ ],
436
+ default: ['print', 'copy', 'annotate', 'fill-forms'],
437
+ description: 'What viewers may do. Leave this option out to allow everything (except modify when an owner password is set); select none to allow nothing.',
438
+ },
439
+ ],
440
+ },
441
+ {
442
+ displayName: 'Metadata',
443
+ name: 'metadataFields',
444
+ type: 'collection',
445
+ placeholder: 'Add Field',
446
+ default: {},
447
+ description: 'At least one field is required; an empty value clears that field',
448
+ displayOptions: { show: { operation: ['setMetadata'] } },
449
+ options: [
450
+ { displayName: 'Author', name: 'author', type: 'string', default: '' },
451
+ { displayName: 'Creator', name: 'creator', type: 'string', default: '' },
452
+ {
453
+ displayName: 'Keywords',
454
+ name: 'keywords',
455
+ type: 'string',
456
+ default: '',
457
+ description: 'Comma-separated list',
458
+ },
459
+ { displayName: 'Subject', name: 'subject', type: 'string', default: '' },
460
+ { displayName: 'Title', name: 'title', type: 'string', default: '' },
461
+ ],
462
+ },
120
463
  {
121
464
  displayName: 'Invoice',
122
465
  name: 'invoice',
@@ -150,7 +493,7 @@ class Pdfops {
150
493
  hint: 'The name of the output item’s binary field to write the resulting PDF to',
151
494
  displayOptions: {
152
495
  show: {
153
- operation: ['fill', 'merge', 'invoice'],
496
+ operation: ['fill', 'merge', 'invoice', 'extractPages', 'deletePages', 'rotate', 'split', 'watermark', 'pageNumbers', 'unlock', 'protect', 'setMetadata'],
154
497
  },
155
498
  },
156
499
  },
@@ -160,10 +503,10 @@ class Pdfops {
160
503
  type: 'string',
161
504
  default: '',
162
505
  placeholder: 'e.g. filled.pdf',
163
- description: 'File name for the output PDF. Defaults per operation.',
506
+ description: 'File name for the output PDF. Defaults per operation. For Split PDF it is the prefix: parts are named prefix-1.pdf, prefix-2.pdf and so on',
164
507
  displayOptions: {
165
508
  show: {
166
- operation: ['fill', 'merge', 'invoice'],
509
+ operation: ['fill', 'merge', 'invoice', 'extractPages', 'deletePages', 'rotate', 'split', 'watermark', 'pageNumbers', 'unlock', 'protect', 'setMetadata'],
167
510
  },
168
511
  },
169
512
  },
@@ -171,17 +514,11 @@ class Pdfops {
171
514
  };
172
515
  }
173
516
  async execute() {
174
- var _a;
517
+ var _a, _b, _c, _d, _e, _f;
175
518
  const items = this.getInputData();
176
519
  const returnData = [];
177
520
  const operation = this.getNodeParameter('operation', 0);
178
- let hasCredential = true;
179
- try {
180
- await this.getCredentials('pdfopsApi');
181
- }
182
- catch {
183
- hasCredential = false;
184
- }
521
+ const hasCredential = !!((_b = (_a = this.getNode().credentials) === null || _a === void 0 ? void 0 : _a.pdfopsApi) === null || _b === void 0 ? void 0 : _b.id);
185
522
  const request = async (options) => {
186
523
  options.headers = {
187
524
  ...options.headers,
@@ -221,10 +558,10 @@ class Pdfops {
221
558
  const fileName = this.getNodeParameter('fileName', 0) || 'merged.pdf';
222
559
  const outputBinaryPropertyName = this.getNodeParameter('outputBinaryPropertyName', 0);
223
560
  const formData = new FormData();
224
- for (let i = 0; i < items.length; i++) {
225
- formData.append('pdf', await pdfBlob(i, binaryPropertyName), `input-${i}.pdf`);
226
- }
227
561
  try {
562
+ for (let i = 0; i < items.length; i++) {
563
+ formData.append('pdf', await pdfBlob(i, binaryPropertyName), `input-${i}.pdf`);
564
+ }
228
565
  const response = (await request({
229
566
  method: 'POST',
230
567
  url: `${BASE_URL}/api/merge`,
@@ -240,14 +577,18 @@ class Pdfops {
240
577
  });
241
578
  }
242
579
  catch (error) {
580
+ const apiError = (0, decodeApiError_1.decodeApiError)(error);
243
581
  if (this.continueOnFail()) {
244
582
  returnData.push({
245
- json: { error: error.message },
583
+ json: {
584
+ error: (_c = apiError === null || apiError === void 0 ? void 0 : apiError.message) !== null && _c !== void 0 ? _c : error.message,
585
+ ...((apiError === null || apiError === void 0 ? void 0 : apiError.details) ? { message: apiError.details } : {}),
586
+ },
246
587
  pairedItem: { item: 0 },
247
588
  });
248
589
  }
249
590
  else {
250
- throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
591
+ throw withApiMessage(new n8n_workflow_1.NodeApiError(this.getNode(), error), apiError);
251
592
  }
252
593
  }
253
594
  return [returnData];
@@ -258,57 +599,174 @@ class Pdfops {
258
599
  for (let i = 0; i < items.length; i++) {
259
600
  let options;
260
601
  let binaryOut;
261
- if (operation === 'inspect') {
262
- const formData = new FormData();
263
- formData.append('pdf', await pdfBlob(i, this.getNodeParameter('binaryPropertyName', i)), 'input.pdf');
264
- options = { method: 'POST', url: `${BASE_URL}/api/inspect`, body: formData, json: true };
265
- }
266
- else if (operation === 'fill') {
267
- const formData = new FormData();
268
- formData.append('pdf', await pdfBlob(i, this.getNodeParameter('binaryPropertyName', i)), 'input.pdf');
269
- formData.append('fields', JSON.stringify(jsonParam('fields', i)));
270
- if (this.getNodeParameter('flatten', i)) {
271
- formData.append('flatten', 'true');
602
+ let splitOut = false;
603
+ try {
604
+ if (operation === 'inspect') {
605
+ const formData = new FormData();
606
+ formData.append('pdf', await pdfBlob(i, this.getNodeParameter('binaryPropertyName', i)), 'input.pdf');
607
+ options = { method: 'POST', url: `${BASE_URL}/api/inspect`, body: formData, json: true };
608
+ }
609
+ else if (operation === 'fill') {
610
+ const formData = new FormData();
611
+ formData.append('pdf', await pdfBlob(i, this.getNodeParameter('binaryPropertyName', i)), 'input.pdf');
612
+ formData.append('fields', JSON.stringify(jsonParam('fields', i)));
613
+ if (this.getNodeParameter('flatten', i)) {
614
+ formData.append('flatten', 'true');
615
+ }
616
+ options = {
617
+ method: 'POST',
618
+ url: `${BASE_URL}/api/fill-form`,
619
+ body: formData,
620
+ encoding: 'arraybuffer',
621
+ };
622
+ binaryOut = {
623
+ fileName: this.getNodeParameter('fileName', i) || 'filled.pdf',
624
+ property: this.getNodeParameter('outputBinaryPropertyName', i),
625
+ };
626
+ }
627
+ else if (operation === 'invoice') {
628
+ options = {
629
+ method: 'POST',
630
+ url: `${BASE_URL}/api/invoice`,
631
+ body: jsonParam('invoice', i),
632
+ json: true,
633
+ encoding: 'arraybuffer',
634
+ };
635
+ binaryOut = {
636
+ fileName: this.getNodeParameter('fileName', i) || 'invoice.pdf',
637
+ property: this.getNodeParameter('outputBinaryPropertyName', i),
638
+ };
639
+ }
640
+ else if (DOC_OPS[operation]) {
641
+ const spec = DOC_OPS[operation];
642
+ const formData = new FormData();
643
+ formData.append('pdf', await pdfBlob(i, this.getNodeParameter('binaryPropertyName', i)), 'input.pdf');
644
+ const add = (name, value) => {
645
+ if (value !== undefined && value !== null && value !== '') {
646
+ formData.append(name, String(value));
647
+ }
648
+ };
649
+ add('password', this.getNodeParameter('password', i));
650
+ const collection = (name) => this.getNodeParameter(name, i, {});
651
+ if (operation === 'extractPages' || operation === 'deletePages') {
652
+ add('pages', this.getNodeParameter('pages', i));
653
+ }
654
+ else if (operation === 'rotate') {
655
+ add('degrees', this.getNodeParameter('degrees', i));
656
+ add('pages', this.getNodeParameter('pages', i));
657
+ }
658
+ else if (operation === 'split') {
659
+ const mode = this.getNodeParameter('splitMode', i);
660
+ add(mode, this.getNodeParameter(mode, i));
661
+ }
662
+ else if (operation === 'watermark') {
663
+ add('text', this.getNodeParameter('text', i));
664
+ add('pages', this.getNodeParameter('pages', i));
665
+ for (const [k, v] of Object.entries(collection('watermarkOptions')))
666
+ add(k, v);
667
+ }
668
+ else if (operation === 'pageNumbers') {
669
+ add('pages', this.getNodeParameter('pages', i));
670
+ for (const [k, v] of Object.entries(collection('pageNumberOptions')))
671
+ add(k, v);
672
+ }
673
+ else if (operation === 'protect') {
674
+ add('user_password', this.getNodeParameter('userPassword', i));
675
+ add('owner_password', this.getNodeParameter('ownerPassword', i));
676
+ const { permissions } = collection('protectOptions');
677
+ if (Array.isArray(permissions))
678
+ formData.append('permissions', permissions.join(','));
679
+ }
680
+ else if (operation === 'setMetadata') {
681
+ for (const [k, v] of Object.entries(collection('metadataFields'))) {
682
+ formData.append(k, String(v !== null && v !== void 0 ? v : ''));
683
+ }
684
+ }
685
+ if (spec.output === 'pdf') {
686
+ options = {
687
+ method: 'POST',
688
+ url: `${BASE_URL}/api/${spec.path}`,
689
+ body: formData,
690
+ encoding: 'arraybuffer',
691
+ };
692
+ binaryOut = {
693
+ fileName: this.getNodeParameter('fileName', i) || spec.defaultFile,
694
+ property: this.getNodeParameter('outputBinaryPropertyName', i),
695
+ };
696
+ }
697
+ else {
698
+ options = {
699
+ method: 'POST',
700
+ url: `${BASE_URL}/api/${spec.path}`,
701
+ body: formData,
702
+ json: true,
703
+ };
704
+ if (spec.output === 'parts') {
705
+ splitOut = true;
706
+ binaryOut = {
707
+ fileName: (this.getNodeParameter('fileName', i) || spec.defaultFile).replace(/\.pdf$/i, ''),
708
+ property: this.getNodeParameter('outputBinaryPropertyName', i),
709
+ };
710
+ }
711
+ }
712
+ }
713
+ else if (operation === 'usage') {
714
+ options = { method: 'GET', url: `${BASE_URL}/api/usage`, json: true };
715
+ }
716
+ else {
717
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation "${operation}"`, {
718
+ itemIndex: i,
719
+ });
272
720
  }
273
- options = {
274
- method: 'POST',
275
- url: `${BASE_URL}/api/fill-form`,
276
- body: formData,
277
- encoding: 'arraybuffer',
278
- };
279
- binaryOut = {
280
- fileName: this.getNodeParameter('fileName', i) || 'filled.pdf',
281
- property: this.getNodeParameter('outputBinaryPropertyName', i),
282
- };
283
- }
284
- else if (operation === 'invoice') {
285
- options = {
286
- method: 'POST',
287
- url: `${BASE_URL}/api/invoice`,
288
- body: jsonParam('invoice', i),
289
- json: true,
290
- encoding: 'arraybuffer',
291
- };
292
- binaryOut = {
293
- fileName: this.getNodeParameter('fileName', i) || 'invoice.pdf',
294
- property: this.getNodeParameter('outputBinaryPropertyName', i),
295
- };
296
- }
297
- else if (operation === 'usage') {
298
- options = { method: 'GET', url: `${BASE_URL}/api/usage`, json: true };
299
721
  }
300
- else {
301
- throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Unknown operation "${operation}"`, {
302
- itemIndex: i,
303
- });
722
+ catch (error) {
723
+ if (this.continueOnFail()) {
724
+ returnData.push({ json: { error: error.message }, pairedItem: { item: i } });
725
+ continue;
726
+ }
727
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i });
304
728
  }
305
729
  try {
730
+ if (binaryOut && !splitOut)
731
+ options.returnFullResponse = true;
306
732
  const response = await request(options);
307
- if (binaryOut) {
733
+ if (splitOut && binaryOut) {
734
+ const parts = response.parts;
735
+ if (!Array.isArray(parts)) {
736
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Unexpected split response: no parts', {
737
+ itemIndex: i,
738
+ });
739
+ }
740
+ for (let n = 0; n < parts.length; n++) {
741
+ const name = `${binaryOut.fileName}-${n + 1}.pdf`;
742
+ returnData.push({
743
+ json: { pages: parts[n].pages, page_count: parts[n].page_count, part: n + 1 },
744
+ binary: {
745
+ [binaryOut.property]: await this.helpers.prepareBinaryData(Buffer.from(parts[n].pdf, 'base64'), name, 'application/pdf'),
746
+ },
747
+ pairedItem: { item: i },
748
+ });
749
+ }
750
+ }
751
+ else if (binaryOut) {
752
+ const full = response;
753
+ const json = { fileName: binaryOut.fileName };
754
+ const fieldMap = (_d = full.headers) === null || _d === void 0 ? void 0 : _d['x-pdfops-field-map'];
755
+ if (fieldMap) {
756
+ try {
757
+ json.field_map = JSON.parse(fieldMap);
758
+ }
759
+ catch {
760
+ json.field_map = fieldMap;
761
+ }
762
+ }
763
+ const note = (_e = full.headers) === null || _e === void 0 ? void 0 : _e['x-pdfops-note'];
764
+ if (note)
765
+ json.note = note;
308
766
  returnData.push({
309
- json: { fileName: binaryOut.fileName },
767
+ json,
310
768
  binary: {
311
- [binaryOut.property]: await this.helpers.prepareBinaryData(response, binaryOut.fileName, 'application/pdf'),
769
+ [binaryOut.property]: await this.helpers.prepareBinaryData(full.body, binaryOut.fileName, 'application/pdf'),
312
770
  },
313
771
  pairedItem: { item: i },
314
772
  });
@@ -318,21 +776,18 @@ class Pdfops {
318
776
  }
319
777
  }
320
778
  catch (error) {
321
- const apiError = decodeApiError(error);
779
+ const apiError = (0, decodeApiError_1.decodeApiError)(error);
322
780
  if (this.continueOnFail()) {
323
781
  returnData.push({
324
782
  json: {
325
- error: (_a = apiError === null || apiError === void 0 ? void 0 : apiError.message) !== null && _a !== void 0 ? _a : error.message,
326
- ...((apiError === null || apiError === void 0 ? void 0 : apiError.details) ? { details: apiError.details } : {}),
783
+ error: (_f = apiError === null || apiError === void 0 ? void 0 : apiError.message) !== null && _f !== void 0 ? _f : error.message,
784
+ ...((apiError === null || apiError === void 0 ? void 0 : apiError.details) ? { message: apiError.details } : {}),
327
785
  },
328
786
  pairedItem: { item: i },
329
787
  });
330
788
  continue;
331
789
  }
332
- throw new n8n_workflow_1.NodeApiError(this.getNode(), error, {
333
- itemIndex: i,
334
- ...(apiError ? { message: apiError.message, description: apiError.details } : {}),
335
- });
790
+ throw withApiMessage(new n8n_workflow_1.NodeApiError(this.getNode(), error, { itemIndex: i }), apiError, i);
336
791
  }
337
792
  }
338
793
  return [returnData];