我想在我的 django 專案中生成 pdf。我使用 pdfkit 模塊將 html 頁面轉換為 pdf 檔案。我使用這個函式,但它有錯誤,比如: OSError Exception Value:
No wkhtmltopdf executable found: "b''" 如果這個檔案存在,請檢查這個行程是否可以讀取它或者你可以在方法呼叫中手動傳遞路徑給它,檢查自述檔案。否則請安裝 wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
def customer_render_pdf_view(request, *args, **kwargs):
pk = kwargs.get('pk')
customer = get_object_or_404(Person, pk=pk)
enrolment = get_object_or_404(Enrollment,pk=pk)
template_path = 'test.html'
context = {'customer': customer, 'enrolment':enrolment}
template = get_template(template_path)
html = template.render(context)
pdf = pdfkit.from_string(html, False)
response = HttpResponse(pdf, content_type='application/pdf' )
response['Content-Disposition'] = 'filename= "report.pdf"'
return response
uj5u.com熱心網友回復:
你可以這樣使用
匯入報告實驗室和pdf
uj5u.com熱心網友回復:
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.template.loader import get_template
import pdfkit
from .models import Buses
def pdf(request, id):
bus = Buses.objects.get(id=id)
template = get_template('buses/pdf.html')
html = template.render({'bus': bus})
options = {
'page-size': 'Letter',
'encoding': "UTF-8",
}
config =
pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
#use your actual location here
pdf = pdfkit.from_string(html, False, configuration=config,
options=options)
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="
{}_{}.pdf"'.format(bus.company, bus.name)
return response
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371715.html
