From 79eb0df9ee6839567b60c62fda19a902a89082eb Mon Sep 17 00:00:00 2001 From: Shailendra1703 Date: Thu, 11 Sep 2025 23:25:28 +0530 Subject: [PATCH] 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 434c8bb61..fde98f14b 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 000000000..193d421b7 --- /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;