我正在从html创建pdf并且它被转换但没有图像。我也试过绝对的网址,但它仍然没有用 pdf功能:
def test_pdf(request):
template = get_template('../templates/index.html')
html = template.render(Context(data))
filename = 'pdfs/'+str(random.random())+'.pdf'
file = open(filename, "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8',link_callback=link_callback)
# Return PDF document through a Django HTTP response
file.seek(0)
pdf = file.read()
file.close() # Don't forget to close the file handle
return HttpResponse({"success":"success"})
def link_callback(uri, rel):
sUrl = settings.STATIC_URL # Typically /static/
sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/
mUrl = settings.MEDIA_URL # Typically /static/media/
mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/
if uri.startswith(mUrl):
path = os.path.join(mRoot, uri.replace(mUrl, ""))
elif uri.startswith(sUrl):
path = os.path.join(sRoot, uri.replace(sUrl, ""))
else:
return uri # handle absolute uri (ie: http://some.tld/foo.png)
if not os.path.isfile(path):
raise Exception(
'media URI must start with %s or %s' % (sUrl, mUrl)
)
return path
设置文件:
PROJECT_ROOT = "/var/www/html/newclone/userapi/"
MEDIA_ROOT = path.join(PROJECT_ROOT,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = path.join(PROJECT_ROOT,'static-root')
STATIC_URL = "/static/"
这有什么问题。 pdf生成成功但图像丢失
html文件:
<div class="h1"><img src="/media/xyz"></div>
答案 0 :(得分:0)
甚至无法想象错误: 在我的CSS中我写道:
.main{width:75%;}
因为这个图像不可见。我随意删除它,图像开始显示