def _print_index(self, index, header): # Generate index page and TOC based on it story = self._story story.append(Spacer(self._get_page_width(), self.PAGE_HEIGHT / 4)) story.append(RLParagraph(header, self._styles['title'])) story.append(Spacer(self._get_page_width(), self.PAGE_HEIGHT / 5)) story.append(RLParagraph(self.author, self._styles['title-author'])) blocks = iter(index) for block in blocks: refs = [part.text for part in block if isinstance(part, Reference)] if '__endfrontpage__' in refs: story.append(PageBreak()) if '__endbackpage__' in refs: break self._print_paragraph(block, root=True) self._find_page_info(blocks) # Print table of contents. index.md contains story.append(PageBreak()) story.append(RLParagraph(self.TITLE_TOC, self._styles['h3'])) self._add_toc(TableOfContents()) # Generate list of sources if self.print_sources: story.append(PageBreak()) for title, re_ext in self.SOURCE_INDEXES: story.append(RLParagraph(title, self._styles['h3'])) self._add_toc(ListOfListings(re_ext))
def handle_pageEnd(self): # Get main page frame boundaries frame = self.pageTemplate.frames[0] l, r = frame._leftPadding, frame._width - frame._rightPadding h = frame._topPadding H = frame._height # Draw heading para = RLParagraph(os.environ['TSDOC_HEADER'], style=ParagraphStyle('title', alignment=enums.TA_RIGHT, fontName='LCMSS8', fontSize=12, leading=14)) pw, ph = para.wrapOn(self.canv, (r - l), h) para.drawOn(self.canv, l, H - h) # Draw watermark with open('book/watermark.txt') as f: watermark = f.read() para = RLParagraph(watermark, style=ParagraphStyle('footer-base', fontName='Times-Roman', leading=10, fontSize=8, alignment=enums.TA_LEFT)) pw, ph = para.wrapOn(self.canv, (r - l), h) para.drawOn(self.canv, l, h - ph) return self._handle_pageEnd()
def handle_pageEnd(self): if self.page < 3: return self._handle_pageEnd() canv = self.canv # Get main page frame boundaries frames = self.pageTemplate.frames w = sum(frame._width for frame in frames) l, r = frames[0]._leftPadding, w - frames[-1]._rightPadding h = max(frames[0]._bottomPadding, frames[-1]._bottomPadding) # Draw line canv.saveState() canv.setStrokeColorRGB(0, 0, 0) canv.setLineWidth(0.5) canv.line(l, h, r, h) canv.restoreState() # Draw page number & header rtext, ltext = str(self.page), self.header if (self.page % 2) == 0: rtext, ltext = ltext, rtext style = ParagraphStyle('footer-base', fontName='Times-Roman', leading=12, fontSize=10) right = RLParagraph(rtext, style=ParagraphStyle('footer-right', parent=style, alignment=enums.TA_RIGHT)) left = RLParagraph(ltext, style=ParagraphStyle('footer-left', parent=style, alignment=enums.TA_LEFT)) pw, ph = right.wrapOn(canv, (r - l), h) right.drawOn(canv, l, h - ph) pw, ph = left.wrapOn(canv, (r - l), h) left.drawOn(canv, l, h - ph) # Set next page template(-s) if self._page_template is not None: self._handle_nextPageTemplate(self._page_template) return self._handle_pageEnd()
def _print_code(self, code, root=False): style = self._styles['code' if root else 'subcode'] if isinstance(code, CodeListing) and not self.print_sources: self._story.append( RLParagraph( 'Source file: <a href="{url}/{fname}" color="blue">{fname}</a>' .format(url=self.SOURCE_URL, fname=code.fname), style=self._styles['default'])) return out = cStringIO.StringIO() for part in code.parts: # TODO: support bold/italic text in preformatted text = str(part).replace('\t', ' ') for line in text.splitlines(True): tags = self.CODE_TAGS.get(type(part), ('', '')) self._print_code_line(line, out, style, tags) if isinstance(code, CodeListing): self._story.append(_ListingInfoFlowable(code.fname)) self._begin_incut(code) self._story.append(XPreformatted(out.getvalue(), style)) self._end_incut(code)
def _end_incut(self, block): incut_style, _ = self._incut_format(block) if incut_style: incut_style = ParagraphStyle(incut_style.name + '-end', parent=incut_style, fontSize=4, leading=4) self._story.append(RLParagraph(' ', style=incut_style))
def _print_reference(self): index = SimpleIndex() index.setup(self._styles['root']) self._story.append(NextPageTemplate('SimpleIndex')) self._story.append(PageBreak()) self._story.append(RLParagraph(self.TITLE_INDEX, self._styles['h3'])) self._story.append(index) return index.getCanvasMaker()
def _begin_incut(self, block): incut_style, incut_message = self._incut_format(block) if incut_style: self._story.append(CondPageBreak(pagesizes.inch / 2)) self._story.append(RLParagraph(incut_message, style=incut_style))
def do_print_pages(self, stream, header, pages): story = self._story self.stream = stream print >> sys.stderr, "Generating PDF..." self._register_fonts() pages = iter(pages) if self.print_index: index = next(pages) self._current_page = index self._print_index(index, header) for page in pages: # In the beginning of new section -- add page break self._current_page = page if hasattr(page, 'is_external'): story.append(PageBreak()) story.append(Spacer(self.PAGE_WIDTH, self.PAGE_HEIGHT / 5)) if self.print_index: story.append( _PageInfoFlowable(page, self._page_info.get(page.page_path))) if hasattr(page, 'is_external'): story.append(RLParagraph(page.header, style=self._styles['h2'])) continue # Render page blocks for block in page: self._print_paragraph(block, root=True) if self._page_template_name != self.doc_cls.DEFAULT_PAGE_TEMPLATE: # If page has overriden page styles, revert it afterwards story.append( NextPageTemplate(self.doc_cls.DEFAULT_PAGE_TEMPLATE)) self._page_template_name = self.doc_cls.DEFAULT_PAGE_TEMPLATE # Rendering PDF... print >> sys.stderr, "Rendering PDF..." docf = NamedTemporaryFile() doc = self.doc_cls(docf) doc.addPageTemplates(self._get_page_templates()) if self.print_index: canvasmaker = self._print_reference() doc.multiBuild(story, canvasmaker=canvasmaker) else: doc.build(story) # Rendering PDF with images... print >> sys.stderr, "Rendering PDF images..." docf.flush() self._merge_pdf_images(docf, stream, doc.outlines)