我使用django-wkhtmltopdf生成pdf报告。然后报告下载到客户端计算机。我想在服务器上留下它的副本。我如何将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 :(得分:-1)
尝试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()