2015-10-20 66 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

相关问题