Skip to content

Instantly share code, notes, and snippets.

@dhondta
Last active July 31, 2023 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhondta/ca5fb748957b1ec6f13418ac41c94d5b to your computer and use it in GitHub Desktop.
Save dhondta/ca5fb748957b1ec6f13418ac41c94d5b to your computer and use it in GitHub Desktop.
Tinyscript tool to generate PDF's from reports in a STIX package

STIX report to PDF

This Tinyscript-based tool allows to decompres a STIX XML file and to output it as a PDF using pdfkit.

This can be installed using:

$ pip install bs4 pdfkit tinyscript
$ tsm install stix-reports-to-pdf
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import pdfkit
import zipfile
from bs4 import BeautifulSoup
from tinyscript import *
try:
from HTMLParser import HTMLParser
except ImportError:
from html.parser import HTMLParser
__author__ = "Alexandre D'Hondt"
__version__ = "1.2"
__copyright__ = "A. D'Hondt"
__license__ = "agpl-3.0"
__examples__ = ["stix_report_entities.xml", "stix_report_entities.xml.zip"]
__doc__ = "This tool allows to decompres a STIX XML file and to output it as a PDF using pdfkit."
html = HTMLParser()
XML = "stix_report_entities.xml"
def generate_report(stix):
with open(stix) as f:
soup = BeautifulSoup(f.read(), 'xml')
for report in soup.find("stix:Reports").find_all("stix:Report"):
fn = "{}.pdf".format(ts.slugify(report.find("report:Title").text))
content = html.unescape(report.find("report:Description").text)
pdfkit.from_string(content, fn, {
'page-size': "A4",
'margin-top': "0.5in",
'margin-right': "0.5in",
'margin-bottom': "0.75in",
'margin-left': "0.5in",
'footer-center': "[page]/[toPage]",
})
if __name__ == '__main__':
parser.add_argument("stix", default=XML, type=ts.file_exists,
help="STIX file (XML or ZIP format)")
initialize()
try:
with zipfile.ZipFile(args.stix, 'r') as z:
z.extract(XML)
args.stix = XML
except:
pass
generate_report(args.stix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment