from reportlab.lib import colors
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.pdfgen import canvas
from reportlab.platypus.flowables \
import Spacer, Preformatted, Image, KeepTogether
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.xpreformatted \
import PythonPreformatted
from reportlab.platypus.frames import Frame
from reportlab.platypus.doctemplate \
import PageTemplate, BaseDocTemplate
def eachPage(canvas, doc):
"Adornments for each page."
canvas.saveState()
canvas.setFont('Helvetica', 8)
canvas.setFillColor(colors.black)
# add page number
num = "%d" % doc.page
canvas.drawCentredString(10.5*cm, 1*cm, num)
canvas.restoreState()
class LinuxMagDocTemplate(BaseDocTemplate):
"The document template used for all pages."
# quotes
while '"' in para:
for j in chr(227), chr(210):
i = para.find('"')
para = para[:i] + j + para[i+1:]
# guillemets and URLs
data = [
('<C>', chr(200), chr(199)),
# ('<U>', '<font color="blue">', '</font>')]
('<U>', '<a href="%s" color="blue">', '</a>')]
for (tag, before, after) in data:
pat = '%s.*?%s' % (tag, tag)
while para.find(tag) >= 0:
m = re.search(pat, para)
if not m:
continue
start, end = m.start()+3, m.end()-3
word = para[start:end]
if tag == '<U>':
before = before % word
word = before + word + after
para = re.sub(pat, word, para, 1)
# strip off heading and trailing empty lines
if listing:
while not listing[0].strip():
del listing[0]
while not listing[-1].strip():
del listing[-1]
# number lines if we have more than 10 lines
listLen = len(listing)
if listLen > 10:
format = "%%%dd %%s"
format = format % len(str(listLen))
for k in xrange(len(listing)):
listing[k] = format % (k+1, listing[k])
# assemble and add to story
listing = ''.join(listing)
# extract lines belonging to the listing
listing = []
j = 0
while 1:
j = j + 1
try:
line = lines[i + j]
except IndexError:
break
if line and line[0] == '@':
break
else:
listing.append(line)
i = i + j - 1