当前位置: 首页>>代码示例>>Python>>正文


Python pisa.pisaDocument方法代码示例

本文整理汇总了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 
开发者ID:hacktoolkit,项目名称:django-htk,代码行数:20,代码来源:pdf_utils.py

示例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 
开发者ID:silverapp,项目名称:silver,代码行数:27,代码来源:pdf.py

示例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 
开发者ID:CroceRossaItaliana,项目名称:jorvik,代码行数:23,代码来源:models.py

示例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 
开发者ID:renatoliveira,项目名称:notto,代码行数:16,代码来源:pdf_builder.py

示例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) 
开发者ID:toms3t,项目名称:Propalyzer,代码行数:13,代码来源:pdf_render.py

示例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() 
开发者ID:nigma,项目名称:django-easy-pdf,代码行数:30,代码来源:rendering.py

示例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 
开发者ID:RiajulKashem,项目名称:SRMS,代码行数:11,代码来源:views.py

示例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() 
开发者ID:CroceRossaItaliana,项目名称:jorvik,代码行数:8,代码来源:monitoraggio.py

示例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() 
开发者ID:bobintetley,项目名称:asm3,代码行数:52,代码来源:utils.py


注:本文中的xhtml2pdf.pisa.pisaDocument方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。