【发布时间】:2018-06-12 20:16:57
【问题描述】:
我正在尝试为当前页面(服务详细信息,这是一个动态页面)创建一个“转换为 pdf”按钮我尝试使用 ReportLab(正如 Django 文档所建议的那样,但我无法让它工作,并且 ReportLab 文档没有提及这种可能性)
现在我可以从这个视图中创建一个 pdf 文件:(编辑,为了清楚起见,回到 django 文档中的代码) 视图.py
@login_required
def service_detail(request, pk):
service = get_object_or_404(Service, pk=pk)
return render(request, 'detail.html', {'service':service, 'pk':pk})
@login_required
def render_to_pdf(request):
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="service.pdf"''
# Create the PDF object, using the response object as its "file."
p = canvas.Canvas(response)
# Draw things on the PDF. Here's where the PDF generation happens.
p.drawString("Hello world.")
# Close the PDF object cleanly, and we're done.
p.showPage()
p.save()
return response
urls.py
url(r'^service/(?P<pk>\d+)/$', views.service_detail, name='service_detail'), #Services details
url(r'^render_to_pdf/', views.render_to_pdf, name='render_to_pdf'),
服务详情的模板包括动态元素,例如:
Your location: {{ service.user_location }} <br><br>
任何人都知道我可以做什么,或者我可以使用什么其他技术来创建这样的 PDF?
我主要使用 Python、Django、HTML 和 CSS
【问题讨论】:
-
“我只是无法让它工作”是对您问题的完全无用的描述。请解释如何它“不起作用”。