Skip to content

Commit

Permalink
new: [expansion:convert_markdown_to_pdf] Added module to render a mar…
Browse files Browse the repository at this point in the history
…kdown (under GFM) into PDF
  • Loading branch information
mokaddem committed Oct 15, 2024
1 parent 4ea30ae commit eb55006
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions misp_modules/modules/expansion/convert_markdown_to_pdf.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit eb55006

Please sign in to comment.