This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
md-pdf/md_pdf/build/pdf.py
2017-05-17 15:02:52 +01:00

55 lines
1.7 KiB
Python

import pdfkit
from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR
from md_pdf.build.templates import OUTPUT_COVER_FILE
import os
import logging
logger = logging.getLogger(__file__)
DEFAULT_MARGIN_VERTICAL = '1.5cm'
DEFAULT_MARGIN_HORIZONTAL = '2.5cm'
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')
TOC_TEMPLATE_FILE = os.path.join(TEMPLATES_DIR, 'toc.xsl')
TOC_OPTIONS = {
'xsl-style-sheet': TOC_TEMPLATE_FILE
}
PDF_OPTIONS = {
"no-pdf-compression": "",
"enable-internal-links": "",
"header-html": HEADER_FILE,
"footer-html": FOOTER_FILE,
"footer-spacing": 5,
"header-spacing": 5,
"user-style-sheet": STYLE_FILE
}
def export_pdf(content, config):
if logger.getEffectiveLevel() > logging.DEBUG:
PDF_OPTIONS['quiet'] = ""
PDF_OPTIONS['title'] = config.get('title', 'Output')
PDF_OPTIONS['replace'] = [(key, str(value)) for key, value in config['context'].items()]
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)
logger.info("Rendering PDF...")
return pdfkit.from_string(
content,
os.path.join(os.path.abspath(config['output_dir']), 'output.pdf'),
options=PDF_OPTIONS,
cover=OUTPUT_COVER_FILE,
toc=TOC_OPTIONS,
cover_first=True
)