Remove header and footer from 1st page

不羁岁月 提交于 2020-01-15 07:35:12

问题


 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!