我认为这对你有用:
def fetch_resources(uri, rel):
if uri.startswith(settings.MEDIA_URL):
path = os.path.join(settings.MEDIA_ROOT,uri.replace(settings.MEDIA_URL, ""))
elif uri.startswith(settings.STATIC_URL):
path = os.path.join(settings.LOADING_STATIC_FOR_PDF,uri.replace(settings.STATIC_URL, ""))
return path
def preview_pdf(request):
context = RequestContext(request)
if request.method == 'GET':
context = {
# sending data to html
}
rendered_html = render_to_string("path/xxx.html", locals())
template = get_template('path/xxx.html')
html = template.render(context)
result = StringIO.StringIO()
file = open('test.pdf', "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8',link_callback=fetch_resources)
file.seek(0)
pdf = file.read()
file.close()
return HttpResponse(pdf, 'application/pdf')