Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _margin_top = 10*mm
- _margin_bottom = 30*mm
- _margin_left = 20*mm
- _margin_right = 50*mm
- _footer_height = 30*mm
- _header_height = 20*mm
- class PageNumCanvas(canvas.Canvas):
- """
- http://code.activestate.com/recipes/546511-page-x-of-y-with-reportlab/
- http://code.activestate.com/recipes/576832/
- """
- #----------------------------------------------------------------------
- def __init__(self, *args, **kwargs):
- """Constructor"""
- canvas.Canvas.__init__(self, *args, **kwargs)
- self.pages = []
- #----------------------------------------------------------------------
- def showPage(self):
- """
- On a page break, add information to the list
- """
- self.pages.append(dict(self.__dict__))
- self._startPage()
- #----------------------------------------------------------------------
- def save(self):
- """
- Add the page number to each page (page x of y)
- """
- page_count = len(self.pages)
- for page in self.pages:
- self.__dict__.update(page)
- self.draw_page_number(page_count)
- canvas.Canvas.showPage(self)
- canvas.Canvas.save(self)
- #----------------------------------------------------------------------
- def draw_page_number(self, page_count):
- """
- Add the page number
- """
- page = "Page %s of %s" % (self._pageNumber, page_count)
- self.setFont("Helvetica", 9)
- self.drawCentredString(210*mm/2, 10*mm, page)
- class FrontCoverTemplate(PageTemplate):
- def __init__(self):
- frame = Frame( _margin_left, _margin_bottom+_footer_height, 210*mm-(_margin_left+_margin_right), 297*mm-(_margin_top+_margin_bottom+_header_height+_footer_height), id='main_frame', showBoundary=1,rightPadding=0, leftPadding=0, topPadding=0, bottomPadding=0)
- PageTemplate.__init__(self, id='first',frames=[frame], pagesize=A4)
- def beforeDrawPage(self,canvas,doc):
- styles = getSampleStyleSheet()
- canvas.saveState()
- #header
- #draw header border
- canvas.setFillColorRGB(1,0,1)
- canvas.rect(_margin_left,297*mm-_margin_top-_header_height,210*mm-_margin_left-_margin_right,_header_height, fill=1)
- #footer
- canvas.setFillColorRGB(1,0,1)
- canvas.rect(_margin_left,_margin_bottom,210*mm-_margin_left-_margin_right,_footer_height, fill=1)
- canvas.drawString(_margin_left,_margin_bottom-10*mm, "My address")
- canvas.restoreState()
- #to be updated
- class LaterTemplate(PageTemplate):
- def __init__(self):
- frame = Frame( 20*mm, 20*mm, 297*mm-40*mm, 210*mm-40*mm, id='main_frame', showBoundary=1,rightPadding=0, leftPadding=0, topPadding=0, bottomPadding=0)
- PageTemplate.__init__(self, id='later',frames=[frame],pagesize=landscape(A4))
- class DeviceMontageOrder():
- def __init__(self):
- self.buffer = BytesIO()
- pdfmetrics.registerFont(TTFont('Roboto', 'Roboto-Regular.ttf'))
- self.styles = getSampleStyleSheet()####styles
- self.moj_styl=ParagraphStyle('moj_styl',
- alignment=TA_LEFT,
- fontSize=8,
- fontName="Roboto")
- self.maly_styl=ParagraphStyle('maly_styl',
- alignment=TA_LEFT,
- fontSize=6,
- fontName="Roboto")
- margin = 0.8*cm
- self.doc = BaseDocTemplate(self.buffer)
- self.doc.addPageTemplates(
- [
- FrontCoverTemplate(),
- LaterTemplate(),
- ])
- def get_info_page(self):
- elements =[]
- elements.append(Paragraph("Jedziemy gdzieśtam",self.moj_styl))
- return elements
- def get_device_page(self):
- elements =[]
- elements.append(Paragraph("Montujemy sobie cośtam",self.moj_styl))
- return elements
- def print_single_order(self):
- elements=[]
- elements.append(NextPageTemplate('first'))
- elements = elements + self.get_info_page()
- elements.append(PageBreak())
- elements.append(NextPageTemplate('later'))
- elements = elements + self.get_device_page()
- elements.append(PageBreak())
- elements = elements + self.get_device_page()
- print(elements)
- self.doc.build(elements,canvasmaker=PageNumCanvas)
- # Get the value of the BytesIO buffer and write it to the response.
- pdf = self.buffer.getvalue()
- self.buffer.close()
- return pdf
Add Comment
Please, Sign In to add comment