@custom-js/n8n-nodes-pdf-toolkit 1.49.0 → 1.51.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.Html2Pdf = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
4
5
  class Html2Pdf {
5
6
  constructor() {
6
7
  this.description = {
@@ -33,6 +34,14 @@ class Html2Pdf {
33
34
  description: "The HTML content to convert to PDF",
34
35
  required: true,
35
36
  },
37
+ {
38
+ displayName: "Output Filename",
39
+ name: "outputFilename",
40
+ type: "string",
41
+ default: "output.pdf",
42
+ description: "Name for the generated PDF file (include .pdf extension)",
43
+ required: false,
44
+ },
36
45
  ],
37
46
  };
38
47
  }
@@ -40,38 +49,61 @@ class Html2Pdf {
40
49
  const items = this.getInputData();
41
50
  const returnData = [];
42
51
  for (let i = 0; i < items.length; i++) {
43
- const credentials = await this.getCredentials("customJsApi");
44
- const htmlInput = this.getNodeParameter("htmlInput", i);
45
- const options = {
46
- url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
47
- method: 'POST',
48
- headers: {
49
- "customjs-origin": "n8n/generatePDF",
50
- "x-api-key": credentials.apiKey,
51
- },
52
- body: {
53
- input: htmlInput,
54
- code: "const { HTML2PDF } = require('./utils'); return HTML2PDF(input)",
55
- returnBinary: "true",
56
- },
57
- encoding: 'arraybuffer',
58
- json: true,
59
- };
60
- const response = await this.helpers.httpRequest(options);
61
- if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
62
- // No binary data returned; emit only JSON without a binary property
52
+ try {
53
+ const credentials = await this.getCredentials("customJsApi");
54
+ const htmlInput = this.getNodeParameter("htmlInput", i);
55
+ const options = {
56
+ url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
57
+ method: 'POST',
58
+ headers: {
59
+ "customjs-origin": "n8n/generatePDF",
60
+ },
61
+ body: {
62
+ input: htmlInput,
63
+ code: "const { HTML2PDF } = require('./utils'); return HTML2PDF(input)",
64
+ returnBinary: "true",
65
+ },
66
+ encoding: null,
67
+ json: true,
68
+ };
69
+ const response = await this.helpers.requestWithAuthentication.call(this, 'customJsApi', options);
70
+ if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
71
+ // No binary data returned; emit only JSON without a binary property
72
+ returnData.push({
73
+ json: items[i].json,
74
+ pairedItem: {
75
+ item: i,
76
+ },
77
+ });
78
+ continue;
79
+ }
80
+ const outputFilename = this.getNodeParameter("outputFilename", i, "output.pdf");
81
+ const binaryData = await this.helpers.prepareBinaryData(response, outputFilename);
63
82
  returnData.push({
64
83
  json: items[i].json,
84
+ binary: {
85
+ data: binaryData,
86
+ },
87
+ pairedItem: {
88
+ item: i,
89
+ },
65
90
  });
66
- continue;
67
91
  }
68
- const binaryData = await this.helpers.prepareBinaryData(response, "output.pdf");
69
- returnData.push({
70
- json: items[i].json,
71
- binary: {
72
- data: binaryData,
73
- },
74
- });
92
+ catch (error) {
93
+ const errorMessage = error instanceof Error ? error.message : String(error);
94
+ if (this.continueOnFail()) {
95
+ returnData.push({
96
+ json: {
97
+ error: errorMessage,
98
+ },
99
+ pairedItem: {
100
+ item: i,
101
+ },
102
+ });
103
+ continue;
104
+ }
105
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i });
106
+ }
75
107
  }
76
108
  return [returnData];
77
109
  }
@@ -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 = {
@@ -180,6 +181,14 @@ class InvoiceGenerator {
180
181
  },
181
182
  description: 'A JSON array of invoice items. E.g., [{"description":"Item 1","quantity":2,"unitPrice":50}]',
182
183
  },
