n8n-nodes-pdfbro 0.1.11 → 0.1.13
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/PdfBro/PdfBro.node.js +27 -33
- package/package.json +1 -1
|
@@ -1,23 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.PdfBro = void 0;
|
|
7
|
-
const pdf_lib_1 = require("pdf-lib");
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
const pdf_parse_1 = __importDefault(require("pdf-parse"));
|
|
10
|
-
// @ts-ignore
|
|
11
|
-
const pdfmake_1 = __importDefault(require("pdfmake/build/pdfmake"));
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
const vfs_fonts_1 = __importDefault(require("pdfmake/build/vfs_fonts"));
|
|
14
|
-
// @ts-ignore
|
|
15
|
-
const html_to_pdfmake_1 = __importDefault(require("html-to-pdfmake"));
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
const jsdom_1 = require("jsdom");
|
|
18
|
-
// Initialize pdfmake fonts
|
|
19
|
-
// @ts-ignore
|
|
20
|
-
pdfmake_1.default.vfs = vfs_fonts_1.default.pdfMake.vfs;
|
|
21
4
|
class PdfBro {
|
|
22
5
|
constructor() {
|
|
23
6
|
this.description = {
|
|
@@ -167,6 +150,8 @@ class PdfBro {
|
|
|
167
150
|
const items = this.getInputData();
|
|
168
151
|
const returnData = [];
|
|
169
152
|
const operation = this.getNodeParameter('operation', 0);
|
|
153
|
+
// Dynamic imports to prevent load-time crashes
|
|
154
|
+
const { PDFDocument, degrees } = require('pdf-lib');
|
|
170
155
|
// --- Helper: Parse Split Range ---
|
|
171
156
|
const parseRange = (rangeStr, totalPages) => {
|
|
172
157
|
if (rangeStr === '*') {
|
|
@@ -216,7 +201,7 @@ class PdfBro {
|
|
|
216
201
|
for (let i = 0; i < items.length; i++) {
|
|
217
202
|
try {
|
|
218
203
|
if (operation === 'merge') {
|
|
219
|
-
const mergedPdf = await
|
|
204
|
+
const mergedPdf = await PDFDocument.create();
|
|
220
205
|
// Get fixed collection
|
|
221
206
|
// @ts-ignore
|
|
222
207
|
const binaries = ((_a = this.getNodeParameter('inputBinaries', i)) === null || _a === void 0 ? void 0 : _a.files) || [];
|
|
@@ -227,7 +212,7 @@ class PdfBro {
|
|
|
227
212
|
const propName = entry.binaryPropertyName;
|
|
228
213
|
if (this.helpers.assertBinaryData(i, propName)) {
|
|
229
214
|
const validBuffer = await this.helpers.getBinaryDataBuffer(i, propName);
|
|
230
|
-
const pdf = await
|
|
215
|
+
const pdf = await PDFDocument.load(validBuffer);
|
|
231
216
|
const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
|
|
232
217
|
copiedPages.forEach((page) => mergedPdf.addPage(page));
|
|
233
218
|
}
|
|
@@ -244,14 +229,14 @@ class PdfBro {
|
|
|
244
229
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
|
245
230
|
const rangeStr = this.getNodeParameter('splitRange', i);
|
|
246
231
|
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
247
|
-
const pdf = await
|
|
232
|
+
const pdf = await PDFDocument.load(validBuffer);
|
|
248
233
|
const totalPages = pdf.getPageCount();
|
|
249
234
|
// Parse logic
|
|
250
235
|
let indicesToKeep = parseRange(rangeStr, totalPages);
|
|
251
236
|
if (rangeStr === '*') {
|
|
252
237
|
// Burst mode: return separate items
|
|
253
238
|
for (const pageIndex of indicesToKeep) {
|
|
254
|
-
const newPdf = await
|
|
239
|
+
const newPdf = await PDFDocument.create();
|
|
255
240
|
const [copiedPage] = await newPdf.copyPages(pdf, [pageIndex]);
|
|
256
241
|
newPdf.addPage(copiedPage);
|
|
257
242
|
const newPdfBuffer = await newPdf.save();
|
|
@@ -265,11 +250,9 @@ class PdfBro {
|
|
|
265
250
|
}
|
|
266
251
|
else {
|
|
267
252
|
// Range mode: return single PDF with selected pages
|
|
268
|
-
|
|
269
|
-
// "1-3" usually means "Create a PDF with pages 1 to 3".
|
|
270
|
-
const newPdf = await pdf_lib_1.PDFDocument.create();
|
|
253
|
+
const newPdf = await PDFDocument.create();
|
|
271
254
|
const copiedPages = await newPdf.copyPages(pdf, indicesToKeep);
|
|
272
|
-
copiedPages.forEach(p => newPdf.addPage(p));
|
|
255
|
+
copiedPages.forEach((p) => newPdf.addPage(p));
|
|
273
256
|
const newPdfBuffer = await newPdf.save();
|
|
274
257
|
returnData.push({
|
|
275
258
|
json: { ...items[i].json, extractedPages: indicesToKeep.map(p => p + 1).join(', ') },
|
|
@@ -281,13 +264,22 @@ class PdfBro {
|
|
|
281
264
|
}
|
|
282
265
|
else if (operation === 'invoice') {
|
|
283
266
|
const htmlContent = this.getNodeParameter('htmlContent', i);
|
|
267
|
+
// Lazy load Invoice dependencies
|
|
268
|
+
const { JSDOM } = require('jsdom');
|
|
269
|
+
const htmlToPdfmake = require('html-to-pdfmake');
|
|
270
|
+
const pdfMake = require('pdfmake/build/pdfmake');
|
|
271
|
+
const pdfFonts = require('pdfmake/build/vfs_fonts');
|
|
272
|
+
// Initialize fonts if not already done
|
|
273
|
+
if (!pdfMake.vfs) {
|
|
274
|
+
pdfMake.vfs = pdfFonts.pdfMake.vfs;
|
|
275
|
+
}
|
|
284
276
|
// convert html to pdfmake format (requires window)
|
|
285
|
-
const { window } = new
|
|
286
|
-
const docDefContent = (
|
|
277
|
+
const { window } = new JSDOM('');
|
|
278
|
+
const docDefContent = htmlToPdfmake(htmlContent, { window });
|
|
287
279
|
const docDefinition = {
|
|
288
280
|
content: docDefContent,
|
|
289
281
|
};
|
|
290
|
-
const pdfDocGenerator =
|
|
282
|
+
const pdfDocGenerator = pdfMake.createPdf(docDefinition);
|
|
291
283
|
const buffer = await new Promise((resolve, reject) => {
|
|
292
284
|
pdfDocGenerator.getBuffer((buffer) => {
|
|
293
285
|
resolve(buffer);
|
|
@@ -304,11 +296,11 @@ class PdfBro {
|
|
|
304
296
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
|
305
297
|
const degreesVal = this.getNodeParameter('rotationDegrees', i);
|
|
306
298
|
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
307
|
-
const pdf = await
|
|
299
|
+
const pdf = await PDFDocument.load(validBuffer);
|
|
308
300
|
const pages = pdf.getPages();
|
|
309
|
-
pages.forEach(page => {
|
|
301
|
+
pages.forEach((page) => {
|
|
310
302
|
const currentRotation = page.getRotation().angle;
|
|
311
|
-
page.setRotation(
|
|
303
|
+
page.setRotation(degrees(currentRotation + degreesVal));
|
|
312
304
|
});
|
|
313
305
|
const rotatedPdfBuffer = await pdf.save();
|
|
314
306
|
returnData.push({
|
|
@@ -319,9 +311,11 @@ class PdfBro {
|
|
|
319
311
|
});
|
|
320
312
|
}
|
|
321
313
|
else if (operation === 'extractText') {
|
|
314
|
+
// Lazy load pdf-parse
|
|
315
|
+
const pdfParse = require('pdf-parse');
|
|
322
316
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
|
323
317
|
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
324
|
-
const data = await (
|
|
318
|
+
const data = await pdfParse(validBuffer);
|
|
325
319
|
returnData.push({
|
|
326
320
|
json: {
|
|
327
321
|
...items[i].json,
|
|
@@ -335,7 +329,7 @@ class PdfBro {
|
|
|
335
329
|
else if (operation === 'metadata') {
|
|
336
330
|
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
|
|
337
331
|
const validBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
|
338
|
-
const pdf = await
|
|
332
|
+
const pdf = await PDFDocument.load(validBuffer);
|
|
339
333
|
returnData.push({
|
|
340
334
|
json: {
|
|
341
335
|
...items[i].json,
|