@aaqu/node-red-aaqu-pdf 0.4.3 → 0.4.5
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/nodes/pdf-add-page.html
CHANGED
package/nodes/pdf-create.html
CHANGED
package/nodes/pdf-draw-text.html
CHANGED
package/nodes/pdf-get-page.html
CHANGED
package/nodes/pdf-get-page.js
CHANGED
|
@@ -5,35 +5,82 @@ module.exports = function (RED) {
|
|
|
5
5
|
RED.nodes.createNode(this, config);
|
|
6
6
|
|
|
7
7
|
this.pdfPage = config.pdfPage;
|
|
8
|
-
|
|
9
8
|
const node = this;
|
|
10
9
|
|
|
11
10
|
node.on("input", async function (msg) {
|
|
12
11
|
try {
|
|
12
|
+
node.status({ fill: "blue", shape: "dot", text: "Processing PDF..." });
|
|
13
|
+
|
|
14
|
+
// Validate payload
|
|
15
|
+
if (!msg || typeof msg !== "object") {
|
|
16
|
+
throw new Error("Incoming msg is missing or not an object.");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!msg.payload) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"msg.payload is required and must contain a PDF file.",
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Payload must be a Buffer, Uint8Array or ArrayBuffer
|
|
26
|
+
const validTypes =
|
|
27
|
+
Buffer.isBuffer(msg.payload) ||
|
|
28
|
+
msg.payload instanceof Uint8Array ||
|
|
29
|
+
msg.payload instanceof ArrayBuffer;
|
|
30
|
+
|
|
31
|
+
if (!validTypes) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
"msg.payload must be a Buffer, Uint8Array or ArrayBuffer containing a PDF.",
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Load PDF
|
|
13
38
|
const pdfDoc = await PDFDocument.load(msg.payload);
|
|
14
|
-
|
|
15
|
-
// console.log(pages);
|
|
39
|
+
const totalPages = pdfDoc.getPageCount();
|
|
16
40
|
|
|
17
|
-
|
|
41
|
+
// Determine page number
|
|
42
|
+
const rawPage = msg.pdfPage ?? node.pdfPage;
|
|
43
|
+
const pageNum = Number(rawPage);
|
|
18
44
|
|
|
19
|
-
|
|
45
|
+
if (!Number.isInteger(pageNum)) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Invalid page number: '${rawPage}'. Must be an integer.`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (pageNum < 1 || pageNum > totalPages) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`Page number out of range: ${pageNum}. Valid range is 1–${totalPages}.`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const pageIndex = pageNum - 1;
|
|
58
|
+
|
|
59
|
+
// Create output PDF with selected page only
|
|
60
|
+
const newPDF = await PDFDocument.create();
|
|
61
|
+
const copiedPages = await newPDF.copyPages(pdfDoc, [pageIndex]);
|
|
20
62
|
const [page] = copiedPages;
|
|
21
63
|
|
|
22
64
|
newPDF.addPage(page);
|
|
23
65
|
const pdfBytes = await newPDF.save();
|
|
24
66
|
|
|
67
|
+
// Return result
|
|
25
68
|
msg.payload = Buffer.from(pdfBytes);
|
|
69
|
+
|
|
70
|
+
node.status({ fill: "green", shape: "dot", text: "Page extracted" });
|
|
26
71
|
node.send(msg);
|
|
27
72
|
} catch (err) {
|
|
28
|
-
|
|
73
|
+
node.status({
|
|
29
74
|
fill: "red",
|
|
30
|
-
shape: "
|
|
31
|
-
text: "Error
|
|
75
|
+
shape: "dot",
|
|
76
|
+
text: "Error extracting page",
|
|
32
77
|
});
|
|
33
78
|
|
|
34
|
-
|
|
79
|
+
// Log error with msg context
|
|
80
|
+
node.error(err.message || err, msg);
|
|
35
81
|
}
|
|
36
82
|
});
|
|
37
83
|
}
|
|
84
|
+
|
|
38
85
|
RED.nodes.registerType("pdf-get-page", PDFGetPageNode);
|
|
39
86
|
};
|
package/nodes/pdf-save.html
CHANGED
package/nodes/pdf-to-img.html
CHANGED