本文整理汇总了Python中xhtml2pdf.pisa.pisaDocument方法的典型用法代码示例。如果您正苦于以下问题:Python pisa.pisaDocument方法的具体用法?Python pisa.pisaDocument怎么用?Python pisa.pisaDocument使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xhtml2pdf.pisa
的用法示例。
在下文中一共展示了pisa.pisaDocument方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render_to_pdf_response_pisa
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def render_to_pdf_response_pisa(template_name, context_dict):
"""Render to a PDF response using Pisa
Caveat: xhtml2pdf / pisa seems to not be well-maintained and does not handle CSS3
https://github.com/xhtml2pdf/xhtml2pdf/issues/44
PyPI: https://pypi.python.org/pypi/pisa/
"""
import cStringIO as StringIO
from xhtml2pdf import pisa
html = generate_html_from_template(template_name, context_dict)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('utf-8')), result)
if pdf:
response = HttpResponse(result.getvalue(), mimetype='application/pdf')
else:
response = HttpResponseServerError('Error generating PDF file')
return response
示例2: generate
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def generate(self, template, context, upload=True):
html = template.render(context)
pdf_file_object = BytesIO()
pisa_status = pisa.pisaDocument(
src=BytesIO(html.encode("UTF-8")),
dest=pdf_file_object,
encoding='UTF-8',
link_callback=fetch_resources
)
if pisa_status.err:
logger.error(
'xhtml2pdf encountered exception during generation of pdf %s: %s',
context['filename'],
pisa_status.err
)
return
if upload:
self.upload(
pdf_file_object=pdf_file_object,
filename=context['filename']
)
return pdf_file_object
示例3: _generate_pdf
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def _generate_pdf(cls, course):
from io import BytesIO
from xhtml2pdf import pisa
from django.http import HttpResponse
from django.template.loader import render_to_string
risposte = SurveyResult.objects.filter(course=course)
html = render_to_string('pdf_questionario.html', {
'risposte': risposte,
}).encode('utf-8')
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html), result, encoding='UTF-8')
converted = result.getvalue() if not pdf.err else ''
filename = 'Questionario-di-gradimento.pdf'
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
response.write(converted)
return response
示例4: render_to_pdf
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def render_to_pdf(template_src, context_dict):
"""
renders a document to pdf using a template
"""
if context_dict is None:
context_dict = {}
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), result,
encoding='UTF-8')
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
示例5: render
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def render(path: str, params: dict):
template = get_template(path)
html = template.render(params)
response = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), response)
if not pdf.err:
return HttpResponse(response.getvalue(), content_type='application/pdf')
else:
return HttpResponse("Error Rendering PDF", status=400)
示例6: html_to_pdf
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def html_to_pdf(content, encoding="utf-8",
link_callback=fetch_resources, **kwargs):
"""
Converts html ``content`` into PDF document.
:param unicode content: html content
:returns: PDF content
:rtype: :class:`bytes`
:raises: :exc:`~easy_pdf.exceptions.PDFRenderingError`
"""
src = BytesIO(content.encode(encoding))
dest = BytesIO()
pdf = pisa.pisaDocument(src, dest, encoding=encoding,
link_callback=link_callback, **kwargs)
if pdf.err:
logger.error("Error rendering PDF document")
for entry in pdf.log:
if entry[0] == xhtml2pdf.default.PML_ERROR:
logger_x2p.error("line %s, msg: %s, fragment: %s", entry[1], entry[2], entry[3])
raise PDFRenderingError("Errors rendering PDF", content=content, log=pdf.log)
if pdf.warn:
for entry in pdf.log:
if entry[0] == xhtml2pdf.default.PML_WARNING:
logger_x2p.warning("line %s, msg: %s, fragment: %s", entry[1], entry[2], entry[3])
return dest.getvalue()
示例7: renderPdf
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def renderPdf(template, content={}):
t = get_template(template)
send_data = t.render(content)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(send_data.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
else:
return None
示例8: convert_html_to_pdf
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def convert_html_to_pdf(self):
html = self._render_to_string().encode('utf-8')
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html), result, encoding='UTF-8')
if not pdf.err:
return result.getvalue()
示例9: html_to_pdf_pisa
# 需要导入模块: from xhtml2pdf import pisa [as 别名]
# 或者: from xhtml2pdf.pisa import pisaDocument [as 别名]
def html_to_pdf_pisa(dbo, htmldata):
"""
Converts HTML content to PDF and returns the PDF file data as bytes.
NOTE: wkhtmltopdf is far superior, but this is a pure Python solution and it does work.
"""
# Allow orientation and papersize to be set
# with directives in the document source - eg: <!-- pdf orientation landscape, pdf papersize letter -->
orientation = "portrait"
# Sort out page size arguments
papersize = "A4"
if htmldata.find("pdf orientation landscape") != -1: orientation = "landscape"
if htmldata.find("pdf orientation portrait") != -1: orientation = "portrait"
if htmldata.find("pdf papersize a5") != -1: papersize = "A5"
if htmldata.find("pdf papersize a4") != -1: papersize = "A4"
if htmldata.find("pdf papersize a3") != -1: papersize = "A3"
if htmldata.find("pdf papersize letter") != -1: papersize = "letter"
# Zoom - eg: <!-- pdf zoom 0.5 end -->
# Not supported in any meaningful way by pisa (not smart scaling)
# zm = regex_one("pdf zoom (.+?) end", htmldata)
# Margins, top/bottom/left/right eg: <!-- pdf margins 2cm 2cm 2cm 2cm end -->
margins = "2cm"
mg = regex_one("pdf margins (.+?) end", htmldata)
if mg != "":
margins = mg
header = "<!DOCTYPE html>\n<html>\n<head>"
header += '<style>'
header += '@page {size: %s %s; margin: %s}' % ( papersize, orientation, margins )
header += '</style>'
header += "</head><body>"
footer = "</body></html>"
htmldata = htmldata.replace("font-size: xx-small", "font-size: 6pt")
htmldata = htmldata.replace("font-size: x-small", "font-size: 8pt")
htmldata = htmldata.replace("font-size: small", "font-size: 10pt")
htmldata = htmldata.replace("font-size: medium", "font-size: 14pt")
htmldata = htmldata.replace("font-size: large", "font-size: 18pt")
htmldata = htmldata.replace("font-size: x-large", "font-size: 24pt")
htmldata = htmldata.replace("font-size: xx-large", "font-size: 36pt")
# Remove any img tags with signature:placeholder/user as the src
htmldata = re.sub(r'<img.*?signature\:.*?\/>', '', htmldata)
# Fix up any google QR codes where a protocol-less URI has been used
htmldata = htmldata.replace("\"//chart.googleapis.com", "\"http://chart.googleapis.com")
# Switch relative document uris to absolute service based calls
htmldata = fix_relative_document_uris(dbo, htmldata)
# Do the conversion
from xhtml2pdf import pisa
out = bytesio()
pdf = pisa.pisaDocument(stringio(header + htmldata + footer), dest=out)
if pdf.err:
raise IOError(pdf.err)
return out.getvalue()