如何在python中下载创建的PDF文件?

2024-04-27 03:55:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我能够将HTML转换成PDF格式,并将其存储在本地目录中。问题是,在使用python以编程方式创建了同一个文件之后,如何下载它?在

这是我的代码:

filename = "test.pdf"
        body= fromstring( u"<div>" + tostring( fromstring(   str(self.request.post['content']).decode('utf-8') ).body ) + u"</div>" )
        tx = tostring(body).encode('utf-8')
        pisa.CreatePDF(tx, file(filename, 'wb'))

Tags: 文件代码div目录pdfhtml格式编程
1条回答
网友
1楼 · 发布于 2024-04-27 03:55:23

嗨,我找到了解决方案:

    fh = open('path_to_file','r')
    self._response.data = fh.read()
    fh.close() 

    self._response.headers.add('Content-Type', 'application/pdf')
    self._response.headers.add('Content-Disposition', 'attachment')

所以现在我可以下载创建的文件。。。在

相关问题 更多 >