@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.
@@ -17,7 +17,7 @@
17
17
  },
18
18
  inputs: 1,
19
19
  outputs: 1,
20
- icon: "file.svg",
20
+ icon: "font-awesome/fa-file-pdf-o",
21
21
  paletteLabel: "pdf add page",
22
22
  label: function () {
23
23
  return this.name || "pdf add page";
@@ -7,7 +7,7 @@
7
7
  },
8
8
  inputs: 1,
9
9
  outputs: 1,
10
- icon: "file.svg",
10
+ icon: "font-awesome/fa-file-pdf-o",
11
11
  paletteLabel: "pdf create",
12
12
  label: function () {
13
13
  return this.name || "pdf create";
@@ -29,7 +29,7 @@
29
29
  },
30
30
  inputs: 1,
31
31
  outputs: 1,
32
- icon: "file.svg",
32
+ icon: "font-awesome/fa-file-pdf-o",
33
33
  paletteLabel: "pdf draw text",
34
34
  label: function () {
35
35
  return this.name || "pdf draw text";
@@ -8,7 +8,7 @@
8
8
  },
9
9
  inputs: 1,
10
10
  outputs: 1,
11
- icon: "file.svg",
11
+ icon: "font-awesome/fa-file-pdf-o",
12
12
  paletteLabel: "pdf get page",
13
13
  label: function () {
14
14
  return this.name || "pdf get page";
@@ -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
- //const pages = pdfDoc.getPageCount();
15
- // console.log(pages);
39
+ const totalPages = pdfDoc.getPageCount();
16
40
 
17
- const newPDF = await PDFDocument.create();
41
+ // Determine page number
42
+ const rawPage = msg.pdfPage ?? node.pdfPage;
43
+ const pageNum = Number(rawPage);
18
44
 
19
- const copiedPages = await newPDF.copyPages(pdfDoc, [node.pdfPage - 1]);
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
- this.status({
73
+ node.status({
29
74
  fill: "red",
30
- shape: "ring",
31
- text: "Error parsing PDF",
75
+ shape: "dot",
76
+ text: "Error extracting page",
32
77
  });
33
78
 
34
- node.error(err);
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
  };
@@ -7,7 +7,7 @@
7
7
  },
8
8
  inputs: 1,
9
9
  outputs: 1,
10
- icon: "file.svg",
10
+ icon: "font-awesome/fa-file-pdf-o",
11
11
  paletteLabel: "pdf save",
12
12
  label: function () {
13
13
  return this.name || "pdf save";
@@ -8,7 +8,7 @@
8
8
  },
9
9
  inputs: 1,
10
10
  outputs: 1,
11
- icon: "file.svg",
11
+ icon: "font-awesome/fa-file-pdf-o",
12
12
  paletteLabel: "pdf to img",
13
13
  label: function () {
14
14
  return this.name || "pdf to img";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aaqu/node-red-aaqu-pdf",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "description": "node-red pdf creator",
5
5
  "scripts": {
6
6
  "node-red": "node-red"