6

我使用 Pisa 从 HTML 生成一个 pdf 文件:

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()

我的 HTML 有一个指向外部 CSS 的链接并正确呈现,但 Pisa 不使用 CSS(例如字体大小、表格单元格宽度、文本对齐...)。

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

<body>
....

我错过了什么?

谢谢

4

1 回答 1

5

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.

于 2012-01-24T12:54:22.850 回答