|
- # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
- # MIT License. See license.txt
- from __future__ import unicode_literals
-
- 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",
- '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
|