|
|
@@ -7,6 +7,7 @@ from frappe.modules import get_doc_path |
|
|
|
from jinja2 import TemplateNotFound |
|
|
|
from frappe.utils import cint, strip_html |
|
|
|
from frappe.utils.pdf import get_pdf |
|
|
|
from pyPdf import PdfFileWriter, PdfFileReader |
|
|
|
|
|
|
|
no_cache = 1 |
|
|
|
no_sitemap = 1 |
|
|
@@ -17,31 +18,28 @@ standard_format = "templates/print_formats/standard.html" |
|
|
|
@frappe.whitelist() |
|
|
|
def download_multi_pdf(doctype, name, format=None): |
|
|
|
# name can include names of many docs of the same doctype. |
|
|
|
totalhtml = "" |
|
|
|
# Pagebreak to be added between each doc html |
|
|
|
pagebreak = """<p style="page-break-after:always;"></p>""" |
|
|
|
|
|
|
|
options = {} |
|
|
|
|
|
|
|
import json |
|
|
|
result = json.loads(name) |
|
|
|
# Get html of each doc and combine including page breaks |
|
|
|
|
|
|
|
# Concatenating pdf files |
|
|
|
output = PdfFileWriter() |
|
|
|
for i, ss in enumerate(result): |
|
|
|
html = frappe.get_print(doctype, ss, format) |
|
|
|
if i == len(result)-1: |
|
|
|
totalhtml = totalhtml + html |
|
|
|
else: |
|
|
|
totalhtml = totalhtml + html + pagebreak |
|
|
|
output = frappe.get_print(doctype, ss, format, as_pdf = True, output = output) |
|
|
|
|
|
|
|
frappe.local.response.filename = "{doctype}.pdf".format(doctype=doctype.replace(" ", "-").replace("/", "-")) |
|
|
|
frappe.local.response.filecontent = read_multi_pdf(output) |
|
|
|
frappe.local.response.type = "download" |
|
|
|
|
|
|
|
# Title of pdf |
|
|
|
options.update({ |
|
|
|
'title': doctype, |
|
|
|
}) |
|
|
|
def read_multi_pdf(output): |
|
|
|
# Get the content of the merged pdf files |
|
|
|
fname = os.path.join("/tmp", "frappe-pdf-{0}.pdf".format(frappe.generate_hash())) |
|
|
|
output.write(open(fname,"wb")) |
|
|
|
|
|
|
|
frappe.local.response.filecontent = get_pdf(totalhtml, options) |
|
|
|
frappe.local.response.type = "download" |
|
|
|
with open(fname, "rb") as fileobj: |
|
|
|
filedata = fileobj.read() |
|
|
|
|
|
|
|
return filedata |
|
|
|
|
|
|
|
@frappe.whitelist() |
|
|
|
def download_pdf(doctype, name, format=None, doc=None): |
|
|
|