Attachment 'PdfLetter.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3   MoinMoin - Processor PdfLetter
   4     
   5   @copyright: 2004 by Fredrik Björnsson <info@cleosan.com>
   6   @license: GNU GPL.
   7 
   8   Changes:
   9     2004-10-07 - Started
  10     2004-10-07 - Fixed indention to 4 spaces and a small bug
  11   
  12 """
  13                 
  14                 
  15 Dependencies = []
  16 
  17 import os,StringIO
  18 from MoinMoin import config
  19 from MoinMoin.action import AttachFile
  20 from reportlab.platypus import KeepTogether, Spacer, SimpleDocTemplate, Table, Paragraph, TableStyle, Frame,PageBreak
  21 from reportlab.lib.pagesizes import A4
  22 from reportlab.lib.units import cm
  23 from reportlab.lib import colors
  24 from reportlab.lib.enums import *
  25 from reportlab.lib.styles import getSampleStyleSheet,ParagraphStyle
  26 
  27 
  28   
  29 def process(request, formatter, lines):
  30     """ the process starts here """
  31     del lines[0]
  32     keys=['TO','FROM','DATE','SUBJECT','BODY','SIGNATURE','INCLUDED']
  33     args=parseLines(keys,lines)
  34   
  35     # make the html
  36     request.write("""<div style="text-align:right;"><a target=_new href="?action=AttachFile&do=get&target=report.pdf">Get Pdf</a></div>""")
  37     request.write(formatter.rawHTML("<hr><div style='font-size:24px;text-align:center;'>"+"<br>".join(args['FROM']))+"</div><hr>")
  38     request.write(formatter.rawHTML("<div style='font-size:20px;text-align:right;'>"+"".join(args['DATE']))+"</div>")
  39     request.write(formatter.rawHTML("<div style='font-size:20px;text-align:left;margin-left:50px;'>"+"<br>".join(args['TO']))+"</div>")
  40     request.write(formatter.rawHTML("<div style='font-size:20px;text-align:left;margin-left:50px;margin-top:30px;'>"+"".join(args['SUBJECT']))+"</div>")
  41     request.write(formatter.rawHTML("<div style='font-size:20px;text-align:left;margin-left:50px;margin-top:10px;'>"+"<br>".join(args['BODY']))+"</div>")
  42     request.write(formatter.rawHTML("<div style='font-size:20px;text-align:left;margin-left:50px;margin-top:30px;'>"+"<br>".join(args['SIGNATURE']))+"</div>")
  43     request.write(formatter.rawHTML("<div style='font-size:20px;text-align:left;margin-left:50px;margin-top:30px;'>"+"<br>".join(args['INCLUDED']))+"</div>")
  44   
  45     # make the pdf
  46     attDir=AttachFile.getAttachDir(formatter.page.page_name,create=1)+'/'
  47     open(attDir+'report.pdf','w').write(makeLetterPdf(args))
  48  
  49  
  50 #
  51 # Pdf function
  52 #
  53 
  54 def makeLetterPdf(args):
  55     """ Makes the pdf letter """
  56     tableStyle=[
  57       ('ALIGN', (0,0), (-1,-1), 'LEFT'),
  58       ('FONT', (0,0), (-1,-1),'Helvetica', 12),
  59       ('ALIGN', (0,0), (0,0), 'CENTER'),
  60       ('FONT', (0,0), (0,0),'Helvetica', 14),    
  61       ('LINEABOVE', (0,0), (0,0), 1, colors.black),
  62       ('LINEBELOW', (0,0), (0,0), 1, colors.black),
  63       ('ALIGN', (0,1), (0,1), 'RIGHT'),
  64       ]
  65     bodyRows=[]
  66     bodyStyle=getNormalStyle(size=12)
  67     for line in args['BODY']:
  68         bodyRows.append([Paragraph(line,bodyStyle)])
  69     
  70     tableData=[
  71     ["\n".join(args['FROM'])],
  72     ["".join(args['DATE'])],
  73     [""],
  74     ["\n".join(args['TO'])],
  75     [""],
  76     ["".join(args['SUBJECT'])]
  77     ]+bodyRows+[
  78     [""],
  79     ["\n".join(args['SIGNATURE'])],
  80     [""],
  81     ["\n".join(args['INCLUDED'])]
  82     ]
  83     
  84     tableColumn=[17*cm]
  85     table = Table(tableData,tableColumn,rowHeights=None,splitByRow=1)
  86     table.setStyle(TableStyle(tableStyle))
  87     table.hAlign = "LEFT"
  88     temp = StringIO.StringIO()
  89     doc = SimpleDocTemplate(temp, pagesize=A4, showBoundary=0, leftMargin=1.5*cm, rightMargin=1.5*cm, topMargin=1.5*cm, bottomMargin=1.5*cm)
  90     doc.build([table])
  91     return temp.getvalue()
  92   
  93 
  94 
  95 #
  96 # Help functions
  97 #
  98 
  99 def parseLines(keys,lines):
 100     """  Parses the lines, and returns a dict  """
 101     found_key,args,tmp='',{},[]
 102     for line in lines:
 103         split=line.split('=')
 104         if len(split)>1 and split[0].strip() in keys:
 105             args[found_key]=tmp
 106             found_key,tmp=split[0].strip(),[]
 107             if split[1].strip()!='':
 108                 tmp.append(split[1].strip())  
 109         else:
 110           tmp.append(line)  
 111     args[found_key]=tmp
 112     for key in keys:
 113         if not args.has_key(key):
 114             args[key]=['']
 115         elif len(args[key])>1 and args[key][-1]=='':
 116             del args[key][-1] 
 117     return args
 118 
 119 def getNormalStyle(align="left",font="Helvetica",size=11,leading=14.4):
 120     """ get a normal style to use with paragraph """
 121     # Normal paragraph style
 122     pNormal = ParagraphStyle('Normal',None)
 123     pNormal.fontName = font
 124     pNormal.fontSize = size
 125     pNormal.leading = leading
 126     if align=="left":
 127         pNormal.alignment = TA_LEFT
 128     elif align=="right":
 129         pNormal.alignment = TA_RIGHT
 130     else:
 131         pNormal.alignment = TA_CENTER
 132     return pNormal
 133                                                             

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-10-07 15:39:46, 4.6 KB) [[attachment:PdfLetter.py]]
  • [get | view] (2004-10-06 22:20:14, 30.8 KB) [[attachment:example_pdf.jpg]]
  • [get | view] (2004-10-06 22:20:43, 3.1 KB) [[attachment:example_pdf.pdf]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.