2012-01-11 72 views
-3

我能夠將HTML轉換爲PDF並將其存儲在本地目錄中。關鍵是如何在使用python創建主動創建之後下載相同的文件?如何使用python下載創建的PDF文件?

這是我的代碼:

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')) 
+0

你有沒有注意到[Generating PDF]的Django文檔索引中有明確的鏈接(https://docs.djangoproject.com/en/1.3/howto/outputting-pdf/)? – 2012-01-11 10:27:04

+0

您已經創建了一個文件,並且您想下載它。這很含糊。呃...使用'scp'?下載到哪裏? – Celada 2012-01-11 10:27:18

+0

塞拉達,我想通過盒子下載(當你下載zip文件,打開和另存爲的東西的時候會問你)?我知道它有點模糊......! – Lindi 2012-01-11 10:36:39

回答

0

您好我創辦瞭解決方案:

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') 

所以,現在我可以下載所創建的文件...