From eb55006f155cf118aa4cb1e23b05534a3f61f0e3 Mon Sep 17 00:00:00 2001 From: Sami Mokaddem Date: Tue, 15 Oct 2024 11:58:42 +0200 Subject: [PATCH] new: [expansion:convert_markdown_to_pdf] Added module to render a markdown (under GFM) into PDF --- .../expansion/convert_markdown_to_pdf.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 misp_modules/modules/expansion/convert_markdown_to_pdf.py diff --git a/misp_modules/modules/expansion/convert_markdown_to_pdf.py b/misp_modules/modules/expansion/convert_markdown_to_pdf.py new file mode 100755 index 00000000..8eac5b68 --- /dev/null +++ b/misp_modules/modules/expansion/convert_markdown_to_pdf.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python\ + +import json +import base64 +import pandoc + +misperrors = {'error': 'Error'} +mispattributes = {'input': ['text'], 'output': ['text']} +moduleinfo = { + 'version': '0.1', + 'author': 'Sami Mokaddem', + 'description': 'Render the markdown (under GFM) into PDF. Requires pandoc (https://pandoc.org/) and wkhtmltopdf (https://wkhtmltopdf.org/).', + 'module-type': ['expansion'], + 'name': 'Markdown to PDF converter', + 'logo': '', + 'requirements': ['pandoc'], + 'features': '', + 'references': [], + 'input': '', + 'output': '', +} + + +def convert(markdown): + doc = pandoc.read(markdown, format='gfm') + margin = '3' + options = [ + '--pdf-engine=wkhtmltopdf', + f'-V margin-left={margin}', + f'-V margin-right={margin}', + f'-V margin-top={margin}', + f'-V margin-bottom={margin}', + ] + converted = pandoc.write(doc, format='pdf', options=options) + return base64.b64encode(converted).decode() + +def handler(q=False): + if q is False: + return False + request = json.loads(q) + if request.get('text'): + data = request['text'] + else: + return False + data = json.loads(data) + markdown = data.get('markdown') + try: + rendered = convert(markdown) + except Exception as e: + rendered = f'Error: {e}' + + r = {'results': [{'types': mispattributes['output'], + 'values':[rendered]}]} + return r + + +def introspection(): + return mispattributes + + +def version(): + return moduleinfo