i am creating pdf from html and it gets converted but without image. i have tried the absolute urls also but it still doesn't work pdf function:
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
settings file :
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/"
What is wrong in this. pdf is generated successfully but the images are missing
html file :
<div class="h1"><img src="/media/xyz"></div>
Could not even imagine the error : in my css i have written :
.main{width:75%;}
because of this images were not visible. I just removed it randomly and images started displaying
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.