@custom-js/n8n-nodes-pdf-toolkit 1.48.0 → 1.50.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.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InvoiceGenerator = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
4
5
  class InvoiceGenerator {
5
6
  constructor() {
6
7
  this.description = {
@@ -187,56 +188,57 @@ class InvoiceGenerator {
187
188
  const items = this.getInputData();
188
189
  const returnData = [];
189
190
  for (let i = 0; i < items.length; i++) {
190
- const credentials = await this.getCredentials('customJsApi');
191
- const pdfTemplate = this.getNodeParameter('pdfTemplate', i);
192
- const issuer = this.getNodeParameter('issuer.issuerValues', i);
193
- const payment = this.getNodeParameter('payment.paymentValues', i);
194
- const recipient = this.getNodeParameter('recipient.recipientValues', i);
195
- const billing = this.getNodeParameter('billing.billingValues', i);
196
- const itemsMode = this.getNodeParameter('itemsMode', i);
197
- let invoiceItems;
198
- if (itemsMode === 'json') {
199
- const itemsJson = this.getNodeParameter('itemsJson', i);
200
- if (typeof itemsJson === 'string') {
201
- try {
202
- invoiceItems = JSON.parse(itemsJson);
203
- }
204
- catch (error) {
205
- if (error instanceof Error) {
206
- throw new Error(`Invalid JSON in 'Items JSON' field: ${error.message}`);
191
+ try { // Added try block
192
+ const credentials = await this.getCredentials('customJsApi');
193
+ const pdfTemplate = this.getNodeParameter('pdfTemplate', i);
194
+ const issuer = this.getNodeParameter('issuer.issuerValues', i);
195
+ const payment = this.getNodeParameter('payment.paymentValues', i);
196
+ const recipient = this.getNodeParameter('recipient.recipientValues', i);
197
+ const billing = this.getNodeParameter('billing.billingValues', i);
198
+ const itemsMode = this.getNodeParameter('itemsMode', i);
199
+ let invoiceItems;
200
+ if (itemsMode === 'json') {
201
+ const itemsJson = this.getNodeParameter('itemsJson', i);
202
+ if (typeof itemsJson === 'string') {
203
+ try {
204
+ invoiceItems = JSON.parse(itemsJson);
205
+ }
206
+ catch (error) {
207
+ if (error instanceof Error) {
208
+ throw new Error(`Invalid JSON in 'Items JSON' field: ${error.message}`);
209
+ }
210
+ throw new Error(`Invalid JSON in 'Items JSON' field: ${String(error)}`);
207
211
  }
208
- throw new Error(`Invalid JSON in 'Items JSON' field: ${String(error)}`);
209
212
  }
210
- }
211
- else if (Array.isArray(itemsJson)) {
212
- invoiceItems = itemsJson;
213
+ else if (Array.isArray(itemsJson)) {
214
+ invoiceItems = itemsJson;
215
+ }
216
+ else {
217
+ invoiceItems = [];
218
+ }
213
219
  }
214
220
  else {
215
- invoiceItems = [];
221
+ const itemsData = this.getNodeParameter('items', i);
222
+ invoiceItems = itemsData.itemsValues;
216
223
  }
217
- }
218
- else {
219
- const itemsData = this.getNodeParameter('items', i);
220
- invoiceItems = itemsData.itemsValues;
221
- }
222
- invoiceItems = invoiceItems.map(item => {
223
- return {
224
- description: item.description,
225
- quantity: Number(item.quantity),
226
- unitPrice: Number(item.unitPrice),
224
+ invoiceItems = invoiceItems.map(item => {
225
+ return {
226
+ description: item.description,
227
+ quantity: Number(item.quantity),
228
+ unitPrice: Number(item.unitPrice),
229
+ };
230
+ });
231
+ const invoiceData = {
232
+ issuer,
233
+ payment,
234
+ recipient,
235
+ billing,
236
+ items: invoiceItems,
227
237
  };
228
- });
229
- const invoiceData = {
230
- issuer,
231
- payment,
232
- recipient,
233
- billing,
234
- items: invoiceItems,
235
- };
236
- const code = `
237
- const { HTML2PDF } = requir` + `e('./utils');
238
- const nunjucks = requir` + `e('nunjucks');
239
- const fetch = requir` + `e('node-fetch');
238
+ const code = `
239
+ const { HTML2PDF } = require('./utils');
240
+ const nunjucks = require('nunjucks');
241
+ const fetch = require('node-fetch');
240
242
  const tpl = 'https://www.customjs.space/pdf-templates/Invoice1.html';
241
243
  const templateString = await fetch(tpl).then(r => r.text());
242
244
  const renderedHtml = await nunjucks.renderString(
@@ -245,38 +247,59 @@ class InvoiceGenerator {
245
247
 
246
248
  }
247
249
  );
248
- console.log('renderedHtml', renderedHtml);
250
+
249
251
  return HTML2PDF(renderedHtml);
250
252
  `;
251
- const options = {
252
- url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
253
- method: 'POST',
254
- headers: {
255
- 'customjs-origin': 'n8n/invoice-generator',
256
- 'x-api-key': credentials.apiKey,
257
- },
258
- body: {
259
- input: invoiceData,
260
- code: code,
261
- returnBinary: 'true',
262
- },
263
- encoding: 'arraybuffer',
264
- json: true,
265
- };
266
- const response = await this.helpers.httpRequest(options);
267
- if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
253
+ const options = {
254
+ url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
255
+ method: 'POST',
256
+ headers: {
257
+ 'customjs-origin': 'n8n/invoice-generator',
258
+ },
259
+ body: {
260
+ input: invoiceData,
261
+ code: code,
262
+ returnBinary: 'true',
263
+ },
264
+ encoding: null,
265
+ json: true,
266
+ };
267
+ const response = await this.helpers.requestWithAuthentication.call(this, 'customJsApi', options);
268
+ if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
269
+ returnData.push({
270
+ json: items[i].json,
271
+ pairedItem: {
272
+ item: i,
273
+ },
274
+ });
275
+ continue;
276
+ }
277
+ const binaryData = await this.helpers.prepareBinaryData(response, "Invoice.pdf");
268
278
  returnData.push({
269
279
  json: items[i].json,
280
+ binary: {
281
+ data: binaryData,
282
+ },
283
+ pairedItem: {
284
+ item: i,
285
+ },
270
286
  });
271
- continue;
272
287
  }
273
- const binaryData = await this.helpers.prepareBinaryData(response, "Invoice.pdf");
274
- returnData.push({
275
- json: items[i].json,
276
- binary: {
277
- data: binaryData,
278
- },
279
- });
288
+ catch (error) { // Added catch block
289
+ const errorMessage = error instanceof Error ? error.message : String(error);
290
+ if (this.continueOnFail()) {
291
+ returnData.push({
292
+ json: {
293
+ error: errorMessage,
294
+ },
295
+ pairedItem: {
296
+ item: i,
297
+ },
298
+ });
299
+ continue;
300
+ }
301
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i });
302
+ }
280
303
  }
281
304
  return [returnData];
282
305
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Markdown2Html = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
4
5
  class Markdown2Html {
5
6
  constructor() {
6
7
  this.description = {
@@ -40,28 +41,47 @@ class Markdown2Html {
40
41
  const items = this.getInputData();
41
42
  const returnData = [];
42
43
  for (let i = 0; i < items.length; i++) {
43
- const credentials = await this.getCredentials("customJsApi");
44
- const markdownInput = this.getNodeParameter("markdownInput", i);
45
- const options = {
46
- url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
47
- method: 'POST',
48
- headers: {
49
- "customjs-origin": "n8n/markdown2html",
50
- "x-api-key": credentials.apiKey,
51
- },
52
- body: {
53
- input: markdownInput,
54
- code: "const { MD2HTML } = requir" + "e('./utils'); return MD2HTML(input)",
55
- returnBinary: "false",
56
- },
57
- json: true,
58
- };
59
- const response = await this.helpers.httpRequest(options);
60
- returnData.push({
61
- json: {
62
- output: response.toString(),
63
- },
64
- });
44
+ try {
45
+ const credentials = await this.getCredentials("customJsApi");
46
+ const markdownInput = this.getNodeParameter("markdownInput", i);
47
+ const options = {
48
+ url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
49
+ method: 'POST',
50
+ headers: {
51
+ "customjs-origin": "n8n/markdown2html",
52
+ },
53
+ body: {
54
+ input: markdownInput,
55
+ code: "const { MD2HTML } = require('./utils'); return MD2HTML(input)",
56
+ returnBinary: "false",
57
+ },
58
+ json: true,
59
+ };
60
+ const response = await this.helpers.requestWithAuthentication.call(this, 'customJsApi', options);
61
+ returnData.push({
62
+ json: {
63
+ output: response.toString(),
64
+ },
65
+ pairedItem: {
66
+ item: i,
67
+ },
68
+ });
69
+ }
70
+ catch (error) {
71
+ const errorMessage = error instanceof Error ? error.message : String(error);
72
+ if (this.continueOnFail()) {
73
+ returnData.push({
74
+ json: {
75
+ error: errorMessage,
76
+ },
77
+ pairedItem: {
78
+ item: i,
79
+ },
80
+ });
81
+ continue;
82
+ }
83
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i });
84
+ }
65
85
  }
66
86
  return [returnData];
67
87
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MergePdfs = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
4
5
  class MergePdfs {
5
6
  constructor() {
6
7
  this.description = {
@@ -52,49 +53,71 @@ class MergePdfs {
52
53
  async execute() {
53
54
  const items = this.getInputData();
54
55
  const returnData = [];
55
- const credentials = await this.getCredentials("customJsApi");
56
- const isBinary = this.getNodeParameter("resource", 0) === "binary";
57
- const field_name = this.getNodeParameter("field_name", 0);
58
- const files = isBinary ? items.map((item, i) => {
59
- var _a;
60
- if ((_a = item.binary) === null || _a === void 0 ? void 0 : _a.data) {
61
- return Buffer.from(item.binary.data.data, "base64");
62
- }
63
- }) : [];
64
- const urls = !isBinary ? field_name : [];
65
- const options = {
66
- url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
67
- method: 'POST',
68
- headers: {
69
- "customjs-origin": "n8n/mergePDFs",
70
- "x-api-key": credentials.apiKey,
71
- },
72
- body: {
73
- input: isBinary ? { files } : { urls },
74
- code: `
75
- const { PDF_MERGE } = requir` + `e('./utils');
56
+ try {
57
+ const credentials = await this.getCredentials("customJsApi");
58
+ const isBinary = this.getNodeParameter("resource", 0) === "binary";
59
+ const field_name = this.getNodeParameter("field_name", 0);
60
+ const files = isBinary ? items.map((item, i) => {
61
+ var _a;
62
+ if ((_a = item.binary) === null || _a === void 0 ? void 0 : _a.data) {
63
+ return Buffer.from(item.binary.data.data, "base64");
64
+ }
65
+ }) : [];
66
+ const urls = !isBinary ? field_name : [];
67
+ const options = {
68
+ url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
69
+ method: 'POST',
70
+ headers: {
71
+ "customjs-origin": "n8n/mergePDFs",
72
+ },
73
+ body: {
74
+ input: isBinary ? { files } : { urls },
75
+ code: `
76
+ const { PDF_MERGE } = require('./utils');
76
77
  input = [...input.files || [],...input.urls || []].filter(i => i);
77
78
  return PDF_MERGE(input);`,
78
- returnBinary: "true",
79
- },
80
- encoding: 'arraybuffer',
81
- json: true,
82
- };
83
- const response = await this.helpers.httpRequest(options);
84
- if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
85
- // No binary data returned; emit only JSON without a binary property
79
+ returnBinary: "true",
80
+ },
81
+ encoding: null,
82
+ json: true,
83
+ };
84
+ const response = await this.helpers.requestWithAuthentication.call(this, 'customJsApi', options);
85
+ if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
86
+ // No binary data returned; emit only JSON without a binary property
87
+ returnData.push({
88
+ json: {},
89
+ pairedItem: items.map((_, i) => ({
90
+ item: i,
91
+ })),
92
+ });
93
+ return [returnData];
94
+ }
95
+ const binaryData = await this.helpers.prepareBinaryData(response, "output.pdf");
86
96
  returnData.push({
87
97
  json: {},
98
+ binary: {
99
+ data: binaryData,
100
+ },
101
+ pairedItem: items.map((_, i) => ({
102
+ item: i,
103
+ })),
88
104
  });
89
- return [returnData];
90
105
  }
91
- const binaryData = await this.helpers.prepareBinaryData(response, "output.pdf");
92
- returnData.push({
93
- json: {},
94
- binary: {
95
- data: binaryData,
96
- },
97
- });
106
+ catch (error) {
107
+ const errorMessage = error instanceof Error ? error.message : String(error);
108
+ if (this.continueOnFail()) {
109
+ returnData.push({
110
+ json: {
111
+ error: errorMessage,
112
+ },
113
+ pairedItem: items.map((_, i) => ({
114
+ item: i,
115
+ })),
116
+ });
117
+ return [returnData];
118
+ }
119
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error);
120
+ }
98
121
  return [returnData];
99
122
  }
100
123
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PdfFormFill = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
4
5
  class PdfFormFill {
5
6
  constructor() {
6
7
  this.description = {
@@ -90,51 +91,73 @@ class PdfFormFill {
90
91
  return Buffer.from(file.data, "base64");
91
92
  };
92
93
  for (let i = 0; i < items.length; i++) {
93
- const credentials = await this.getCredentials("customJsApi");
94
- const field_name = this.getNodeParameter("field_name", i);
95
- const isBinary = this.getNodeParameter("resource", i) === "binary";
96
- const file = isBinary ? getFile(field_name, i) : "";
97
- if (!isBinary) {
98
- throw new Error(`Invalid binary data`);
99
- }
100
- const options = {
101
- url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
102
- method: 'POST',
103
- headers: {
104
- "customjs-origin": "n8n/pdfFormFill",
105
- "x-api-key": credentials.apiKey,
106
- },
107
- body: {
108
- input: {
109
- file: file,
110
- // n8n fixedCollection with multipleValues returns an object like { field: [{ name, value }, ...] }
111
- fields: ((_a = this.getNodeParameter("fields", i)) === null || _a === void 0 ? void 0 : _a.field) || []
94
+ try {
95
+ const credentials = await this.getCredentials("customJsApi");
96
+ const field_name = this.getNodeParameter("field_name", i);
97
+ const isBinary = this.getNodeParameter("resource", i) === "binary";
98
+ const file = isBinary ? getFile(field_name, i) : "";
99
+ if (!isBinary) {
100
+ throw new Error(`Invalid binary data`);
101
+ }
102
+ const options = {
103
+ url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
104
+ method: 'POST',
105
+ headers: {
106
+ "customjs-origin": "n8n/pdfFormFill",
112
107
  },
113
- code: `
114
- const { PDF_FILL_FORM } = requir` + `e('./utils');
108
+ body: {
109
+ input: {
110
+ file: file,
111
+ // n8n fixedCollection with multipleValues returns an object like { field: [{ name, value }, ...] }
112
+ fields: ((_a = this.getNodeParameter("fields", i)) === null || _a === void 0 ? void 0 : _a.field) || []
113
+ },
114
+ code: `
115
+ const { PDF_FILL_FORM } = require('./utils');
115
116
  const pdfInput = input.file;
116
117
  const fieldValues = Object.fromEntries((input.fields || []).map(x => [x.name, x.value]));
117
118
  return PDF_FILL_FORM(pdfInput, fieldValues);`,
118
- returnBinary: "true",
119
- },
120
- encoding: 'arraybuffer',
121
- json: true,
122
- };
123
- const response = await this.helpers.httpRequest(options);
124
- if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
125
- // No binary data returned; emit only JSON without a binary property
119
+ returnBinary: "true",
120
+ },
121
+ encoding: null,
122
+ json: true,
123
+ };
124
+ const response = await this.helpers.requestWithAuthentication.call(this, 'customJsApi', options);
125
+ if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
126
+ // No binary data returned; emit only JSON without a binary property
127
+ returnData.push({
128
+ json: items[i].json,
129
+ pairedItem: {
130
+ item: i,
131
+ },
132
+ });
133
+ continue;
134
+ }
135
+ const binaryData = await this.helpers.prepareBinaryData(response, "document.pdf");
126
136
  returnData.push({
127
137
  json: items[i].json,
138
+ binary: {
139
+ data: binaryData,
140
+ },
141
+ pairedItem: {
142
+ item: i,
143
+ },
128
144
  });
129
- continue;
130
145
  }
131
- const binaryData = await this.helpers.prepareBinaryData(response, "document.pdf");
132
- returnData.push({
133
- json: items[i].json,
134
- binary: {
135
- data: binaryData,
136
- },
137
- });
146
+ catch (error) {
147
+ const errorMessage = error instanceof Error ? error.message : String(error);
148
+ if (this.continueOnFail()) {
149
+ returnData.push({
150
+ json: {
151
+ error: errorMessage,
152
+ },
153
+ pairedItem: {
154
+ item: i,
155
+ },
156
+ });
157
+ continue;
158
+ }
159
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i });
160
+ }
138
161
  }
139
162
  return [returnData];
140
163
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PdfToPng = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
4
5
  class PdfToPng {
5
6
  constructor() {
6
7
  this.description = {
@@ -61,48 +62,70 @@ class PdfToPng {
61
62
  return Buffer.from(file.data, "base64");
62
63
  };
63
64
  for (let i = 0; i < items.length; i++) {
64
- const credentials = await this.getCredentials("customJsApi");
65
- const field_name = this.getNodeParameter("field_name", i);
66
- const isBinary = this.getNodeParameter("resource", i) === "binary";
67
- const file = isBinary ? getFile(field_name, i) : "";
68
- if (!isBinary &&
69
- !field_name.startsWith("http://") &&
70
- !field_name.startsWith("https://")) {
71
- throw new Error(`Invalid URL: ${field_name}`);
72
- }
73
- const options = {
74
- url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
75
- method: 'POST',
76
- headers: {
77
- "customjs-origin": "n8n/pdfToPng",
78
- "x-api-key": credentials.apiKey,
79
- },
80
- body: {
81
- input: isBinary ? { file: file } : { urls: field_name },
82
- code: `
83
- const { PDF2PNG } = requir` + `e('./utils');
65
+ try {
66
+ const credentials = await this.getCredentials("customJsApi");
67
+ const field_name = this.getNodeParameter("field_name", i);
68
+ const isBinary = this.getNodeParameter("resource", i) === "binary";
69
+ const file = isBinary ? getFile(field_name, i) : "";
70
+ if (!isBinary &&
71
+ !field_name.startsWith("http://") &&
72
+ !field_name.startsWith("https://")) {
73
+ throw new Error(`Invalid URL: ${field_name}`);
74
+ }
75
+ const options = {
76
+ url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
77
+ method: 'POST',
78
+ headers: {
79
+ "customjs-origin": "n8n/pdfToPng",
80
+ },
81
+ body: {
82
+ input: isBinary ? { file: file } : { urls: field_name },
83
+ code: `
84
+ const { PDF2PNG } = require('./utils');
84
85
  input = input.file || input.urls;
85
86
  return PDF2PNG(input);`,
86
- returnBinary: "true",
87
- },
88
- encoding: 'arraybuffer',
89
- json: true,
90
- };
91
- const response = await this.helpers.httpRequest(options);
92
- if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
93
- // No binary data returned; emit only JSON without a binary property
87
+ returnBinary: "true",
88
+ },
89
+ encoding: null,
90
+ json: true,
91
+ };
92
+ const response = await this.helpers.requestWithAuthentication.call(this, 'customJsApi', options);
93
+ if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
94
+ // No binary data returned; emit only JSON without a binary property
95
+ returnData.push({
96
+ json: items[i].json,
97
+ pairedItem: {
98
+ item: i,
99
+ },
100
+ });
101
+ continue;
102
+ }
103
+ const binaryData = await this.helpers.prepareBinaryData(response, "output.png");
94
104
  returnData.push({
95
105
  json: items[i].json,
106
+ binary: {
107
+ data: binaryData,
108
+ },
109
+ pairedItem: {
110
+ item: i,
111
+ },
96
112
  });
97
- continue;
98
113
  }
99
- const binaryData = await this.helpers.prepareBinaryData(response, "output.png");
100
- returnData.push({
101
- json: items[i].json,
102
- binary: {
103
- data: binaryData,
104
- },
105
- });
114
+ catch (error) {
115
+ const errorMessage = error instanceof Error ? error.message : String(error);
116
+ if (this.continueOnFail()) {
117
+ returnData.push({
118
+ json: {
119
+ error: errorMessage,
120
+ },
121
+ pairedItem: {
122
+ item: i,
123
+ },
124
+ });
125
+ continue;
126
+ }
127
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i });
128
+ }
106
129
  }
107
130
  return [returnData];
108
131
  }