1
def preview_badge(request, module_slug):
    event = request.event

    return  generate_badge_pdf(event, preview=True)


def generate_badge_pdf(event, preview=False):

    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'

    # set page layout variables
    pagesize = pagesizes.landscape(A4)
    pagewidth, pageheight = pagesize
    doc = platypus.BaseDocTemplate(filename=response, pagesize=pagesize,
            showBoundary=1)
    newHeight = doc.bottomMargin + doc.topMargin + doc.height
    newWidth = doc.leftMargin + doc.rightMargin + doc.width
    print __file__, 123, pagesize
    # reset margins
    doc.leftMargin = 0
    doc.bottomMargin = 0
    doc.rightMargin = 0
    doc.topMargin = 0

    # create the frames
    left_frame_1 = Frame(doc.leftMargin,
            doc.bottomMargin,
            newWidth / 3,
            newHeight / 6,
            showBoundary=1,
            id="left_frame_1")
    left_frame_2 = Frame(doc.leftMargin,
            doc.bottomMargin + newHeight / 6,
            newWidth / 3,
            newHeight / 6,
            showBoundary=1,
            id="left_frame_2")
    left_frame_3 = Frame(doc.leftMargin,
            doc.bottomMargin + newHeight / 6 * 2,
            newWidth / 3,
            newHeight / 6,
            showBoundary=1,
            id="left_frame_3")
    left_frame_4 = Frame(doc.leftMargin,
            doc.bottomMargin + newHeight / 6 * 3,
            newWidth / 3,
            newHeight / 6,
            showBoundary=1,
            id="left_frame_4")
    left_frame_5 = Frame(doc.leftMargin,
            doc.bottomMargin + newHeight / 6 * 4,
            newWidth / 3,
            newHeight / 6,
            showBoundary=1,
            id="left_frame_5")
    left_frame_6 = Frame(doc.leftMargin,
            doc.bottomMargin + newHeight / 6 * 5,
            newWidth / 3,
            newHeight / 6,
            showBoundary=1,
            id="left_frame_6")
    mid_frame_1 = Frame(doc.leftMargin + newWidth / 3,
            doc.bottomMargin,
            newWidth / 3,
            newHeight / 6 * 3,
            showBoundary=1,
            id="mid_frame_1")
    mid_frame_2 = Frame(doc.leftMargin + newWidth / 3,
            doc.bottomMargin + newHeight / 6 * 3,
            newWidth / 3,
            newHeight / 6 * 3,
            showBoundary=1,
            id="mid_frame_2")
    right_frame_1 = Frame(doc.leftMargin + newWidth / 3 * 2,
            doc.bottomMargin,
            newWidth / 3,
            newHeight / 6 * 2,
            showBoundary=1,
            id="right_frame_1")
    right_frame_2 = Frame(doc.leftMargin + newWidth / 3 * 2,
            doc.bottomMargin + newHeight / 6 * 2,
            newWidth / 3,
            newHeight / 6 * 3,
            showBoundary=1,
            id="right_frame_2")
    right_frame_3 = Frame(doc.leftMargin + newWidth / 3 * 2,
            doc.bottomMargin + newHeight / 6 * 5,
            newWidth / 3,
            newHeight / 6,
            showBoundary=1,
            id="right_frame_3")

    frames = [left_frame_1, left_frame_2, left_frame_3, left_frame_4,
            left_frame_5, left_frame_6, mid_frame_1, mid_frame_2,
            right_frame_1, right_frame_2, right_frame_3]


    # default content
    indentVal = 5
    fontSize = 16

    mid_2 = '''
    <b>NOTE</b><br /><br />
    '''

    right_2 = '''
    <br /><br />
    <b><font size="16">REGISTRATION Handy Kit</font></b> <br />
    '''

    styles = getSampleStyleSheet()
    Elements = []
    Logo = []

    # LEFT COL CONTENT
    Elements.append(Paragraph('''
        <para leftIndent="%d" fontSize="%d">%s</para>''' %
        (indentVal, fontSize, event.badgecontent.left_1_header,),
        styles['Normal']))
    Elements.append(Paragraph('''
        <para leftIndent="%d"><br />%s<br />%s<br />%s<br />%s</para>''' %
        (indentVal, event.badgecontent.left_1_line_1,
            event.badgecontent.left_1_line_2,
            event.badgecontent.left_1_line_3,
            event.badgecontent.left_1_line_4), styles['Normal'],))
    Elements.append(FrameBreak())

    Elements.append(Paragraph('''
        <para leftIndent="%d" fontSize="%d">%s</para>''' %
        (indentVal, fontSize, event.badgecontent.left_2_header,),
        styles['Normal']))
    Elements.append(Paragraph('''
        <para leftIndent="%d"><br />%s<br />%s<br />%s<br />%s</para>''' %
        (indentVal, event.badgecontent.left_2_line_1,
            event.badgecontent.left_2_line_2,
            event.badgecontent.left_2_line_3,
            event.badgecontent.left_2_line_4), styles['Normal'],))
    Elements.append(FrameBreak())

    Elements.append(Paragraph('''
        <para leftIndent="%d" fontSize="%d">%s</para>''' %
        (indentVal, fontSize, event.badgecontent.left_3_header,),
        styles['Normal']))
    Elements.append(Paragraph('''
        <para leftIndent="%d"><br />%s<br />%s<br />%s<br />%s</para>''' %
        (indentVal, event.badgecontent.left_3_line_1,
            event.badgecontent.left_3_line_2,
            event.badgecontent.left_3_line_3,
            event.badgecontent.left_3_line_4), styles['Normal'],))
    Elements.append(FrameBreak())

    Elements.append(Paragraph('''
        <para leftIndent="%d" fontSize="%d">%s</para>''' %
        (indentVal, fontSize, event.badgecontent.left_3_header,),
        styles['Normal']))
    Elements.append(Paragraph('''
        <para leftIndent="%d"><br />%s<br />%s<br />%s<br />%s</para>''' %
        (indentVal, event.badgecontent.left_4_line_1,
            event.badgecontent.left_4_line_2,
            event.badgecontent.left_4_line_3,
            event.badgecontent.left_4_line_4), styles['Normal'],))
    Elements.append(FrameBreak())

    Elements.append(Paragraph('''
        <para leftIndent="%d" fontSize="%d">%s</para>''' %
        (indentVal, fontSize, event.badgecontent.left_5_header,),
        styles['Normal']))
    Elements.append(Paragraph('''
        <para leftIndent="%d"><br />%s<br />%s<br />%s<br />%s</para>''' %
        (indentVal, event.badgecontent.left_5_line_1,
            event.badgecontent.left_5_line_2,
            event.badgecontent.left_5_line_3,
            event.badgecontent.left_5_line_4), styles['Normal'],))
    Elements.append(FrameBreak())

    Elements.append(Paragraph('''
        <para leftIndent="%d" fontSize="%d">%s</para>''' %
        (indentVal, fontSize, event.badgecontent.left_6_header,),
        styles['Normal']))
    Elements.append(Paragraph('''
        <para leftIndent="%d"><br />%s<br />%s<br />%s<br />%s</para>''' %
        (indentVal, event.badgecontent.left_6_line_1,
            event.badgecontent.left_6_line_2,
            event.badgecontent.left_6_line_3,
            event.badgecontent.left_6_line_4), styles['Normal'],))
    Elements.append(FrameBreak())

    # MID COL CONTENT
    Elements.append(Paragraph('''
        <para leftIndent="%d">%s</para>
        ''' % (indentVal, event.badgecontent.mid_1), styles['Normal']))
    Elements.append(FrameBreak())
    Elements.append(Paragraph('''
        <para leftIndent="%d">%s</para>
        ''' % (indentVal, mid_2), styles['Normal']))
    Elements.append(FrameBreak())

    # RIGHT COL CONTENT
    Elements.append(Paragraph('''
        <para fontSize="14"><img src="%s" />%s<br /><br /><br /><br /><br />
        </para>
        ''' % ('http://127.0.0.1:8000/media/tmplCR84D.jpg', event.name), styles['Normal']))
    Elements.append(Paragraph('''
        <para align="center" fontSize="20">%s<br /><br /></para>
        ''' % ('John Doe'), styles['Normal']))
    Elements.append(Paragraph('''
        <para align="center" fontSize="16">%s</para>
        ''' % ('Nanyang Technological University'),
        styles['Normal']))
    Elements.append(FrameBreak())

    Elements.append(Paragraph('''
        <para leftIndent="%d">%s</para>
        ''' % (indentVal, right_2), styles['Normal']))
    Elements.append(FrameBreak())

    Elements.append(Paragraph('''
        <para align="center" fontSize="18">%s</para>
        ''' % ('REG ID: TEST-R-1234'),
        styles['Normal']))
    Elements.append(Paragraph('''
        <para align="center" fontSize="14"><br /> %s<br /></para>
        ''' % ('John Doe'),
        styles['Normal']))
    Elements.append(Paragraph('''
        <para align="center"><br />%s<br /> %s <br /> </para>''' %
        ('Category: Test', '(This portion will be retained by conference staff)'), styles['Normal'],))
    Elements.append(FrameBreak())

    doc.addPageTemplates(platypus.PageTemplate(id="TwoCol", frames=frames))
    doc.build(Elements)

    return response

这个特定的部分失败了

Elements.append(Paragraph('''
            <para fontSize="14"><img src="%s" />%s<br /><br /><br /><br /><br />
            </para>
            ''' % ('http://127.0.0.1:8000/media/tmplCR84D.jpg', event.name), styles['Normal']))

当我在浏览器中加载页面时,它只是进入永久的“等待页面加载”模式,而没有实际加载页面。我知道我的其余代码有效,因为当我取出img标签时,我可以在页面中加载 pdf。

我也确定 urlhttp://127.0.0.1:8000/media/tmplCR84D.jpg显示图像。

4

0 回答 0