#!c:\python27\python.exe
print 'Content-type: text/html\n\n'
import datetime
import cgi
import cgitb
cgitb.enable()
import sys,os
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import PyPDF2
from PyPDF2 import PdfFileWriter,PdfFileReader

def drawbox(c,tdict):
   from  reportlab.lib.units import  inch
   # move the origin up and to the left
   c.translate(inch,inch)
   # define a large font
   c.setFont("Helvetica", 14)
   # choose some colors

   c.setStrokeColorRGB(0.2,0.5,0.3)
   c.setFillColorRGB(0.98,0.98,0.055)
   # draw some lines
   #c.line(0,0,0,1.7*inch)
   #c.line(0,0,1*inch,0)
   # draw a rectangle
   c.rect(0*inch,7.85*inch,3.5*inch,0.8*inch, fill=1)
   #horizpos,vertpos,width,height
   # make text go straight up
   #c.rotate(90)
   # change color
   c.setFillColorRGB(0,0,0.77)
   phone = tdict['agtphone']
   if len(phone) == 10:
       phone = '('+phone[0:3]+') '+phone[3:6]+'-'+phone[6:]
   c.drawString(0.1*inch, 8.44*inch, tdict['agency'])
   c.drawString(0.1*inch, 8.2*inch, phone)
   c.drawString(0.1*inch, 7.95*inch, tdict['agtemail'])

def mktemplate(agentnum,tdict):
    c = canvas.Canvas("template.pdf",pagesize=letter)

    drawbox(c,tdict)
    c.save()

def main():
    tform = cgi.FieldStorage()
    tdict = {}
    agentpath = tform.getvalue('agentpath')
    print 'agentpath '+agentpath
    agentlic = tform.getvalue('license')
    print 'agentpath '+agentpath

    tdict['agentpath'] = agentpath
    tdict['agtphone'] = tform.getvalue('phone')
    tdict['agtname'] = tform.getvalue('agentname')
    tdict['agency'] = tform.getvalue('agency')
    tdict['agtemail'] = tform.getvalue('email')
    tdict['agtlic'] = tform.getvalue('license')
    #tdict['agtnum'] = tform.getvalue('agentnum')
    print '<br>----tform----------<br>'
    print tdict['agentpath']
    print '<br>--------path------<br>'
    print tdict['agentpath']
    print '<br>--------------<br>'
    print tdict['agtphone']
    print '<br>--------------<br>'
    print tdict['agtname']
    print '<br>--------------<br>'
    print tdict['agency']
    print '<br>--------------<br>'
    print tdict['agtemail']
    print '<br>--------------<br>'
    print tdict['agtlic']
    print '<br>--------------<br>'
    #print tdict['agtnum']
    #print '<br>--------------<br>'
    print tdict
    print '<br>--------------<br>'

    template = mktemplate(agentpath,tdict)

    output = PdfFileWriter()
    input2 = PdfFileReader(file("template.pdf","rb"))
    input1 = PdfFileReader(file("/htdocs/brochures/ms/moo.pdf","rb"))



    stampinf = input2.getPage(0)
    watermark = PdfFileReader(file("template.pdf","rb"))
    #stampinf.mergePage(watermark.getPage(0))
    #output.addPage(page0)

    page1 = input1.getPage(0)
    page1.mergePage(watermark.getPage(0))
    output.addPage(page1)

    page2 = input1.getPage(1)
    output.addPage(page2)

    page3 = input1.getPage(2)
    output.addPage(page3)

    page4 = input1.getPage(3)
    output.addPage(page4)
    #---------------------
    page5 = input1.getPage(4)
    output.addPage(page5)

    page6 = input1.getPage(5)
    output.addPage(page6)

    page7 = input1.getPage(6)
    output.addPage(page7)

    page8 = input1.getPage(7)
    output.addPage(page8)

    page9 = input1.getPage(8)
    output.addPage(page9)

    page10 = input1.getPage(9)
    output.addPage(page10)




    page11 = input1.getPage(10)
    output.addPage(page11)

    page12 = input1.getPage(11)
    output.addPage(page12)

    outputStream = file(r"c:/htdocs/output.pdf","wb")
    output.write(outputStream)
    outputStream.close()


    rc = '<html><head>'
    rc = rc + '<META HTTP-EQUIV="Refresh" CONTENT="2; url=/output.pdf">';
    rc = rc + '</head><body>Reloading</body></html>';
    print rc

    #print 'end of file'


if __name__ == '__main__':
    main()
