Skip to content

Instantly share code, notes, and snippets.

@dhondta

dhondta/README.md

Last active Jan 23, 2020
Embed
What would you like to do?
Tinyscript tool to generate PDF's from reports in a STIX package

STIX report to PDF

This can be installed using:

$ pip install tinyscript
$ wget https://gist.githubusercontent.com/dhondta/ca5fb748957b1ec6f13418ac41c94d5b/raw/stix-reports-to-pdf.py && chmod +x stix-reports-to-pdf.py && sudo mv stix-reports-to-pdf.py /usr/bin/stix-reports-to-pdf
bs4
pdfkit
slugify
tinyscript
#!/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.1"
__copyright__ = "A. D'Hondt"
__license__ = "agpl-3.0"
__examples__ = ["stix_report_entities.xml", "stix_report_entities.xml.zip"]
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