2015-10-20 96 views
2

我使用django-wkhtmltopdf生成PDF報告。該報告然後下載到客戶端機器。我想將其副本留在服務器上。我如何將pdf直接保存到服務器上。Django:將pdf保存到服務器而不是客戶端

我的繼承人PDF查看:

class PDFView(DetailView): 
    template = 'pdf_reports/report.html' 
    today = datetime.now() 
    context = {'today': today.strftime('%d %b %Y')} 
    model = Model 

    def get(self, request, *args, **kwargs): 
     self.context['model'] = self.get_object() 

     response=PDFTemplateResponse(request=request, 
            template=self.template, 
            filename ="report.pdf", 


     context=self.context, 
           show_content_in_browser=False, 
           cmd_options={'margin-top': 0, 
               'margin-left': 0, 
               'margin-right': 0} 
           ) 
    return response 
+0

我想像你保存響應的方式與你[fileupload]相同(https://docs.djangoproject.com/en/1.8/topics/http/file-upload s /#basic-file-uploads)我還沒試過這個.. – Sayse

回答

0

嘗試pdfkit,它也使用wkhtmltopdf

import pdfkit 

pdfkit.from_url('http://google.com', 'out.pdf') 

要保存在一個模型,試試這個:

import tempfile 
import pdfkit 

from django.core.files.base import File 

# instance is your model 

with tempfile.NamedTemporaryFile() as fh: 
    pdfkit.from_url('http://google.com', fh.name) 
    instance.pdf = File(fh) 
    instance.save() 
+0

'instance'定義在哪裏? – raratiru

相關問題