2017-03-27 09:17:49 +01:00
|
|
|
import pdfkit
|
2017-04-05 14:44:22 +01:00
|
|
|
from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR
|
2017-03-28 21:59:11 +01:00
|
|
|
from md_pdf.build.cover import OUTPUT_COVER_FILE
|
|
|
|
import os
|
2017-04-01 16:12:03 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__file__)
|
2017-03-27 09:17:49 +01:00
|
|
|
|
2017-03-28 21:59:11 +01:00
|
|
|
|
2017-05-03 15:33:42 +01:00
|
|
|
DEFAULT_MARGIN_VERTICAL = '1.5cm'
|
|
|
|
DEFAULT_MARGIN_HORIZONTAL = '2.5cm'
|
|
|
|
|
2017-04-05 14:44:22 +01:00
|
|
|
STYLE_FILE = os.path.join(STATIC_DIR, 'style.css')
|
|
|
|
HEADER_FILE = os.path.join(TEMPLATES_DIR, 'header.html')
|
|
|
|
FOOTER_FILE = os.path.join(TEMPLATES_DIR, 'footer.html')
|
2017-03-28 21:59:11 +01:00
|
|
|
PDF_OPTIONS = {
|
2017-03-27 09:17:49 +01:00
|
|
|
"quiet": "",
|
|
|
|
"no-pdf-compression": "",
|
2017-05-10 21:42:03 +01:00
|
|
|
"enable-internal-links": "",
|
2017-03-27 09:17:49 +01:00
|
|
|
|
2017-03-28 21:59:11 +01:00
|
|
|
"header-html": HEADER_FILE,
|
|
|
|
"footer-html": FOOTER_FILE,
|
2017-03-27 09:17:49 +01:00
|
|
|
"footer-spacing": 5,
|
|
|
|
"header-spacing": 5,
|
2017-05-03 13:30:38 +01:00
|
|
|
|
|
|
|
"user-style-sheet": STYLE_FILE
|
2017-03-27 09:17:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-30 08:52:35 +01:00
|
|
|
def export_pdf(content, config):
|
2017-03-30 18:26:06 +01:00
|
|
|
PDF_OPTIONS['title'] = config.get('title', 'Output')
|
2017-03-31 18:12:11 +01:00
|
|
|
PDF_OPTIONS['replace'] = [(key, str(value)) for key, value in config['context'].items()]
|
2017-05-03 15:33:42 +01:00
|
|
|
|
|
|
|
PDF_OPTIONS['margin-top'] = config['context'].get('margin_vertical', DEFAULT_MARGIN_VERTICAL)
|
|
|
|
PDF_OPTIONS['margin-bottom'] = config['context'].get('margin_vertical', DEFAULT_MARGIN_VERTICAL)
|
|
|
|
PDF_OPTIONS['margin-left'] = config['context'].get('margin_horizontal', DEFAULT_MARGIN_HORIZONTAL)
|
|
|
|
PDF_OPTIONS['margin-right'] = config['context'].get('margin_horizontal', DEFAULT_MARGIN_HORIZONTAL)
|
|
|
|
|
2017-04-01 16:12:03 +01:00
|
|
|
logger.info("Rendering PDF...")
|
2017-03-27 09:17:49 +01:00
|
|
|
return pdfkit.from_string(
|
|
|
|
content,
|
2017-03-30 18:26:06 +01:00
|
|
|
os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'),
|
2017-03-28 21:59:11 +01:00
|
|
|
options=PDF_OPTIONS,
|
|
|
|
cover=OUTPUT_COVER_FILE
|
2017-03-27 09:17:49 +01:00
|
|
|
)
|