比萨在Django的pdf生成CSS [英] CSS not rendered by Pisa's pdf generation in Django

查看:159
本文介绍了比萨在Django的pdf生成CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用比萨从HTML生成pdf文件:

  def fetch_resources(uri,rel):
path = os.path.join(settings.MEDIA_ROOT,uri.replace(settings.MEDIA_URL,))
返回路径

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 = .pisaDocument(StringIO.StringIO(
html.encode(UTF-8)),结果,link_callback = fetch_resources)
result.close()
/ pre>

我的HTML具有指向外部CSS的链接,并且正确呈现,但是比萨不使用CSS(例如,字体大小,表格宽度,文本-align ...)。

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

< body>
....

我错过了什么?



谢谢

解决方案

您可以尝试这个比萨和reportlab-pitfalls
我不得不添加这个

  def fetch_resources(uri,rel):

除此之外,我仍然将所有的CSS都放在模板中。还要确保您使用的是 xhtml2pdf ,而不是旧的ho.pisa。


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

解决方案

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.

这篇关于比萨在Django的pdf生成CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