def pisaStory(src, path=None, link_callback=None, debug=0, default_css=None, xhtml=False, encoding=None, context=None, xml_output=None, **kw): # Prepare Context if not context: context = pisaContext(path, debug=debug) context.pathCallback = link_callback # Use a default set of CSS definitions to get an expected output if default_css is None: default_css = DEFAULT_CSS # Parse and fill the story pisaParser(src, context, default_css, xhtml, encoding, xml_output) # Avoid empty documents if not context.story: context.story = [Spacer(1,1)] if context.indexing_story: context.story.append(context.indexing_story) # Remove anchors if they do not exist (because of a bug in Reportlab) for frag, anchor in context.anchorFrag: if anchor not in context.anchorName: frag.link = None return context
def pisaStory(src, path=None, link_callback=None, debug=0, default_css=None, xhtml=False, encoding=None, context=None, xml_output=None, **kw): # Prepare Context if not context: context = pisaContext(path, debug=debug) context.pathCallback = link_callback # Use a default set of CSS definitions to get an expected output if default_css is None: default_css = DEFAULT_CSS # Parse and fill the story pisaParser(src, context, default_css, xhtml, encoding, xml_output) # Avoid empty documents if not context.story: context.story = [Spacer(1, 1)] if context.indexing_story: context.story.append(context.indexing_story) # Remove anchors if they do not exist (because of a bug in Reportlab) for frag, anchor in context.anchorFrag: if anchor not in context.anchorName: frag.link = None return context
def test_tr_background_color(self): """ Test background on <tr> tag; If it works, "darkgreen" will be returned as hexval "0x006400" """ html = """ <html> <head> <style> tr { background-color: darkgreen } </style> </head> <body> <table> <tr> <td>Test</td> </tr> </table> </body> </html> """ context = pisaParser(BytesIO(html.encode('utf-8')), pisaContext(None)) table = context.story[0] color = table._bkgrndcmds[0][3] self.assertEqual(color.hexval(), '0x006400', '"background-color" in CSS not equal with output!')
def test_td_rowspan(self): """ Test rowspan on <td> tag; If it works, rowspan="3" will be equal to (0, 2) in _spanCmds of the ReportLab table """ html = """ <html> <head> <style> </style> </head> <body> <table> <tr> <td rowspan="3">AAAAAAAAAAAA</td> <td>BBB</td> </tr> <tr> <td>CCC</td> </tr> <tr> <td>DDD</td> </tr> </table> </body> </html> """ context = pisaParser(BytesIO(html.encode('utf-8')), pisaContext(None)) table = context.story[0] row_span = table._spanCmds[0][2] self.assertEqual(row_span, (0, 2), '"rowspan=" not working properly in the <td> tag!')
def test_height_as_list(self): """Asserts attributes like 'height: 10px !important" are parsed""" c = pisaContext(".") data = b"<p style='height: 10px !important;width: 10px !important'>test</p>" r = pisaParser(data, c) self.assertEqual(c, r) self.assertEqual(r.err, 0) self.assertEqual(r.warn, 0)
def test_image_os_path(self): c = pisaContext(".") tests_folder = os.path.dirname(os.path.realpath(__file__)) img_path = os.path.join(tests_folder, 'samples', 'img', 'denker.png') data = '<img src="{0}">'.format(img_path).encode('utf-8') r = pisaParser(data, c) self.assertEqual(c, r) self.assertEqual(r.err, 0) self.assertEqual(r.warn, 0)
def pisaStory( src, path = None, link_callback = None, debug = 0, default_css = None, xhtml = False, encoding = None, c = None, xml_output = None, **kw): # Prepare Context if not c: c = pisaContext(path, debug=debug) c.pathCallback = link_callback # Use a default set of CSS definitions to get an expected output if default_css is None: default_css = DEFAULT_CSS # Parse and fill the story pisaParser(src, c, default_css, xhtml, encoding, xml_output) #if 0: # import reportlab.pdfbase.pdfmetrics as pm # pm.dumpFontData() # Avoid empty documents if not c.story: c.story = [Spacer(1,1)] # c.addPara(force=True) # Remove anchors if they do not exist (because of a bug in Reportlab) for frag, anchor in c.anchorFrag: if anchor not in c.anchorName: frag.link = None return c
def test_empty_table(self): """ Test, if an empty table will return an empty story; This will also raise a log.warning() """ html = """ <html> <head> </head> <body> <table> </table> </body> </html> """ context = pisaParser(BytesIO(html.encode('utf-8')), pisaContext(None)) self.assertEqual(context.story, [], "Empty table doesn't return an empty story!")
def test_td_width_and_height(self): """ Test width and height on <td> tag; If it works, width: 2pt will be equal to 2.0 and height: 3pt will be equal to 3.0 in the ReportLab table """ html = """ <html> <head> <style> td { width: 2pt; height: 3pt } </style> </head> <body> <table> <tr> <td>AAA</td> <td>BBB</td> </tr> <tr> <td>CCC</td> <td>DDD</td> </tr> </table> </body> </html> """ context = pisaParser(BytesIO(html.encode('utf-8')), pisaContext(None)) table = context.story[0] col_widths = table._colWidths row_heights = table._rowHeights for width in col_widths: self.assertEqual(width, 2.0, '<td> width in CSS not equal with output!') for height in row_heights: self.assertEqual(height, 3.0, '<td> height in CSS not equal with output!')
def test_getFile(self): c = pisaContext(".") r = pisaParser(_data, c) self.assertEqual(c.getFile(None), None)
def testParser(self): c = pisaContext(".") r = pisaParser(_data, c) self.assertEqual(c, r)
def test_image_base64(self): c = pisaContext(".") data = b'<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=">' r = pisaParser(data, c) self.assertEqual(r.warn, 0)