【问题标题】:pdfencrypt with Reportlab使用 Reportlab 加密
【发布时间】:2021-11-28 05:29:31
【问题描述】:

我想为从 Django 项目中打开 pdf 文件添加密码保护。

def pdf_view(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="members.pdf"'
    elements = []
    doc = SimpleDocTemplate(response, rightMargin=0.5 * cm, leftMargin=6.5 * cm, topMargin=0.3 * cm, bottomMargin=0)
    rows = []
    users = User.objects.all()
    for user in users:
        rows.append((
            user.username,
            user.email,
            user.first_name,
            user.last_name,
            formats.date_format(user.date_joined, "SHORT_DATETIME_FORMAT"),
            formats.date_format(user.subscriptions.current_period_end, "SHORT_DATETIME_FORMAT")
        ))
    table = Table(rows, colWidths=46 * mm, rowHeights=30, repeatRows=1)
    table.setStyle([
        ('GRID', (0, 0), (-1, -1), 0.25, colors.black),
        ("ALIGN", (0, 0), (-1, -1), "LEFT"),
    ])
    table = Table(rows, colWidths=46 * mm, rowHeights=30, repeatRows=1)
    elements.append(table)
    doc.build(elements)
    return response

我在哪里可以添加这行代码来做加密

pdfencrypt.StandardEncryption("password", canPrint=0)

任何帮助将不胜感激

【问题讨论】:

标签: python django reportlab


【解决方案1】:

您可以添加如下代码行

def pdf_view(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="members.pdf"'
    elements = []
    **doc = SimpleDocTemplate(response, encrypt=pdfencrypt.StandardEncryption("pass", canPrint=0)**, rightMargin=0.5 * cm, leftMargin=6.5 * cm, topMargin=0.3 * cm, bottomMargin=0)
    rows = []
    users = User.objects.all()
    for user in users:
        rows.append((
            user.username,
            user.email,
            user.first_name,
            user.last_name,
            formats.date_format(user.date_joined, "SHORT_DATETIME_FORMAT"),
            formats.date_format(user.subscriptions.current_period_end, "SHORT_DATETIME_FORMAT")
        ))
    table = Table(rows, colWidths=46 * mm, rowHeights=30, repeatRows=1)
    table.setStyle([
        ('GRID', (0, 0), (-1, -1), 0.25, colors.black),
        ("ALIGN", (0, 0), (-1, -1), "LEFT"),
    ])
    table = Table(rows, colWidths=46 * mm, rowHeights=30, repeatRows=1)
    elements.append(table)
    doc.build(elements)
    return response

【讨论】:

    猜你喜欢
    • 2017-01-14
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    • 2018-12-26
    • 2012-01-01
    • 2011-02-12
    相关资源
    最近更新 更多