我想在django web-framework中将一个html页面转换为pdf,除了图像之外,它的工作效果很好。
以下是HTML页面的小片段,我想将其转换为pdf
<div class="certi-description"> <img src="/media/certificate/icons/2.jpg" style="width: 12%;height: 25%;position: absolute; left: {{tag_logo_offset.0|add:'-355'}}px; top: {{tag_logo_offset.1|add:'-170'}}px;"><br>
<div style="position: absolute; left: {{tag_name_offset.0|add:'-355'}}px; top: {{tag_name_offset.1|add:'-170'}}px;"><div id="tag" ><center><font size="3" color="red">{{ tag_name}}</font><center></div></div><br><br>
<div style="position: absolute; left: {{comm_offset.0|add:'-355'}}px; top: {{comm_offset.1|add:'-170'}}px;"><span><font size="5" color="black">is hereby Rewarded to</font></span></div><br>
以下是views.py
中的代码段 template = certi.generic_certificate.template_name
template_new = get_template(template)
context_dict = Context(context)
html = template_new.render(context_dict)
print html
result = StringIO.StringIO()
links = lambda uri, rel: os.path.join(base.MEDIA_ROOT, uri.replace(base.MEDIA_ROOT, ''))
print base.MEDIA_ROOT
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")),dest=result, link_callback=links)
if not pdf.err:
converted_pdf= HttpResponse(result.getvalue(), content_type='application/pdf')
converted_pdf['Content-Disposition'] = 'attachment; filename="certificate%s.pdf"'%certi_id
p = canvas.Canvas(converted_pdf)
p.showPage()
p.save()
pdf = result.getvalue()
result.close()
myfile = ContentFile(pdf)
Certificate.objects.filter(pk=certi_id).update(pdf_template = p)
converted_pdf.write(pdf)
return converted_pdf
base.py作为我的设置文件:
MEDIA_ROOT = normpath(join(DJANGO_ROOT, 'media'))
MEDIA_URL = '/media/'
########## END MEDIA CONFIGURATION
AUTHENTICATION_BACKENDS = ('custom.backends.EmailOrUsernameModelBackend',)
########## STATIC FILE CONFIGURATION
STATIC_ROOT = normpath(join(DJANGO_ROOT, 'final_static'))
STATIC_URL = '/static/'
我在/ media / certificate / icons文件夹中有几个图像,比如2.jpg,我在我的html中使用
Everthing正在转换为pdf,但图片除外。我检查了所有可能的路径,但仍然无法正常工作。
请帮助解决此问题。