Selaa lähdekoodia

[Fix] Multiple letter head printing issue on print format (#2365)

version-14
rohitwaghchaure 8 vuotta sitten
committed by Rushabh Mehta
vanhempi
commit
e03d56adb6
4 muutettua tiedostoa jossa 31 lisäystä ja 23 poistoa
  1. +2
    -2
      frappe/__init__.py
  2. +13
    -4
      frappe/utils/pdf.py
  3. +15
    -17
      frappe/utils/print_format.py
  4. +1
    -0
      requirements.txt

+ 2
- 2
frappe/__init__.py Näytä tiedosto

@@ -1152,7 +1152,7 @@ def format(*args, **kwargs):
import frappe.utils.formatters
return frappe.utils.formatters.format_value(*args, **kwargs)

def get_print(doctype=None, name=None, print_format=None, style=None, html=None, as_pdf=False, doc=None):
def get_print(doctype=None, name=None, print_format=None, style=None, html=None, as_pdf=False, doc=None, output = None):
"""Get Print Format for given document.

:param doctype: DocType of document.
@@ -1173,7 +1173,7 @@ def get_print(doctype=None, name=None, print_format=None, style=None, html=None,
html = build_page("print")

if as_pdf:
return get_pdf(html)
return get_pdf(html, output = output)
else:
return html



+ 13
- 4
frappe/utils/pdf.py Näytä tiedosto

@@ -6,17 +6,20 @@ import pdfkit, os, frappe
from frappe.utils import scrub_urls
from frappe import _
from bs4 import BeautifulSoup
from pyPdf import PdfFileWriter, PdfFileReader

def get_pdf(html, options=None):
def get_pdf(html, options=None, output = None):
html = scrub_urls(html)
html, options = prepare_options(html, options)
fname = os.path.join("/tmp", "frappe-pdf-{0}.pdf".format(frappe.generate_hash()))

try:
pdfkit.from_string(html, fname, options=options or {})

with open(fname, "rb") as fileobj:
filedata = fileobj.read()
if output:
append_pdf(PdfFileReader(file(fname,"rb")),output)
else:
with open(fname, "rb") as fileobj:
filedata = fileobj.read()

except IOError, e:
if ("ContentNotFoundError" in e.message
@@ -37,9 +40,15 @@ def get_pdf(html, options=None):
finally:
cleanup(fname, options)

if output:
return output

return filedata

def append_pdf(input,output):
# Merging multiple pdf files
[output.addPage(input.getPage(page_num)) for page_num in range(input.numPages)]

def prepare_options(html, options):
if not options:
options = {}


+ 15
- 17
frappe/utils/print_format.py Näytä tiedosto

@@ -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):


+ 1
- 0
requirements.txt Näytä tiedosto

@@ -39,3 +39,4 @@ psutil
unittest-xml-reporting
xlwt
oauthlib
pypdf

Ladataan…
Peruuta
Tallenna