Conversion:
html = template.render(context)
resultFile = open(filepath, "w+b")
pdf = pisa.CreatePDF(html.encode('utf-8'), dest=resultFile,encoding='utf-8', link_callback=link_callback)
link_callback
def link_callback(uri, rel):
sUrl = settings.STATIC_URL # Usually /static/
sRoot = settings.STATIC_ROOT
mUrl = settings.MEDIA_URL # Usually /static/media/
mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/
# Convert URIs to absolute system paths
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)
# Ensure that file exists
if not os.path.isfile(path):
raise Exception(
'media URI must start with %s or %s' % (sUrl, mUrl)
)
return path
I successfully generated a pdf using xhtml2pdf. The file was created, but unfortunately, the css styles are not being applied correctly. I verified the path returned from link_callback and it is accurate, however, the pdf does not display any css styling.
css:
#page_1 {position:relative; overflow: hidden;margin: 64px 0px 65px 0px;padding: 0px;border: none;}
#page_1 #p1dimg1 {position:absolute;top:0px;left:20%;z-index:-1;width:600px;height:870px;}
#page_1 #p1dimg1 #p1img1 {width:600px;height:870px;}
#page_2 {position:relative; overflow: hidden;padding-left: 100px;border: none;height: 854px;}
#page_2 #p2dimg1 {position:absolute;left:27%;z-index:-1;width:490px;height:669px;}
#page_2 #p2dimg1 #p2img1 {width:490px;height:669px;}
The css path obtained: /Users/mypc/project/project/static/css/lender_borrower.css