seitime-frappe/frappe/utils/pdf.py
2014-12-26 13:17:18 +05:30

36 lines
863 B
Python

# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
import pdfkit, os, frappe
from frappe.utils import scrub_urls
def get_pdf(html, options=None):
if not options:
options = {}
options.update({
"print-media-type": None,
"background": None,
"images": None,
'margin-top': '15mm',
'margin-right': '15mm',
'margin-bottom': '15mm',
'margin-left': '15mm',
'encoding': "UTF-8",
'quiet': None,
'no-outline': None
})
if not options.get("page-size"):
options['page-size'] = frappe.db.get_single_value("Print Settings", "pdf_page_size") or "A4"
html = scrub_urls(html)
fname = os.path.join("/tmp", frappe.generate_hash() + ".pdf")
pdfkit.from_string(html, fname, options=options or {})
with open(fname, "rb") as fileobj:
filedata = fileobj.read()
os.remove(fname)
return filedata