184
+ {
185
+ displayName: 'Output Filename',
186
+ name: 'outputFilename',
187
+ type: 'string',
188
+ default: 'Invoice.pdf',
189
+ description: 'Name for the generated PDF file (include .pdf extension)',
190
+ required: false,
191
+ },
183
192
  ],
184
193
  };
185
194
  }
@@ -187,53 +196,54 @@ class InvoiceGenerator {
187
196
  const items = this.getInputData();
188
197
  const returnData = [];
189
198
  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}`);
199
+ try { // Added try block
200
+ const credentials = await this.getCredentials('customJsApi');
201
+ const pdfTemplate = this.getNodeParameter('pdfTemplate', i);
202
+ const issuer = this.getNodeParameter('issuer.issuerValues', i);
203
+ const payment = this.getNodeParameter('payment.paymentValues', i);
204
+ const recipient = this.getNodeParameter('recipient.recipientValues', i);
205
+ const billing = this.getNodeParameter('billing.billingValues', i);
206
+ const itemsMode = this.getNodeParameter('itemsMode', i);
207
+ let invoiceItems;
208
+ if (itemsMode === 'json') {
209
+ const itemsJson = this.getNodeParameter('itemsJson', i);
210
+ if (typeof itemsJson === 'string') {
211
+ try {
212
+ invoiceItems = JSON.parse(itemsJson);
213
+ }
214
+ catch (error) {
215
+ if (error instanceof Error) {
216
+ throw new Error(`Invalid JSON in 'Items JSON' field: ${error.message}`);
217
+ }
218
+ throw new Error(`Invalid JSON in 'Items JSON' field: ${String(error)}`);
207
219
  }
208
- throw new Error(`Invalid JSON in 'Items JSON' field: ${String(error)}`);
209
220
  }
210
- }
211
- else if (Array.isArray(itemsJson)) {
212
- invoiceItems = itemsJson;
221
+ else if (Array.isArray(itemsJson)) {
222
+ invoiceItems = itemsJson;
223
+ }
224
+ else {
225
+ invoiceItems = [];
226
+ }
213
227
  }
214
228
  else {
215
- invoiceItems = [];
229
+ const itemsData = this.getNodeParameter('items', i);
230
+ invoiceItems = itemsData.itemsValues;
216
231
  }
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),
232
+ invoiceItems = invoiceItems.map(item => {
233
+ return {
234
+ description: item.description,
235
+ quantity: Number(item.quantity),
236
+ unitPrice: Number(item.unitPrice),
237
+ };
238
+ });
239
+ const invoiceData = {
240
+ issuer,
241
+ payment,
242
+ recipient,
243
+ billing,
244
+ items: invoiceItems,
227
245
  };
228
- });
229
- const invoiceData = {
230
- issuer,
231
- payment,
232
- recipient,
233
- billing,
234
- items: invoiceItems,
235
- };
236
- const code = `
246
+ const code = `
237
247
  const { HTML2PDF } = require('./utils');
238
248
  const nunjucks = require('nunjucks');
239
249
  const fetch = require('node-fetch');
@@ -245,38 +255,60 @@ class InvoiceGenerator {
245
255
 
246
256
  }
247
257
  );
248
- console.log('renderedHtml', renderedHtml);
258
+
249
259
  return HTML2PDF(renderedHtml);
250
260
  `;
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)) {
261
+ const options = {
262
+ url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
263
+ method: 'POST',
264
+ headers: {
265
+ 'customjs-origin': 'n8n/invoice-generator',
266
+ },
267
+ body: {
268
+ input: invoiceData,
269
+ code: code,
270
+ returnBinary: 'true',
271
+ },
272
+ encoding: null,
273
+ json: true,
274
+ };
275
+ const response = await this.helpers.requestWithAuthentication.call(this, 'customJsApi', options);
276
+ if (!response || (Buffer.isBuffer(response) && response.length === 0)) {
277
+ returnData.push({
278
+ json: items[i].json,
279
+ pairedItem: {
280
+ item: i,
281
+ },
282
+ });
283
+ continue;
284
+ }
285
+ const outputFilename = this.getNodeParameter('outputFilename', i, 'Invoice.pdf');
286
+ const binaryData = await this.helpers.prepareBinaryData(response, outputFilename);
268
287
  returnData.push({
269
288
  json: items[i].json,
289
+ binary: {
290
+ data: binaryData,
291
+ },
292
+ pairedItem: {
293
+ item: i,
294
+ },
270
295
  });
271
- continue;
272
296
  }
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
- });
297
+ catch (error) { // Added catch block
298
+ const errorMessage = error instanceof Error ? error.message : String(error);
299
+ if (this.continueOnFail()) {
300
+ returnData.push({
301
+ json: {
302
+ error: errorMessage,
303
+ },
304
+ pairedItem: {
305
+ item: i,
306
+ },
307
+ });
308
+ continue;
309
+ }
310
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i });
311
+ }
280
312
  }
