Index: /main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py
===================================================================
--- /main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py	(revision 8111)
+++ /main/waeup.kofa/trunk/src/waeup/kofa/students/utils.py	(revision 8112)
@@ -29,10 +29,11 @@
 from reportlab.lib.pagesizes import A4
 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
-from reportlab.platypus import (Frame, Paragraph, Image,
-    Table, Spacer)
+from reportlab.platypus import (Frame, Paragraph, Image, PageBreak, Table,
+                                Spacer)
 from reportlab.platypus.tables import TableStyle
 from reportlab.platypus.flowables import PageBreak
 from zope.component import getUtility
 from zope.formlib.form import setUpEditWidgets
+
 from waeup.kofa.interfaces import IExtFileStore, IKofaUtils
 from waeup.kofa.interfaces import MessageFactory as _
@@ -57,4 +58,8 @@
     tag1 ='<font color=%s size=%d>' % (color, size)
     return tag1 + '%s:</font>'
+
+def trans(text, lang):
+    # shortcut
+    return translate(text, 'waeup.kofa', target_language=lang)
 
 def formatted_text(text, color=FONT_COLOR, size=FONT_SIZE):
@@ -125,5 +130,5 @@
         data_right.append([f_label,f_text])
     table_left = Table(data_left,style=SLIP_STYLE)
-    table_right = Table(data_right,style=SLIP_STYLE, colWidths=[6*cm, 8*cm])
+    table_right = Table(data_right,style=SLIP_STYLE, colWidths=[5*cm, 6*cm])
     table = Table([[table_left, table_right],],style=SLIP_STYLE)
     return table
@@ -152,4 +157,40 @@
     return table
 
+def docs_as_flowables(view, lang='en'):
+    """Create reportlab flowables out of scanned docs.
+    """
+    # XXX: fix circular import problem
+    from waeup.kofa.students.viewlets import FileManager
+    from waeup.kofa.browser import DEFAULT_IMAGE_PATH
+    from waeup.kofa.browser.pdf import NORMAL_STYLE, ENTRY1_STYLE
+    style = getSampleStyleSheet()
+    data = []
+
+    # Collect viewlets
+    fm = FileManager(view.context, view.request, view)
+    fm.update()
+    if fm.viewlets:
+        sc_translation = trans(_('Scanned Documents'), lang)
+        data.append(Paragraph(sc_translation, style["Heading3"]))
+        # Insert list of scanned documents
+        table_data = []
+        for viewlet in fm.viewlets:
+            f_label = Paragraph(trans(viewlet.label, lang), ENTRY1_STYLE)
+            img_path = getattr(getUtility(IExtFileStore).getFileByContext(
+                view.context, attr=viewlet.download_name), 'name', None)
+            f_text = Paragraph('[Yet not provided]', ENTRY1_STYLE)
+            if img_path is None:
+                pass
+            elif not img_path.endswith('.jpg'):
+                # reportlab requires jpg images, I think.
+                f_text = Paragraph('%s (Not displayable)' % (
+                    viewlet.title,), ENTRY1_STYLE)
+            else:
+                f_text = Image(img.name, width=2*cm, height=1*cm, kind='bound')
+            table_data.append([f_label, f_text])
+        if table_data:
+            # safety belt; empty tables lead to problems.
+            data.append(Table(table_data, style=SLIP_STYLE))
+    return data
 
 def insert_footer(pdf,width,style,text=None, number_of_pages=1):
@@ -237,5 +278,6 @@
             return
         end_level = certificate.end_level
-        if entry_session == grok.getSite()['configuration'].accommodation_session:
+        if entry_session == grok.getSite()[
+            'configuration'].accommodation_session:
             bt = 'fr'
         elif current_level >= end_level:
@@ -263,119 +305,50 @@
         """Render pdf slips for various pages.
         """
-        # (0,0),(-1,-1) = whole table
-        # (0,0),(0,-1) = first column
-        # (-1,0),(-1,-1) = last column
-        # (0,0),(-1,0) = first row
-        # (0,-1),(-1,-1) = last row
-
-        pdf = canvas.Canvas(filename,pagesize=A4)
-        pdf.setTitle(view.label)
-        pdf.setSubject(view.title)
-        pdf.setAuthor('%s (%s)' % (view.request.principal.title,
-            view.request.principal.id))
-        pdf.setCreator('WAeUP Kofa')
-        width, height = A4
+        # XXX: we have to fix the import problems here.
+        from waeup.kofa.browser.interfaces import IPDFCreator
+        from waeup.kofa.browser.pdf import NORMAL_STYLE, ENTRY1_STYLE
+        style = getSampleStyleSheet()
+        creator = getUtility(IPDFCreator)
+        data = []
+        doc_title = view.label
+        author = '%s (%s)' % (view.request.principal.title,
+                              view.request.principal.id)
         footer_text = view.label
         if getattr(student, 'student_id', None) is not None:
             footer_text = "%s - %s - " % (student.student_id, footer_text)
