Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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, {})
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())
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)
# 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
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
def test_get_size_for_in(self):
res = getSize("1in")
self.assertEqual(res, 72.00)
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'))
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'))
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')