CSS not rendered by Pisa's pdf generation in Django

余生颓废 提交于 2020-01-12 21:52:13

问题


I generate a pdf file from HTML using Pisa:

def fetch_resources(uri, rel):
    path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
    return path

def write_pdf(template_src, context_dict, filename):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = open(filename, 'wb')
    pdf = pisa.pisaDocument(StringIO.StringIO(
        html.encode("UTF-8")), result, link_callback=fetch_resources)
    result.close()

My HTML has a link to an external CSS and is rendered properly, but the CSS is not used by Pisa (eg. font size, table cell width, text-align...).

<!DOCTYPE html>
<html lang="fr">
<head>
    <link rel="stylesheet" href="/site_media/style/style.css" />
</head>

<body>
....

Did I miss something?

Thanks


回答1:


You could try this 'Pisa-and-Reportlab-pitfalls' I had to add this

def fetch_resources(uri, rel):

On top of that I still carry all my css within the template. Also make sure you're using xhtml2pdf and not the old ho.pisa.



来源:https://stackoverflow.com/questions/8986831/css-not-rendered-by-pisas-pdf-generation-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!