-        style = getSampleStyleSheet()
-        style.add(ParagraphStyle(name='Right', alignment=TA_RIGHT))
-        pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm))
-
-        story = []
-        frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm))
-        header_title = getattr(grok.getSite(), 'name', u'Sample University')
-        story.append(Paragraph(header_title, style["Heading1"]))
-        frame_header.addFromList(story,pdf)
 
         # Insert student data table
-        story = []
-        frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm))
-        story.append(Paragraph(view.label, style["Heading2"]))
         portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
         if student is not None:
-            #story.append(Spacer(1, 12))
-            bd_translation = translate(_('Base Data'),
-                'waeup.kofa', target_language=portal_language)
-            story.append(Paragraph(bd_translation, style["Heading3"]))
-            studenttable = render_student_data(studentview)
-            story.append(studenttable)
+            bd_translation = trans(_('Base Data'), portal_language)
+            data.append(Paragraph(bd_translation, style["Heading3"]))
+            data.append(render_student_data(studentview))
 
         # Insert widgets
-        story.append(Paragraph(view.title, style["Heading3"]))
-        set_up_widgets(view)
-        data = []
+        data.append(Paragraph(view.title, style["Heading3"]))
         portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
-        for widget in view.widgets:
-            f_label = '<font size=12>%s</font>:' % translate(
-                widget.label.strip(), 'waeup.kofa',
-                target_language=portal_language)
-            f_label = Paragraph(f_label, style["Normal"])
-            f_text = formatted_text(widget(), size=12)
-            f_text = Paragraph(f_text, style["Normal"])
-            data.append([f_label,f_text])
-        table = Table(data,style=SLIP_STYLE, colWidths=[5*cm, 13.5*cm])
-        story.append(table)
-
-        # Import browser components locally
-        # to avoid circular import
-        from waeup.kofa.students.viewlets import FileManager
-        from waeup.kofa.browser import DEFAULT_IMAGE_PATH
-        data = []
-        # Collect viewlets
-        fm = FileManager(view.context, view.request, view)
-        fm.update()
-        if fm.viewlets:
-            sc_translation = translate(_('Scanned Documents'), 'waeup.kofa',
-                target_language=portal_language)
-            story.append(Paragraph(sc_translation, style["Heading3"]))
-            # Insert list of scanned documents
-            for viewlet in fm.viewlets:
-                f_label = formatted_label(size=12) % translate(
-                viewlet.label, 'waeup.kofa',
-                target_language=portal_language)
-                f_label = Paragraph(f_label, style["Normal"])
-                if viewlet.template.__grok_name__ == 'imagedisplay':
-                    # In case we define FileDisplay viewlets with
-                    # an imagedisplay template in the customization package
-                    img = getUtility(IExtFileStore).getFileByContext(
-                        view.context, attr=viewlet.download_name)
-                    if img is None:
-                        img = open(DEFAULT_IMAGE_PATH, 'rb')
-                    doc_img = Image(img.name, width=2*cm, height=1*cm, kind='bound')
-                    data.append([f_label,doc_img])
-                else:
-                    f_text = formatted_text(viewlet.title, size=12)
-                    f_text = Paragraph(f_text, style["Normal"])
-                    data.append([f_label,f_text])
-            table = Table(data,style=SLIP_STYLE, colWidths=[5*cm, 13.5*cm])
-            story.append(table)
-
+        table = creator.getWidgetsTable(
+            view.form_fields, view.context, None, lang=portal_language)
+        data.append(table)
+
+        # Insert scanned docs
+        data.extend(docs_as_flowables(view, portal_language))
+
+        # Insert content table on second page
+        if tabledata and tableheader:
+            data.append(PageBreak())
+            data.append(Paragraph(view.content_title, style["Heading3"]))
+            contenttable = render_table_data(tableheader,tabledata)
+            data.append(contenttable)
+
+        view.response.setHeader(
+            'Content-Type', 'application/pdf')
         try:
-            frame_body.addFromList(story,pdf)
+            pdf_stream = creator.create_pdf(
+                data, None, doc_title, author=author, footer=footer_text)
         except IOError:
             view.flash('Error in image file.')
             return view.redirect(view.url(view.context))
-
-        if tabledata and tableheader:
-            insert_footer(pdf,width,style,footer_text,number_of_pages=2)
-        else:
-            insert_footer(pdf,width,style,footer_text)
-
-        # Insert content table on second page
-        if tabledata and tableheader:
-            pdf.showPage()
-            frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(2*cm))
-            story = []
-            story.append(Paragraph(view.content_title, style["Heading3"]))
-            #story.append(Spacer(1, 12))
-            contenttable = render_table_data(tableheader,tabledata)
-            story.append(contenttable)
-            frame_body.addFromList(story,pdf)
-            insert_footer(pdf,width,style,footer_text,number_of_pages=2)
-
-        view.response.setHeader(
-            'Content-Type', 'application/pdf')
-        return pdf.getpdfdata()
+        return pdf_stream
 
     VERDICTS_DICT = {
