Skip to content

Commit

Permalink
Conversion script and command line call
Browse files Browse the repository at this point in the history
  • Loading branch information
kadowa committed Nov 6, 2015
0 parents commit 8e984c5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

./xml2pdf.py --xml ../files/test.xml --xsl ../stylesheets/jats-html.xsl -o ../files/testpy.pdf --css ../stylesheets/jats-preview.css
45 changes: 45 additions & 0 deletions xml2pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/python

import sys, getopt, logging
from weasyprint import HTML, CSS
from lxml import etree

def usage():
sys.stderr.write("./xml2pdf.py --xml <xml-file> --xsl <xsl-file> -o <pdf-output> [--css <stylesheet>]")

options, rest = getopt.getopt(sys.argv[1:], "o:", ["xml=", "xsl=", "output=","css="])

out_fn = None
xml_fn = None
xsl_fn = None
css_fn = None

for opt, arg in options:
if opt in ("-o", "--output"):
out_fn = arg
elif opt == "--xml":
xml_fn = arg
elif opt == "--xsl":
xsl_fn = arg
elif opt == "--css":
css_fn = arg
else:
sys.stderr.write("Unknown option %s"%opt)

if not (out_fn and xml_fn and xsl_fn):
usage()

xsl = etree.parse(xsl_fn)
transformer = etree.XSLT(xsl)

xml = etree.parse(xml_fn)

logging.info
html = transformer(xml)

if css_fn:
css = [CSS(css_fn)]
else:
css = None

HTML(tree=html).write_pdf(out_fn, css)

0 comments on commit 8e984c5

Please sign in to comment.