From 79eb0df9ee6839567b60c62fda19a902a89082eb Mon Sep 17 00:00:00 2001 From: Shailendra1703 Date: Thu, 11 Sep 2025 23:25:28 +0530 Subject: [PATCH 1/4] Added RenderPDF functionality --- src/core/config/Categories.json | 4 +- src/core/operations/RenderPDF.mjs | 94 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/RenderPDF.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 434c8bb619..fde98f14b3 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -536,7 +536,9 @@ "Hex Density chart", "Scatter chart", "Series chart", - "Heatmap chart" + "Heatmap chart", + "Render PDF" + ] }, { diff --git a/src/core/operations/RenderPDF.mjs b/src/core/operations/RenderPDF.mjs new file mode 100644 index 0000000000..193d421b7e --- /dev/null +++ b/src/core/operations/RenderPDF.mjs @@ -0,0 +1,94 @@ +import { fromBase64, toBase64 } from "../lib/Base64.mjs"; +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; +import Utils from "../Utils.mjs"; + +/** + * Render PDF operation + */ +class RenderPDF extends Operation { + + /** + * RenderPDF constructor + */ + constructor() { + super(); + + this.name = "Render PDF"; + this.module = "File"; + this.description = "Displays the input as a PDF preview. Supports Raw and Base64 input formats."; + this.inputType = "string"; + this.outputType = "byteArray"; + this.presentType = "html"; + this.args = [ + { + "name": "Input format", + "type": "option", + "value": ["Base64","Raw"], + } + ]; + this.checks = [ + { + pattern: "^%PDF-", + flags: "", + args: ["Raw"], + useful: true, + output: { + mime: "application/pdf" + } + } + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {byteArray} + */ + run(input, args) { + const inputFormat = args[0]; + + if (!input.length) return []; + + // Convert input to raw bytes + switch (inputFormat) { + case "Base64": + input = fromBase64(input, undefined, "byteArray"); + break; + case "Raw": + default: + input = Utils.strToByteArray(input); + break; + } + + // Check PDF signature + if ( + input[0] !== 0x25 || // % + input[1] !== 0x50 || // P + input[2] !== 0x44 || // D + input[3] !== 0x46 // F + ) { + throw new OperationError("Input does not appear to be a PDF file."); + } + + return input; + } + + /** + * Displays the PDF using HTML for web apps. + * + * @param {byteArray} data + * @returns {html} + */ + async present(data) { + if (!data.length) return ""; + + const base64 = toBase64(data); + const dataURI = "data:application/pdf;base64," + base64; + + return ``; + } + +} + +export default RenderPDF; From 6967ffe4f7257a4e3b63128d748e0baccde99f47 Mon Sep 17 00:00:00 2001 From: Shailendra1703 Date: Thu, 11 Sep 2025 23:50:17 +0530 Subject: [PATCH 2/4] Fixed Linting issues --- src/core/config/Categories.json | 1 - src/core/operations/RenderPDF.mjs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index fde98f14b3..419eb7ea8a 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -538,7 +538,6 @@ "Series chart", "Heatmap chart", "Render PDF" - ] }, { diff --git a/src/core/operations/RenderPDF.mjs b/src/core/operations/RenderPDF.mjs index 193d421b7e..328bfca37f 100644 --- a/src/core/operations/RenderPDF.mjs +++ b/src/core/operations/RenderPDF.mjs @@ -24,7 +24,7 @@ class RenderPDF extends Operation { { "name": "Input format", "type": "option", - "value": ["Base64","Raw"], + "value": ["Base64", "Raw"], } ]; this.checks = [ From 6698519193a8690f2b018e63cdd7092ee0fdd3f5 Mon Sep 17 00:00:00 2001 From: Shailendra1703 Date: Fri, 12 Sep 2025 00:00:15 +0530 Subject: [PATCH 3/4] test: update help API expected results to include RenderPDF operation --- tests/node/tests/nodeApi.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/node/tests/nodeApi.mjs b/tests/node/tests/nodeApi.mjs index 29a47ffc8d..7aca720bde 100644 --- a/tests/node/tests/nodeApi.mjs +++ b/tests/node/tests/nodeApi.mjs @@ -136,7 +136,7 @@ TestRegister.addApiTests([ it("chef.help: returns multiple results", () => { const result = chef.help("base 64"); - assert.strictEqual(result.length, 13); + assert.strictEqual(result.length, 14); }), it("chef.help: looks in description for matches too", () => { From 4e3e449123478608749b1408d369d0075954c3ed Mon Sep 17 00:00:00 2001 From: Shailendra1703 Date: Mon, 15 Sep 2025 13:54:01 +0530 Subject: [PATCH 4/4] added author details --- src/core/operations/RenderPDF.mjs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/operations/RenderPDF.mjs b/src/core/operations/RenderPDF.mjs index 328bfca37f..c1b6cfb5ee 100644 --- a/src/core/operations/RenderPDF.mjs +++ b/src/core/operations/RenderPDF.mjs @@ -1,3 +1,9 @@ +/** + * @author Shailendra [singhshailendra.in] + * @copyright Crown Copyright 2017 + * @license Apache-2.0 + */ + import { fromBase64, toBase64 } from "../lib/Base64.mjs"; import Operation from "../Operation.mjs"; import OperationError from "../errors/OperationError.mjs";