How to use xhtml2pdf - 10 common examples

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 / testrender / testrender.py View on Github external
def render_pdf(filename, output_dir, options):
    if options.debug:
        print('Rendering %s' % filename)
    basename = os.path.basename(filename)
    outname = '%s.pdf' % os.path.splitext(basename)[0]
    outfile = os.path.join(output_dir, outname)

    input = open(filename, 'rb')
    output = open(outfile, 'wb')

    result = pisa.pisaDocument(input, output, path=filename)

    input.close()
    output.close()

    if result.err:
        print('Error rendering %s: %s' % (filename, result.err))
        sys.exit(1)
    return outfile
github xhtml2pdf / xhtml2pdf / tests / test_tags.py View on Github external
def test_pisa_tag_will_set_attrs_on_init(self):
        dom = minidom.parseString("test")
        element = dom.getElementsByTagName("unit")[0]
        attrs = AttrContainer({})
        instance = tags.pisaTag(element, attrs)
        self.assertEqual(instance.node, element)
        self.assertEqual(instance.tag, "unit")
        self.assertEqual(instance.attr, {})
github xhtml2pdf / xhtml2pdf / test / simple.py View on Github external
def testCGI(data="Hello <b>World</b>"):

    """
    This one shows, how to get the resulting PDF as a
    file object and then send it to STDOUT
    """

    result = cStringIO.StringIO()

    pdf = pisa.CreatePDF(
        cStringIO.StringIO(data),
        result
        )

    if pdf.err:
        print "Content-Type: text/plain"
        print
        dumpErrors(pdf)
    else:
        print "Content-Type: application/octet-stream"
        print
        sys.stdout.write(result.getvalue())
github xhtml2pdf / xhtml2pdf / test / simple.py View on Github external
def testBackgroundAndImage(
    src="test-background.html",
    dest="test-background.pdf"):

    """
    Simple test showing how to create a PDF file from
    PML Source String. Also shows errors and tries to start
    the resulting PDF
    """

    pdf = pisa.CreatePDF(
        file(src, "r"),
        file(dest, "wb"),
        log_warn = 1,
        log_err = 1,
        path = os.path.join(os.getcwd(), src)
        )

    dumpErrors(pdf)
    if not pdf.err:
        pisa.startViewer(dest)
github xhtml2pdf / xhtml2pdf / test / simple.py View on Github external
# limitations under the License.

__version__ = "$Revision: 194 $"
__author__  = "$Author: holtwick $"
__date__    = "$Date: 2008-04-18 18:59:53 +0200 (Fr, 18 Apr 2008) $"

import os
import sys
import cgi
import cStringIO
import logging

import xhtml2pdf.pisa as pisa

# Shortcut for dumping all logs to the screen
pisa.showLogging()

def dumpErrors(pdf, showLog=True):
    #if showLog and pdf.log:
    #    for mode, line, msg, code in pdf.log:
    #        print "%s in line %d: %s" % (mode, line, msg)
    #if pdf.warn:
    #    print "*** %d WARNINGS OCCURED" % pdf.warn
    if pdf.err:
        print "*** %d ERRORS OCCURED" % pdf.err

def testSimple(
    data="""Hello <b>World</b><br><img src="img/test.jpg">""",
    dest="test.pdf"):

    """
    Simple test showing how to create a PDF file from
github xhtml2pdf / xhtml2pdf / test / cookbook.py View on Github external
def HTML2PDF(data, filename, open=False):

    """
    Simple test showing how to create a PDF file from
    PML Source String. Also shows errors and tries to start
    the resulting PDF
    """

    pdf = pisa.CreatePDF(
        io.StringIO(data),
        file(filename, "wb"))

    if open and (not pdf.err):
        pisa.startViewer(filename)

    return not pdf.err
github xhtml2pdf / xhtml2pdf / tests / test_utils.py View on Github external
def test_get_size_for_in(self):
        res = getSize("1in")
        self.assertEqual(res, 72.00)
github xhtml2pdf / xhtml2pdf / tests / test_tables.py View on Github external
def test_add_cell_styles_will_add_lineafter_style_if_borderright_attrs_set_on_context_frag(self):
        context = pisaContext([])
        context.frag.borderRightStyle = "solid"
        context.frag.borderRightWidth = "3px"
        context.frag.borderRightColor = "black"
        instance = self.sut()
        instance.add_cell_styles(context, (0, 1), (3, 5), mode="tr")
        self.assertEqual(instance.styles[0], ('LINEAFTER', (3, 1), (3, 5), '3px', 'black', 'squared'))
github xhtml2pdf / xhtml2pdf / tests / test_tables.py View on Github external
def test_add_cell_styles_will_add_linebelow_style_if_borderbottom_attrs_set_on_context_frag(self):
        context = pisaContext([])
        context.frag.borderBottomStyle = "solid"
        context.frag.borderBottomWidth = "3px"
        context.frag.borderBottomColor = "black"
        instance = self.sut()
        instance.add_cell_styles(context, (0, 1), (3, 5), mode="tr")
        self.assertEqual(instance.styles[0], ('LINEBELOW', (0, 5), (3, 5), '3px', 'black', 'squared'))
github xhtml2pdf / xhtml2pdf / tests / test_tables.py View on Github external
def test_add_cell_styles_will_not_add_lineafter_style_if_borderright_width_set_to_zero_on_context_frag(self):
        context = pisaContext([])
        context.frag.borderRightStyle = "solid"
        context.frag.borderRightWidth = 0
        context.frag.borderRightColor = "black"
        instance = self.sut()
        instance.add_cell_styles(context, (0, 1), (3, 5), mode="tr")
        self.assertNotEqual(instance.styles[0][0], 'LINEAFTER')