问题
class MyDocTemplate(BaseDocTemplate):
def __init__(self, filename, **kw):
self.allowSplitting = 0
apply(BaseDocTemplate.__init__, (self, filename), kw)
template = PageTemplate('normal', [Frame(1.0*cm, 1*cm, 19*cm, 27*cm, id='F1')])
self.addPageTemplates(template)
def afterFlowable(self, flowable):
if flowable.__class__.__name__ == 'Paragraph':
text = flowable.getPlainText()
style = flowable.style.name
if style == 'Heading1':
level = 0
elif style == 'Heading2':
level = 1
else:
return
E = [level, text, self.page]
#if we have a bookmark name append that to our notify data
bn = getattr(flowable,'_bookmarkName',None)
if bn is not None:
E.append(bn)
self.notify('TOCEntry', tuple(E))
class FooterCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs):
canvas.Canvas.__init__(self, *args, **kwargs)
self.pages = []
def showPage(self):
self.pages.append(dict(self.__dict__))
self._startPage()
def save(self):
page_count = len(self.pages)
for page in self.pages:
self.__dict__.update(page)
self.draw_canvas(page_count)
canvas.Canvas.showPage(self)
canvas.Canvas.save(self)
def draw_canvas(self, page_count):
page = "Page %s" % (self._pageNumber)
self.saveState()
self.setFont('Times-Roman', 10)
self.line(40,23,560,23)
self.drawString(280, 25, page)
self.line(40, 810, 560, 810)
self.drawString(40,813,"abc")
self.restoreState()
As you can see this will create TOC as well as header and footer from FooterCanvas class and FooterCanvas class is getting applied to all the pages but I don't want it to be applied to first page of my pdf. So how can I achieve this?
回答1:
It looks that you only have one PageTemplate. When I want similar control, I create two separate PageTemplates, such as below.
frontpage = PageTemplate(id='FrontPage', onPage=footer, frames=[])
backpage = PageTemplate(id='BackPage', onPage=header, frames=[])
MyDocTemplate.addPageTemplates(frontpage) MyDocTemplate.addPageTemplates(backpage)
回答2:
I have found one more way to do it with the help of other answers. So here it is.
Frame1=Frame(x1,y1,width,height,id='F1')
Frame2=Frame(x1,y1,width,height,id='F2')
def FooterCanvas(canvas,doc):
page = "Page %s" % (canvas._pageNumber)
canvas.saveState()
canvas.setFont('Times-Roman', 10)
canvas.drawString(280, 15, page)
canvas.restoreState()
class MyDocTemplate(BaseDocTemplate):
def __init__(self, filename, **kw):
self.allowSplitting = 0
apply(BaseDocTemplate.__init__, (self, filename), kw)
self.addPageTemplates([PageTemplate(id='firstPage',frames=Frame1),PageTemplate(id='allpages',frames=Frame2,onPage=FooterCanvas)
])
def afterFlowable(self, flowable):
if flowable.__class__.__name__ == 'Paragraph':
text = flowable.getPlainText()
style = flowable.style.name
if style == 'Heading1':
self.notify('TOCEntry', (0, text, self.page))
if style == 'Heading2':
self.notify('TOCEntry', (1, text, self.page))
Story=[]
write your styles here and write your TOC styles. create TOC instance and add levels for TOC(levels are optional).
Story.append(NextPageTemplate('firstpage'))
First page with some paragraph or graphics.
Story.append(NextPageTemplate('allpages'))
Story.append(PageBreak())
Rest of the pages with some paragraph or graphics.
doc=MyDocTemplate("abc.pdf")
doc.multiBuild(Story)
来源:https://stackoverflow.com/questions/55036519/remove-header-and-footer-from-1st-page