How to use the xhtml2pdf.document.pisaDocument function in xhtml2pdf

To help you get started, we’ve selected a few xhtml2pdf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xhtml2pdf / xhtml2pdf / tests / test_document.py View on Github external
def test_document_creation_without_metadata():
    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT.format(head="", extra_html="")),
            dest=pdf_file
        )
        _compare_pdf_metadata(pdf_file, tools.assert_not_equal)
github xhtml2pdf / xhtml2pdf / tests / test_document.py View on Github external
def test_document_background_image_not_on_all_pages():
    """
    Test that all pages are being rendered, when background is a pdf file and it's applied for the first page only.
    """
    tests_folder = os.path.dirname(os.path.realpath(__file__))
    background_path = os.path.join(tests_folder, 'samples', 'images.pdf')

    css = "<style>@page {{background-image: url({background_location});}} @page two {{}}</style>".format(
        background_location=background_path)

    extra_html = """  <p>Hello, world!</p>"""

    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT.format(head=css, extra_html=extra_html)),
            dest=pdf_file
        )
        pdf_file.seek(0)
        pdf_reader = PdfFileReader(pdf_file)

        tools.assert_equal(pdf_reader.getNumPages(), 2)
github xhtml2pdf / xhtml2pdf / tests / test_document.py View on Github external
def test_document_background_image():
    """
    Test that a transparent PNG image is rendered properly.
    """
    tests_folder = os.path.dirname(os.path.realpath(__file__))
    image_path = os.path.join(tests_folder, 'samples', 'img', 'denker-transparent.png')

    css_background = "<style>@page {{background-image: url({background_location});}} </style>".format(background_location=image_path)

    with tempfile.TemporaryFile() as pdf_file:
        pisaDocument(
            src=io.StringIO(HTML_CONTENT.format(head=css_background, extra_html="")),
            dest=pdf_file
        )
        pdf_file.seek(0)
        pdf_reader = PdfFileReader(pdf_file)

        xobjects = pdf_reader.getPage(0)['/Resources']['/XObject'].getObject()
        objects = [xobjects[key] for key in xobjects.keys()]

        # Identity the 'denker_transparent.png' image by its height and width, and make sure it's there.
        denker_transparant = [obj for obj in objects if obj['/Height'] == 137 and obj['/Width'] == 70]
        tools.assert_equal(len(denker_transparant), 1)
github xhtml2pdf / xhtml2pdf / tests / test_document.py View on Github external
def test_document_creation_with_css_metadata():
    for css_code in CSS_TESTS:
        with tempfile.TemporaryFile() as pdf_file:
            pisaDocument(
                src=io.StringIO(HTML_CONTENT.format(head=css_code, extra_html="")),
                dest=pdf_file,
                context_meta=METADATA
            )
            _compare_pdf_metadata(pdf_file, tools.assert_equal)
github xhtml2pdf / xhtml2pdf / tests / test_document.py View on Github external
def test_in_memory_document():
    with io.BytesIO() as in_memory_file:
        pisaDocument(HTML_CONTENT.format(head="", extra_html=""), dest=in_memory_file)
        tools.assert_greater(len(in_memory_file.getvalue()), 0)

    with io.BytesIO() as in_memory_file:
        pisaDocument(io.StringIO(HTML_CONTENT.format(head="", extra_html="")), dest=in_memory_file)
        tools.assert_greater(len(in_memory_file.getvalue()), 0)
github xhtml2pdf / xhtml2pdf / tests / test_document.py View on Github external
def test_destination_is_none():
    context = pisaDocument(HTML_CONTENT.format(head="", extra_html=""))
    tools.assert_greater(len(context.dest.getvalue()), 0)
github xhtml2pdf / xhtml2pdf / xhtml2pdf / pisa.py View on Github external
fdest = sys.stdout
            startviewer = 0
        else:
            dest = os.path.abspath(dest)
            try:
                open(dest, "wb").close()
            except:
                print ("File '%s' seems to be in use of another application." % dest)
                sys.exit(2)
            fdest = open(dest, "wb")
            fdestclose = 1

        if not quiet:
            print ("Converting {} to {}...".format(src, dest))

        pisaDocument(
            fsrc,
            fdest,
            debug=debug,
            path=wpath,
            errout=sys.stdout,
            tempdir=tempdir,
            format=format,
            link_callback=lc,
            default_css=css,
            xhtml=xhtml,
            encoding=encoding,
            xml_output=xml_output
        )

        if xml_output:
            xml_output.getvalue()
github RudolfCardinal / crate / pythonlib / rnc_pdf.py View on Github external
def pdf_from_html(html, header_html=None, footer_html=None,
                  wkhtmltopdf_options=None, file_encoding="utf-8"):
    """
    Takes HTML and returns a PDF (as a buffer in Python 2, or a memoryview
    in Python 3).
    For engines not supporting CSS Paged Media - meaning, here, wkhtmltopdf -
    the header_html and footer_html options allow you to pass appropriate HTML
    content to serve as the header/footer (rather than passing it within the
    main HTML).
    """
    if processor == XHTML2PDF:
        memfile = io.BytesIO()
        xhtml2pdf.document.pisaDocument(html, memfile)
        # ... returns a document, but we don't use it, so we don't store it to
        # stop pychecker complaining
        # http://xhtml2pdf.appspot.com/static/pisa-en.html
        memfile.seek(0)
        return buffer(memfile.read())
        # http://stackoverflow.com/questions/3310584

    elif processor == WEASYPRINT:
        # http://ampad.de/blog/generating-pdfs-django/
        return weasyprint.HTML(string=html).write_pdf()

    elif processor == PDFKIT:
        if _wkhtmltopdf_filename is None:
            config = None
        else:
            # config = pdfkit.configuration(wkhtmltopdf=_wkhtmltopdf_filename)