def set_header_footer(self, canvas, doc):
"""
Set header and footer on each page; default is NO header and footer with
a page no.
"""
# Save canvas
canvas.saveState()
page_num = canvas.getPageNumber()
# Header
if self.page_header:
header = Paragraph(self.page_header, self.styles['page_header'])
w, h = header.wrap(doc.width, doc.topMargin)
header.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin - h)
# Footer
_footer = self.page_footer or "Pg. %s" % page_num
footer = Paragraph(_footer, self.styles['page_footer'])
w, h = footer.wrap(doc.width, doc.bottomMargin)
footer.drawOn(canvas, doc.leftMargin, h)
# Release the canvas
canvas.restoreState()
python类restoreState()的实例源码
def test2(canv,testpara):
#print test_program; return
from reportlab.lib.units import inch
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib import rparsexml
parsedpara = rparsexml.parsexmlSimple(testpara,entityReplacer=None)
S = ParagraphStyle("Normal", None)
P = Para(S, parsedpara)
(w, h) = P.wrap(5*inch, 10*inch)
print("wrapped as", (h,w))
canv.saveState()
canv.translate(1*inch, 1*inch)
canv.rect(0,0,5*inch,10*inch, fill=0, stroke=1)
P.canv = canv
canv.saveState()
P.draw()
canv.restoreState()
canv.setStrokeColorRGB(1, 0, 0)
#canv.translate(0, 3*inch)
canv.rect(0,0,w,h, fill=0, stroke=1)
canv.restoreState()
canv.showPage()
def resetTransforms(self):
"""I want to draw something (eg, string underlines) w.r.t. the default user space.
Reset the matrix! This should be used usually as follows::
canv.saveState()
canv.resetTransforms()
#...draw some stuff in default space coords...
canv.restoreState() # go back!
"""
# we have to adjoin the inverse, since reset is not a basic operation (without save/restore)
(selfa, selfb, selfc, selfd, selfe, selff) = self._currentMatrix
det = selfa*selfd - selfc*selfb
resulta = selfd/det
resultc = -selfc/det
resulte = (selfc*selff - selfd*selfe)/det
resultd = selfa/det
resultb = -selfb/det
resultf = (selfe*selfb - selff*selfa)/det
self.transform(resulta, resultb, resultc, resultd, resulte, resultf)
def restoreState(self):
"""restore the graphics state to the matching saved state (see saveState)."""
self._code.append('Q')
self.pop_state_stack()
###############################################################
#
# Drawing methods. These draw things directly without
# fiddling around with Path objects. We can add any geometry
# methods we wish as long as their meaning is precise and
# they are of general use.
#
# In general there are two patterns. Closed shapes
# have the pattern shape(self, args, stroke=1, fill=0);
# by default they draw an outline only. Line segments come
# in three flavours: line, bezier, arc (which is a segment
# of an elliptical arc, approximated by up to four bezier
# curves, one for each quadrant.
#
# In the case of lines, we provide a 'plural' to unroll
# the inner loop; it is useful for drawing big grids
################################################################
#--------first the line drawing methods-----------------------
def mainPageFrame(canvas, doc):
"The page frame used for all PDF documents."
canvas.saveState()
pageNumber = canvas.getPageNumber()
canvas.line(2*cm, A4[1]-2*cm, A4[0]-2*cm, A4[1]-2*cm)
canvas.line(2*cm, 2*cm, A4[0]-2*cm, 2*cm)
if pageNumber > 1:
canvas.setFont('Times-Roman', 12)
canvas.drawString(4 * inch, cm, "%d" % pageNumber)
if hasattr(canvas, 'headerLine'): # hackish
# HVB: Changed fr Hyphenation support
# headerline = string.join(canvas.headerLine, ' \xc2\x8d ')
headerline = string.join(canvas.headerLine, ' \xc2\x8d '.decode('utf8'))
canvas.drawString(2*cm, A4[1]-1.75*cm, headerline)
canvas.setFont('Times-Roman', 8)
msg = "Generated with docpy. See http://www.reportlab.com!"
canvas.drawString(2*cm, 1.65*cm, msg)
canvas.restoreState()
def get_header_footer(self, recipient):
def func(canvas, doc):
width, height = letter
margin = 0.66 * 72
canvas.saveState()
canvas.setFillColor('gray')
canvas.drawString(margin, height - margin, "CONFIDENTIAL")
canvas.drawRightString(
width - margin, height - margin, str(timezone.now()))
canvas.drawString(
margin, margin,
f"Intended for: Title IX Coordinator {recipient}",
)
canvas.restoreState()
return func
def _header_footer(self, canvas, doc):
# Save the state of our canvas so we can draw on it
canvas.saveState()
styles = getSampleStyleSheet()
style_right = ParagraphStyle(name='right', parent=styles['Normal'], fontName='Helvetica',
fontSize=10, alignment=TA_RIGHT)
# Header
fieldsight_logo = Image('http://' + self.base_url +'/static/images/fs1.jpg')
fieldsight_logo._restrictSize(1.5 * inch, 1.5 * inch)
# headerleft = Paragraph("FieldSight", styles['Normal'])
headerright = Paragraph(self.project_name, style_right)
# w1, h1 = headerleft.wrap(doc.width, doc.topMargin)
w2, h2 = headerright.wrap(doc.width, doc.topMargin)
textWidth = stringWidth(self.project_name, fontName='Helvetica',
fontSize=10)
fieldsight_logo.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin + 12)
headerright.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin + 20)
project_logo = Image('http://' + self.base_url + self.project_logo)
project_logo._restrictSize(0.4 * inch, 0.4 * inch)
project_logo.drawOn(canvas, headerright.width + doc.leftMargin -0.5 * inch - textWidth, doc.height + doc.topMargin + 10)
# header.drawOn(canvas, doc.leftMargin + doc.width, doc.height + doc.topMargin +20)
# Footer
footer = Paragraph('Page no. '+str(canvas._pageNumber), style_right)
w, h = footer.wrap(doc.width, doc.bottomMargin)
footer.drawOn(canvas, doc.leftMargin, h + 40)
# Release the canvas
canvas.restoreState()
def myFirstPage(canvas, doc):
from reportlab.lib.colors import red
PAGE_HEIGHT = canvas._pagesize[1]
canvas.saveState()
canvas.setStrokeColor(red)
canvas.setLineWidth(5)
canvas.line(66,72,66,PAGE_HEIGHT-72)
canvas.setFont(_baseFontNameB,24)
canvas.drawString(108, PAGE_HEIGHT-108, "TABLE OF CONTENTS DEMO")
canvas.setFont(_baseFontName,12)
canvas.drawString(4 * inch, 0.75 * inch, "First Page")
canvas.restoreState()
def myLaterPages(canvas, doc):
from reportlab.lib.colors import red
PAGE_HEIGHT = canvas._pagesize[1]
canvas.saveState()
canvas.setStrokeColor(red)
canvas.setLineWidth(5)
canvas.line(66,72,66,PAGE_HEIGHT-72)
canvas.setFont(_baseFontName,12)
canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page)
canvas.restoreState()
def end_at(self, x, y, para, canvas, textobject):
offset = para.fontSize/8.0
canvas.saveState()
color = self.color
if self.color is None:
color = para.fontColor
canvas.setStrokeColor(color)
canvas.line(self.xStart, self.yStart-offset, x,y-offset)
canvas.restoreState()
def _header(canvas, doc, event, report_title, confidential=True, offset=0):
canvas.saveState()
styles = getSampleStyleSheet()
canvas.drawImage(LOGO_PATH, 0.45 * inch, PAGE_HEIGHT - inch * 0.75,
height=0.5*inch, width=1.875*inch)
canvas.setFont('Helvetica-Bold', 18)
y_coord = PAGE_HEIGHT - inch * 0.45 - offset * inch
canvas.drawString(2.75 * inch, y_coord, report_title)
canvas.setFont('Helvetica', 16)
event_title = str(event.number) + ' - ' + event.title
y_coord -= 18
canvas.drawString(2.75 * inch, y_coord, event_title)
event_date = event.date_begins.strftime('%-d %B, %Y')
y_coord -= 14
canvas.setFont('Helvetica', 12)
canvas.drawString(2.75 * inch, y_coord, event_date)
event_venue = ''
if event.hotel:
event_venue += event.hotel.name + ': '
event_venue += event.city + ', ' + event.state_prov
y_coord -= 14
canvas.drawString(2.75 * inch, y_coord, event_venue)
if confidential:
canvas.setFont('Helvetica', 16)
y_coord -= 30
canvas.drawString(2.75 * inch, y_coord, 'Do Not Copy or Distribute')
canvas.restoreState()
def _unpaid_list_header(canvas, doc, event):
canvas.saveState()
canvas.drawImage(LOGO_PATH, 0.45 * inch, PAGE_HEIGHT - inch * 0.75,
height=0.5 * inch, width=1.875*inch)
canvas.setLineWidth(2)
canvas.line(0.45 * inch, PAGE_HEIGHT - inch * 0.8,
PAGE_WIDTH - 0.45 * inch, PAGE_HEIGHT - inch * 0.8)
canvas.setLineWidth(1)
canvas.line(0.45 * inch, PAGE_HEIGHT - inch * 0.84,
PAGE_WIDTH - 0.45 * inch, PAGE_HEIGHT - inch * 0.84)
canvas.setFont('Helvetica-Bold', 18)
canvas.drawCentredString(PAGE_WIDTH/2, PAGE_HEIGHT - inch * 1.15,
'UNPAID DELEGATE LIST')
canvas.line(PAGE_WIDTH / 2 - inch * 1.5, PAGE_HEIGHT - inch * 1.18,
PAGE_WIDTH / 2 + inch * 1.5, PAGE_HEIGHT - inch * 1.18)
style = ParagraphStyle(
name='textStyle',
fontName='Helvetica',
fontSize=11,
leading=13,
alignment=TA_LEFT,
)
boilerplate = Paragraph(
'The undersigned acknowledges having attended "<b>' + \
event.title + \
'</b>" that ran beginning on ' + \
event.date_begins.strftime('%-d %B, %Y') + \
', and has received all materials and related benefits of ' \
'said conference.',
style
)
h = boilerplate.wrap(doc.width, inch * 1.5)[1]
boilerplate.drawOn(canvas, doc.leftMargin, PAGE_HEIGHT - inch * 1.25 - h)
canvas.restoreState()
def _small_header(canvas, doc, event, report_title):
canvas.saveState()
canvas.drawImage(LOGO_PATH, 0.45 * inch, PAGE_HEIGHT - inch * 0.75,
height=0.5 * inch, width=1.875*inch)
canvas.setFont('Helvetica-Bold', 16)
canvas.drawString(inch * 2.6, PAGE_HEIGHT - inch * 0.42,
report_title)
canvas.setFont('Helvetica', 13)
canvas.drawString(inch * 2.6, PAGE_HEIGHT - inch * 0.7,
str(event.number) + ' - ' + event.title)
canvas.restoreState()
def page_template(self, canvas, doc):
canvas.saveState()
canvas.setFont(self.fontName,7)
canvas.drawCentredString(defaultPageSize[0]/2, 40,_("Page %d") % doc.page)
canvas.setFont(self.fontName,5)
canvas.drawCentredString(defaultPageSize[0]/2, 20, (_("Document generated by Griffith v")+
version.pversion+" - Copyright (C) "+version.pyear+" "+
version.pauthor+" - " + _("Released Under the GNU/GPL License")).encode('utf-8'))
canvas.restoreState()
def render(self, canvas, doc):
canvas.saveState()
cnv = _rml_canvas(canvas, doc, self.styles)
cnv.render(self.node)
canvas.restoreState()