Updated fpdf to the last version (py3 compatible), fix contrib/appconfig
This commit is contained in:
+21
-17
@@ -8,8 +8,8 @@ __license__ = "LGPL 3.0"
|
||||
|
||||
# Inspired by tuto5.py and several examples from fpdf.org, html2fpdf, etc.
|
||||
|
||||
from fpdf import FPDF
|
||||
from HTMLParser import HTMLParser
|
||||
from .fpdf import FPDF
|
||||
from .py3k import PY3K, basestring, unicode, HTMLParser
|
||||
|
||||
DEBUG = False
|
||||
|
||||
@@ -78,8 +78,8 @@ class HTML2FPDF(HTMLParser):
|
||||
l = self.table_col_width[i:i+colspan]
|
||||
else:
|
||||
l = [self.td.get('width','240')]
|
||||
w = sum([self.width2mm(lenght) for lenght in l])
|
||||
h = int(self.td.get('height', 0)) / 4 or self.h*1.30
|
||||
w = sum([self.width2mm(length) for length in l])
|
||||
h = int(self.td.get('height', 0)) // 4 or self.h*1.30
|
||||
self.table_h = h
|
||||
border = int(self.table.get('border', 0))
|
||||
if not self.th:
|
||||
@@ -99,30 +99,30 @@ class HTML2FPDF(HTMLParser):
|
||||
height = h + (self.tfooter and self.tfooter[0][0][1] or 0)
|
||||
if self.pdf.y+height>self.pdf.page_break_trigger and not self.th:
|
||||
self.output_table_footer()
|
||||
self.pdf.add_page()
|
||||
self.pdf.add_page(same = True)
|
||||
self.theader_out = self.tfooter_out = False
|
||||
if self.tfoot is None and self.thead is None:
|
||||
if not self.theader_out:
|
||||
self.output_table_header()
|
||||
self.box_shadow(w, h, bgcolor)
|
||||
if DEBUG: print "td cell", self.pdf.x, w, txt, "*"
|
||||
if DEBUG: print("td cell", self.pdf.x, w, txt, "*")
|
||||
self.pdf.cell(w,h,txt,border,0,align)
|
||||
elif self.table is not None:
|
||||
# ignore anything else than td inside a table
|
||||
pass
|
||||
elif self.align:
|
||||
if DEBUG: print "cell", txt, "*"
|
||||
if DEBUG: print("cell", txt, "*")
|
||||
self.pdf.cell(0,self.h,txt,0,1,self.align[0].upper(), self.href)
|
||||
else:
|
||||
txt = txt.replace("\n"," ")
|
||||
if self.href:
|
||||
self.put_link(self.href,txt)
|
||||
else:
|
||||
if DEBUG: print "write", txt, "*"
|
||||
if DEBUG: print("write", txt, "*")
|
||||
self.pdf.write(self.h,txt)
|
||||
|
||||
def box_shadow(self, w, h, bgcolor):
|
||||
if DEBUG: print "box_shadow", w, h, bgcolor
|
||||
if DEBUG: print("box_shadow", w, h, bgcolor)
|
||||
if bgcolor:
|
||||
fill_color = self.pdf.fill_color
|
||||
self.pdf.set_fill_color(*bgcolor)
|
||||
@@ -168,7 +168,7 @@ class HTML2FPDF(HTMLParser):
|
||||
|
||||
def handle_starttag(self, tag, attrs):
|
||||
attrs = dict(attrs)
|
||||
if DEBUG: print "STARTTAG", tag, attrs
|
||||
if DEBUG: print("STARTTAG", tag, attrs)
|
||||
if tag=='b' or tag=='i' or tag=='u':
|
||||
self.set_style(tag,1)
|
||||
if tag=='a':
|
||||
@@ -216,13 +216,16 @@ class HTML2FPDF(HTMLParser):
|
||||
# save previous font state:
|
||||
self.font_stack.append((self.font_face, self.font_size, self.color))
|
||||
if 'color' in attrs:
|
||||
self.color = hex2dec(attrs['color'])
|
||||
color = hex2dec(attrs['color'])
|
||||
self.set_text_color(*color)
|
||||
self.color = color
|
||||
if 'face' in attrs:
|
||||
face = attrs.get('face').lower()
|
||||
self.pdf.set_font(face)
|
||||
self.font_face = face
|
||||
try:
|
||||
self.pdf.set_font(face)
|
||||
self.font_face = face
|
||||
except RuntimeError:
|
||||
pass # font not found, ignore
|
||||
if 'size' in attrs:
|
||||
size = int(attrs.get('size'))
|
||||
self.pdf.set_font(self.font_face, size=int(size))
|
||||
@@ -277,7 +280,7 @@ class HTML2FPDF(HTMLParser):
|
||||
|
||||
def handle_endtag(self, tag):
|
||||
#Closing tag
|
||||
if DEBUG: print "ENDTAG", tag
|
||||
if DEBUG: print("ENDTAG", tag)
|
||||
if tag=='h1' or tag=='h2' or tag=='h3' or tag=='h4':
|
||||
self.pdf.ln(6)
|
||||
self.set_font()
|
||||
@@ -326,7 +329,7 @@ class HTML2FPDF(HTMLParser):
|
||||
self.tr = None
|
||||
if tag=='td' or tag=='th':
|
||||
if self.th:
|
||||
if DEBUG: print "revert style"
|
||||
if DEBUG: print("revert style")
|
||||
self.set_style('B', False) # revert style
|
||||
self.table_col_index += int(self.td.get('colspan','1'))
|
||||
self.td = None
|
||||
@@ -348,7 +351,7 @@ class HTML2FPDF(HTMLParser):
|
||||
if size:
|
||||
self.font_size = size
|
||||
self.h = size / 72.0*25.4
|
||||
if DEBUG: print "H", self.h
|
||||
if DEBUG: print("H", self.h)
|
||||
self.pdf.set_font(self.font_face or 'times','',12)
|
||||
self.pdf.set_font_size(self.font_size or 12)
|
||||
self.set_style('u', False)
|
||||
@@ -365,7 +368,7 @@ class HTML2FPDF(HTMLParser):
|
||||
for s in ('b','i','u'):
|
||||
if self.style.get(s):
|
||||
style+=s
|
||||
if DEBUG: print "SET_FONT_STYLE", style
|
||||
if DEBUG: print("SET_FONT_STYLE", style)
|
||||
self.pdf.set_font('',style)
|
||||
|
||||
def set_text_color(self, r=None, g=0, b=0):
|
||||
@@ -394,5 +397,6 @@ class HTMLMixin(object):
|
||||
def write_html(self, text, image_map=None):
|
||||
"Parse HTML and convert it to PDF"
|
||||
h2p = HTML2FPDF(self, image_map)
|
||||
text = h2p.unescape(text) # To deal with HTML entities
|
||||
h2p.feed(text)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user