summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2019-11-07 23:21:46 +0100
committeremkael <emkael@tlen.pl>2019-11-07 23:21:46 +0100
commit78786c9c6ed9df39f1e34b3ad2735f9656adc7cf (patch)
treef17df0f2823139958fd5be316827a03359e656f5
parentbf6cf6276a5cb6a9b2d8c5f88017899a52edacbd (diff)
PDF output format
-rw-r--r--deal-converter.py8
-rw-r--r--dealconvert/formats/html.py2
-rw-r--r--dealconvert/formats/pdf.py40
-rw-r--r--requirements.txt1
4 files changed, 48 insertions, 3 deletions
diff --git a/deal-converter.py b/deal-converter.py
index 43345cb..424ee6a 100644
--- a/deal-converter.py
+++ b/deal-converter.py
@@ -16,7 +16,10 @@ parser = argparse.ArgumentParser(
parser.add_argument('--jfr', action='store_true',
help='For PBN file, write only JFR DD fields')
parser.add_argument('--columns', type=int, default=None,
- help='For HTML files, print-out column count')
+ help='For HTML and PDF files, print-out column count')
+parser.add_argument('--orientation', type=str,
+ choices=['Landscape', 'Portrait'], default=None,
+ help='For PDF files, page orientation')
parser.add_argument('input', metavar='INPUT_FILE',
help='Input file path')
parser.add_argument('output', metavar='OUTPUT_FILE', nargs='*',
@@ -31,7 +34,8 @@ warnings.showwarning = _warning
try:
converter = DealConverter(
arguments.input,
- jfr_only=arguments.jfr, columns=arguments.columns)
+ jfr_only=arguments.jfr,
+ columns=arguments.columns, orientation=arguments.orientation)
converter.output(arguments.output)
except RuntimeError as e:
print('ERROR: %s' % (str(e)), file=sys.stderr)
diff --git a/dealconvert/formats/html.py b/dealconvert/formats/html.py
index 244b6f2..0b5b403 100644
--- a/dealconvert/formats/html.py
+++ b/dealconvert/formats/html.py
@@ -188,7 +188,7 @@ class HTMLFormat(DealFormat):
for row in deal_rows:
table_content += '<table style="margin-top: -1px"><tr>'
for deal in row:
- table_content += '<td style="width: 4.9cm; border: solid 1px black">'
+ table_content += '<td style="width: 6.4cm; border: solid 1px black; padding: 0">'
deal_cells = [
['', '', '', ''],
['', '', '', ''],
diff --git a/dealconvert/formats/pdf.py b/dealconvert/formats/pdf.py
new file mode 100644
index 0000000..9a4f6ca
--- /dev/null
+++ b/dealconvert/formats/pdf.py
@@ -0,0 +1,40 @@
+import tempfile
+import warnings
+
+import pdfkit
+
+from . import DealFormat
+from .html import HTMLFormat
+from .. import dto
+
+
+class PDFFormat(DealFormat):
+ @property
+ def suffix(self):
+ return '.pdf'
+
+ def __init__(self, *args, **kwargs):
+ DealFormat.__init__(self, *args, **kwargs)
+ self.html_formatter = HTMLFormat(*args, **kwargs)
+
+ def parse_content(self, content):
+ raise NotImplementedError
+
+ def output_content(self, out_file, dealset):
+ html_content = self.html_formatter.get_html_content(dealset)
+ temp_file = tempfile.NamedTemporaryFile(delete=True)
+ pdfkit.from_string(html_content, temp_file.name, options={
+ 'quiet': '',
+ 'margin-bottom': '0',
+ 'margin-top': '0.35cm',
+ 'margin-left': '0',
+ 'margin-right': '0',
+ 'print-media-type': '',
+ 'page-size': 'A4',
+ 'header-left': ' ' + dealset[0].event,
+ 'header-right': 'Page [page] ',
+ 'header-font-size': '8',
+ 'orientation': self.options.get('orientation', 'Landscape') or 'Landscape'
+ })
+ out_file.write(temp_file.read())
+ temp_file.close()
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..6d68c60
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1 @@
+pdfkit == 0.6.1