281
313
  return [returnData];
282
314
  }
@@ -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 } = require('./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 = {
@@ -46,55 +47,86 @@ class MergePdfs {
46
47
  description: "The field names for binary PDF file or urls that indicate PDF files. Please make sure the size of PDf file doesn't exceed 6mb. If it's bigger, pass an array of URLs rather than binary file.",
47
48
  required: true,
48
49
  },
50
+ {
51
+ displayName: "Output Filename",
52
+ name: "outputFilename",
53
+ type: "string",
54
+ default: "output.pdf",
55
+ description: "Name for the generated PDF file (include .pdf extension)",
56
+ required: false,
57
+ },
49
58
  ],
50
59
  };
51
60
  }
52
61
  async execute() {
53
62
  const items = this.getInputData();
54
63
  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: `
64
+ try {
65
+ const credentials = await this.getCredentials("customJsApi");
66
+ const isBinary = this.getNodeParameter("resource", 0) === "binary";
67
+ const field_name = this.getNodeParameter("field_name", 0);
68
+ const files = isBinary ? items.map((item, i) => {
69
+ var _a;
70
+ if ((_a = item.binary) === null || _a === void 0 ? void 0 : _a.data) {
71
+ return Buffer.from(item.binary.data.data, "base64");
72
+ }
73
+ }) : [];
74
+ const urls = !isBinary ? field_name : [];
75
+ const options = {
76
+ url: `https://e.customjs.io/__js1-${credentials.apiKey}`,
77
+ method: 'POST',
78
+ headers: {
79
+ "customjs-origin": "n8n/mergePDFs",
80
+ },
81
+ body: {
82
+ input: isBinary ? { files } : { urls },
83
+ code: `
75
84
  const { PDF_MERGE } = require('./utils');
76
85
  input = [...input.files || [],...input.urls || []].filter(i => i);
77
86
  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
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: {},
97
+ pairedItem: items.map((_, i) => ({
98
+ item: i,
99
+ })),
100
+ });
101
+ return [returnData];
102
+ }
103
+ const outputFilename = this.getNodeParameter("outputFilename", 0, "output.pdf");
104
+ const binaryData = await this.helpers.prepareBinaryData(response, outputFilename);
86
105
  returnData.push({
87
106
  json: {},
107
+ binary: {
108
+ data: binaryData,
109
+ },
110
+ pairedItem: items.map((_, i) => ({
111
+ item: i,
112
+ })),
88
113
  });
89
- return [returnData];
90
114
  }
91
- const binaryData = await this.helpers.prepareBinaryData(response, "output.pdf");
92
- returnData.push({
93
- json: {},
94
- binary: {
95
- data: binaryData,
96
- },
97
- });
115
+ catch (error) {
116
+ const errorMessage = error instanceof Error ? error.message : String(error);
117
+ if (this.continueOnFail()) {
118
+ returnData.push({
119
+ json: {
120
+ error: errorMessage,
121
+ },
122
+ pairedItem: items.map((_, i) => ({
123
+ item: i,
124
+ })),
125
+ });
126
+ return [returnData];
127
+ }
128
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error);
129
+ }
98
130
  return [returnData];
99
131
  }
100
132
  }