<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> from reportlab . platypus import PageTemplate , BaseDocTemplate , Frame <EOL> from reportlab . platypus import NextPageTemplate , Paragraph , PageBreak <EOL> from reportlab . platypus . tableofcontents import TableOfContents <EOL> from reportlab . lib . styles import getSampleStyleSheet , ParagraphStyle <EOL> from reportlab . rl_config import defaultPageSize <EOL> from reportlab . lib . units import inch <EOL> from reportlab . graphics import renderPDF <EOL> from pdfrw import PdfReader <EOL> from pdfrw . buildxobj import pagexobj <EOL> from pdfrw . toreportlab import makerl <EOL> PAGE_WIDTH = defaultPageSize [ <NUM_LIT:0> ] <EOL> PAGE_HEIGHT = defaultPageSize [ <NUM_LIT:1> ] <EOL> class MyTemplate ( PageTemplate ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pdf_template_filename , name = None ) : <EOL> frames = [ Frame ( <EOL> <NUM_LIT> * inch , <EOL> <NUM_LIT:0.5> * inch , <EOL> PAGE_WIDTH - <NUM_LIT> * inch , <EOL> PAGE_HEIGHT - ( <NUM_LIT> * inch ) <EOL> ) ] <EOL> PageTemplate . __init__ ( self , name , frames ) <EOL> page = PdfReader ( pdf_template_filename ) . pages [ <NUM_LIT:0> ] <EOL> self . page_template = pagexobj ( page ) <EOL> self . page_xscale = PAGE_WIDTH / self . page_template . BBox [ <NUM_LIT:2> ] <EOL> self . page_yscale = PAGE_HEIGHT / self . page_template . BBox [ <NUM_LIT:3> ] <EOL> def beforeDrawPage ( self , canvas , doc ) : <EOL> """<STR_LIT>""" <EOL> canvas . saveState ( ) <EOL> rl_obj = makerl ( canvas , self . page_template ) <EOL> canvas . scale ( self . page_xscale , self . page_yscale ) <EOL> canvas . doForm ( rl_obj ) <EOL> canvas . restoreState ( ) <EOL> class MyDocTemplate ( BaseDocTemplate ) : <EOL> """<STR_LIT>""" <EOL> def afterFlowable ( self , flowable ) : <EOL> """<STR_LIT>""" <EOL> if flowable . __class__ . __name__ == '<STR_LIT>' : <EOL> style = flowable . style . name <EOL> text = flowable . getPlainText ( ) <EOL> key = '<STR_LIT:%s>' % self . seq . nextf ( '<STR_LIT>' ) <EOL> if style == '<STR_LIT>' : <EOL> self . canv . bookmarkPage ( key ) <EOL> self . notify ( '<STR_LIT>' , [ <NUM_LIT:1> , text , self . page , key ] ) <EOL> def create_toc ( ) : <EOL> """<STR_LIT>""" <EOL> table_of_contents = TableOfContents ( ) <EOL> table_of_contents . dotsMinLevel = <NUM_LIT:0> <EOL> header1 = ParagraphStyle ( name = '<STR_LIT>' , fontSize = <NUM_LIT:16> , leading = <NUM_LIT:16> ) <EOL> header2 = ParagraphStyle ( name = '<STR_LIT>' , fontSize = <NUM_LIT> , leading = <NUM_LIT> ) <EOL> table_of_contents . levelStyles = [ header1 , header2 ] <EOL> return [ table_of_contents , PageBreak ( ) ] <EOL> def create_pdf ( filename , pdf_template_filename ) : <EOL> """<STR_LIT>""" <EOL> pdf_report = open ( filename , "<STR_LIT:wb>" ) <EOL> document = MyDocTemplate ( pdf_report ) <EOL> templates = [ MyTemplate ( pdf_template_filename , name = '<STR_LIT>' ) ] <EOL> document . addPageTemplates ( templates ) <EOL> styles = getSampleStyleSheet ( ) <EOL> elements = [ NextPageTemplate ( '<STR_LIT>' ) ] <EOL> elements . extend ( create_toc ( ) ) <EOL> for i in range ( <NUM_LIT:200> ) : <EOL> elements . append ( Paragraph ( "<STR_LIT>" + str ( i ) , styles [ '<STR_LIT>' ] ) ) <EOL> document . multiBuild ( elements ) <EOL> pdf_report . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> template , = sys . argv [ <NUM_LIT:1> : ] <EOL> output = '<STR_LIT>' + os . path . basename ( template ) <EOL> create_pdf ( output , template ) </s>
