我正在使用reporlab在我的django项目中生成pdf。我有多个数据要使用reportlab以表格格式存储。我创建了一个表。但该表只显示第一个数据,表行中没有进一步的数据更新。
桌子看起来像:
检查|身份证|牌照|图片|评论
100 |测试|http://www.test.com/image.jpg| Qwerty
代码段如下:

from django.http import HttpResponse
from rest_framework import generics
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
from reportlab.lib import colors

class DamageExportViewSet(generics.ListAPIView):

renderer_classes = [PDFRenderer,]

def get(self, request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="Damage Report File.pdf"'
    width, height = A4
    styles = getSampleStyleSheet()
    styleN = styles["BodyText"]
    styleN.alignment = TA_LEFT
    styleBH = styles["Normal"]
    styleBH.alignment = TA_CENTER

    def coord(x, y, unit=1):
        x, y = x * unit, height - y * unit
        return x, y

    inspection = Paragraph('''<b>Inspection Id</b>''', styleBH)
    licplt = Paragraph('''<b>Licence Plate</b>''', styleBH)
    imgs = Paragraph('''<b>Images</b>''', styleBH)
    cmnts = Paragraph('''<b>Comments</b>''', styleBH)

    buffer = BytesIO()

    p = canvas.Canvas(buffer, pagesize=A4)

    p.drawString(20, 800, "Report generated at " + timezone.now().strftime('%b %d, %Y %H:%M:%S'))

    damage_data = Damage.objects.all()
    try:
        for i in damage_data:
            inspection_data = str(i.inspection_id).encode('utf-8')
            licence_plate = str(i.inspection.vehicle.licence_plate).encode('utf-8')
            images = str(i.image).encode('utf-8')
            comments = str(i.comment).encode('utf-8')
            inspcdata = Paragraph(inspection_data, styleN)
            lncplt = Paragraph(licence_plate, styleN)
            img = Paragraph(images, styleN)
            cmt = Paragraph(comments, styleN)
            data = [[inspection, licplt, imgs, cmnts],
                    [inspcdata, lncplt, img, cmt]]

    except:
        pass
    table = Table(data, colWidths=[4 * cm, 4 * cm, 5 * cm, 4 * cm])

    table.setStyle(TableStyle([
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
    ]))
    table.wrapOn(p, width, height)
    table.wrapOn(p, width, height)
    table.drawOn(p, *coord(1.8, 9.6, cm))
    p.showPage()
    p.save()
    pdf = buffer.getvalue()
    buffer.close()
    response.write(pdf)
    return response

我不知道如何在表格中显示损伤模型的所有数据。图像数据应该作为超链接。
提前谢谢:)

最佳答案

您将在每个循环步骤覆盖data数组。
你需要像这样:

data=[
    ["Inspection_Id", "Licence_Plate", "Images", "Comment"],
    ["100", "TEST", "http://url.to/png", "Qwerty"],
    ["200", "2nd data row", "http://url.to/gif", "Dvorak"]
]

这样你就可以得到
| Inspection_Id | Licence_Plate | Images            | Comment |
| ------------- |-------------- | ----------------- | ------- |
| 100           | TEST          | http://url.to/png | Qwerty  |
| 200           | 2nd data row  | http://url.to/gif | Dvorak  |

因此,循环需要如下所示:
# Fill the first row of `data` with the heading, only once!
data = [[inspection, licplt, imgs, cmnts]]
try:
    for i in damage_data:
        inspection_data = str(i.inspection_id).encode('utf-8')
        licence_plate = str(i.inspection.vehicle.licence_plate).encode('utf-8')
        images = str(i.image).encode('utf-8')
        comments = str(i.comment).encode('utf-8')
        inspcdata = Paragraph(inspection_data, styleN)
        lncplt = Paragraph(licence_plate, styleN)
        img = Paragraph(images, styleN)
        cmt = Paragraph(comments, styleN)

        # Add this loop's step row into data array
        data += [inspcdata, lncplt, img, cmt]

应该是这样:)
图像数据应该作为超链接。
你这是什么意思?
干杯!

关于python - 使用reportlab Django在表中添加行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43345494/

10-09 02:52