生成带有附件的受保护Django网页的PDF [英] Generate PDF of Protected Django Webpage with Attachments

查看:86
本文介绍了生成带有附件的受保护Django网页的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试生成Django Web应用程序中具有的视图的PDF.此视图受保护,这意味着用户必须登录并具有查看页面的特定权限.我也有一些附件(以FileFields的形式存储在数据库中),我想将它们附加到PDF的末尾.

So I'm trying to generate a PDF of a view that I have in a django web application. This view is protected, meaning the user has to be logged in and have specific permission to view the page. I also have some attachments (stored in the database as FileFields) that I would like to append to the end of the PDF.

我已经阅读了大多数有关如何使用pdfkitreportlab从网页生成PDF的文章,但是由于某些原因,所有这些文章都因我而失败了.

I've read most of the posts I could find on how to generate PDFs from a webpage using pdfkit or reportlab, but all of them fail for me for some reason or another.

当前,我所获得的最接近的结果是使用pdfkit成功生成了页面的PDF,但这需要我消除那些要求用户登录并具有页面权限的限制,但这实际上不是长期的选择.我发现了几篇文章,讨论了在受保护的页面上打印pdf文件并提供登录信息,但是我无法使用其中的任何内容.

Currently, the closest I've gotten is successfully generating a PDF of the page using pdfkit, but this requires me to remove the restrictions that require the user to be logged in and have page permissions, which really isn't an option long term. I found a couple posts that discuss printing pdfs on protected pages and providing login information, but I couldn't get any of that to work.

关于如何包括附件,我还没有发现任何东西,也不知道从哪里开始.

I haven't found anything on how to include attachments, and don't really know where to start with that.

如果有需要,我很乐意使用更多信息或代码片段来更新此问题,但是这里有很多动人的部分,我不想向人们灌输无用的信息.让我知道我是否应该提供其他信息,并在此先感谢您的帮助.

I'm more than happy to update this question with more information or snippets of code if need be, but there's quite a few moving parts here and I don't want to flood people with useless information. Let me know if there's any other information I should provide, and thanks in advance for any help.

推荐答案

我成功了!通过将PyPDF2和pdfkit结合使用,我可以使它非常简单地工作.它可以在受保护的页面上工作,因为django负责将完整的html作为字符串获取,我只是将其传递给pdfkit.它也支持附加附件,但是我怀疑(尽管我没有测试过)它是否可以和pdfs一起使用.

I got it working! Through a combination of PyPDF2 and pdfkit, I got this to work pretty simply. It works on protected pages because django takes care of getting the complete html as a string, which I just pass to pdfkit. It also supports appending attachments, but I doubt (though I haven't tested) that it works with anything other than pdfs.

from django.template.loader import get_template
from PyPDF2 import PdfFileWriter, PdfFileReader
import pdfkit

def append_pdf(pdf, output):
    [output.addPage(pdf.getPage(page_num)) for page_num in range(pdf.numPages)]


def render_to_pdf():
    t = get_template('app/template.html')
    c = {'context_data': context_data}

    html = t.render(c)
    pdfkit.from_string(html, 'path/to/file.pdf')

    output = PdfFileWriter()
    append_pdf(PdfFileReader(open('path/to/file.pdf', "rb")), output)

    attaches = Attachment.objects.all()

    for attach in attaches:
        append_pdf(PdfFileReader(open(attach.file.path, "rb")), output)

    output.write(open('path/to/file_with_attachments.pdf', "wb"))

这篇关于生成带有附件的受保护Django网页的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