Ignore:
Timestamp:
19 Dec 2011, 12:28:40 (13 years ago)
Author:
uli
Message:

Use PDF adapter for creating PDFs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/browser.py

    r7380 r7392  
    2323from time import time
    2424from datetime import datetime, date
    25 from zope.component import getUtility, createObject
     25from zope.component import getUtility, createObject, getAdapter
    2626from zope.formlib.form import setUpEditWidgets
    2727from hurry.workflow.interfaces import (
     
    5050from waeup.sirp.browser.viewlets import ManageActionButton, PrimaryNavTab
    5151from waeup.sirp.interfaces import (
    52     ISIRPObject, ILocalRolesAssignable, IExtFileStore,
     52    ISIRPObject, ILocalRolesAssignable, IExtFileStore, IPDF,
    5353    IFileStoreNameChooser, IPasswordValidator, IUserAccount, ISIRPUtils)
    5454from waeup.sirp.permissions import get_users_with_local_roles
     
    707707
    708708    def render(self):
    709         # To recall the table coordinate system:
    710         # (0,0),(-1,-1) = whole table
    711         # (0,0),(0,-1) = first column
    712         # (-1,0),(-1,-1) = last column
    713         # (0,0),(-1,0) = first row
    714         # (0,-1),(-1,-1) = last row
    715 
    716         SLIP_STYLE = TableStyle(
    717             [('VALIGN',(0,0),(-1,-1),'TOP')]
    718             )
    719 
    720         pdf = canvas.Canvas('application_slip.pdf',pagesize=A4)
    721         pdf.setTitle(self.label)
    722         pdf.setSubject('Application')
    723         pdf.setAuthor('%s (%s)' % (self.request.principal.title,
    724             self.request.principal.id))
    725         pdf.setCreator('SIRP SIRP')
    726         width, height = A4
    727         style = getSampleStyleSheet()
    728         pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm))
    729 
    730         story = []
    731         frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm))
    732         header_title = getattr(grok.getSite(), 'name', u'Sample University')
    733         story.append(Paragraph(header_title, style["Heading1"]))
    734         frame_header.addFromList(story,pdf)
    735 
    736         story = []
    737         frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm))
    738         story.append(Paragraph(self.label, style["Heading2"]))
    739         story.append(Spacer(1, 18))
    740         for msg in self.context.history.messages:
    741             f_msg = '<font face="Courier" size=10>%s</font>' % msg
    742             story.append(Paragraph(f_msg, style["Normal"]))
    743         story.append(Spacer(1, 24))
    744         # Setup table data
    745         data = []
    746         # Insert passport photograph
    747         img = getUtility(IExtFileStore).getFileByContext(self.context)
    748         if img is None:
    749             img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb')
    750         doc_img = Image(img.name, width=4*cm, height=3*cm, kind='bound')
    751         data.append([doc_img])
    752         data.append([Spacer(1, 18)])
    753         # Render widget fields
    754         self.setUpWidgets()
    755         for widget in self.widgets:
    756             f_label = '<font size=12>%s</font>:' % widget.label.strip()
    757             f_label = Paragraph(f_label, style["Normal"])
    758             f_text = '<font size=12>%s</font>' % widget()
    759             f_text = Paragraph(f_text, style["Normal"])
    760             data.append([f_label,f_text])
    761         course_admitted = self.getCourseAdmitted()
    762         f_label = '<font size=12>Admitted Course of Study:</font>'
    763         f_text = '<font size=12>%s</font>' % course_admitted
    764         f_label = Paragraph(f_label, style["Normal"])
    765         f_text = Paragraph(f_text, style["Normal"])
    766         data.append([f_label,f_text])
    767 
    768         course_admitted = self.context.course_admitted
    769         if getattr(course_admitted, '__parent__',None):
    770             f_label = '<font size=12>Department:</font>'
    771             f_text = '<font size=12>%s</font>' % (
    772                 course_admitted.__parent__.__parent__.longtitle())
    773             f_label = Paragraph(f_label, style["Normal"])
    774             f_text = Paragraph(f_text, style["Normal"])
    775             data.append([f_label,f_text])
    776 
    777             f_label = '<font size=12>Faculty:</font>'
    778             f_text = '<font size=12>%s</font>' % (
    779                 course_admitted.__parent__.__parent__.__parent__.longtitle())
    780             f_label = Paragraph(f_label, style["Normal"])
    781             f_text = Paragraph(f_text, style["Normal"])
    782             data.append([f_label,f_text])
    783 
    784         # Create table
    785         table = Table(data,style=SLIP_STYLE)
    786         story.append(table)
    787         frame_body.addFromList(story,pdf)
    788 
    789         story = []
    790         frame_footer = Frame(1*cm,0,width-(2*cm),1*cm)
    791         timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
    792         f_text = '<font size=10>%s</font>' % timestamp
    793         story.append(Paragraph(f_text, style["Normal"]))
    794         frame_footer.addFromList(story,pdf)
    795 
     709        pdfstream = getAdapter(self.context, IPDF, name='application_slip')(
     710            view=self)
    796711        self.response.setHeader(
    797712            'Content-Type', 'application/pdf')
    798         return pdf.getpdfdata()
     713        return pdfstream
    799714
    800715class ApplicantManageActionButton(ManageActionButton):
Note: See TracChangeset for help on using the changeset viewer.