【问题标题】:xhtml2pdf Pisa css broken none functionalxhtml2pdf 比萨 css 坏了没有功能
【发布时间】:2015-06-24 19:27:27
【问题描述】:

我正在尝试使用 Django 使用 xhtml2pdf.pisa 使用 html+css 生成 PDF。但是,我在 CSS 方面遇到了各种奇怪的问题。

下面是我的代码:

from django.template.loader import render_to_string
import cStringIO as StringIO
import xhtml2pdf.pisa as pisa
import cgi, os
def fetch_resources(uri, rel):
    path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ""))
    return path
def test_pdf(request):
    html = render_to_string('pdf/quote_sheet.html', { 'pagesize':'A4', }, context_instance=RequestContext(request))
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources)
    if not pdf.err:
        return HttpResponse(result.getvalue(), mimetype='application/pdf')
    return HttpResponse('Gremlins ate your pdf! %s' % cgi.escape(html))

还有我的模板:

<!DOCTYPE HTML PUBLIC "-//W3C/DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    {% load static from staticfiles %}
    {% load i18n %}
    <meta charset="utf-8"/>
    <meta http-equiv="content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="content-language" content='{% trans "g_locale2" %}'/>
    <title>{% block title %}{% endblock %}</title>
    <style type="text/css">
        @page {
            size: A4;
            margin: 1cm;
            @frame footer {
                -pdf-frame-content: footerContent;
                bottom: 0cm;
                margin-left: 9cm;
                margin-right: 9cm;
                height: 1cm;
                font-family: "Microsoft JhengHei";
            }
        }
        @font-face {
            font-family: "Microsoft JhengHei";
            src:url('{% static "ttf/msjh.ttf" %}');
        }
        @font-face {
            font-family: "Microsoft JhengHei";
            src:url('{% static "ttf/msjhbd.ttf" %}');
        }
        @font-face {
            font-family: "Helvetica";
            src:url('{% static "ttf/Helvetica_Reg.ttf" %}');
        }
        table.styled-table tr th {
            color: gray;
            background-color: blue;
            font-size: 14px;
            font-family:"Microsoft JhengHei";
            border: 1px solid black;
        }
        .biz_phone, .biz_fax {
            display: inline-block;
            width: 100px;
            line-height: 32px;
        }
        .biz-info {
            margin-bottom: 20px;
        }
    </style>
    <link rel="stylesheet" href='{% static "css/pdf.css" %}'/>
</head>
<body>
    <div id="main-content">
        <div class="container">
            <div class="biz-info">
                <div class="biz_name">{{ proj.biz.name }}</div>
                <div class="biz_address">{{ proj.biz.address }}</div>
                <div class="biz_phone">{{ proj.biz.phone }}</div>
                <div class="biz_fax">{{ proj.biz.fax }}</div>
            </div>
            <div class="table-div">
                <table class="styled-table">
                    <tr class="row_header">
                        <th class="col_order">{% trans "g_item_num" %}</th>
                        <th class="col_name">{% trans "g_item_name" %}</th>
                        <th class="col_provider">{% trans "g_provider_name" %}</th>
                        <th class="col_budget_quantity">{% trans "g_quantity" %}</th>
                        <th class="col_price">{% trans "g_item_price" %}</th>
                        <th class="col_total_price">{% trans "g_item_total" %}</th>
                    </tr>
                </table>
           </div>
        </div>
    </div>
</body>
</html>

我的代码非常基本,没有什么特别之处,它们只是从网络上逐字复制而来。我遇到了很多奇怪的问题:

  1. 字体大小、背景颜色适用于外部 css,但仅​​适用于正文或 html 标记。
  2. 无论是外部、内部还是内联,宽度、行高等都不起作用。
  3. 父 div 上的 margin-bottom 被应用到每个子 div 而不是父 div...
  4. 各种其他随机问题...

除了认为 css 解析器和布局引擎完全不完整且不起作用之外,我无法从这些症状中观察到模式。但是我在网上找不到和我有同样问题的人。我疯了吗?我不确定这里发生了什么...任何帮助将不胜感激。

【问题讨论】:

  • 在此处回答另一个问题时,我在不同的上下文中看到了 xhtml2pdf 的这种行为。现在我会尝试更深入地研究它,我认为这不是一个孤立的问题
  • 仅供参考,这是相关的答案 - 请参阅我关于表格元素边框样式的注释 css stackoverflow.com/questions/24574976/…
  • 我已经在链接中阅读了您的答案,我不太确定...您是否建议我使用 matplotlib 代替渲染表?问题不仅在于表格,还在于 div。我在代码中做了一些编辑,样式 .biz_phone, .biz_fax { display: inline-block; } 有效,但宽度和行高无效,并且 .biz-info { margin-bottom: 20px; } 被应用于所有子 div biz_name、biz_address、biz_phone、biz_fax,但它应该只应用于 biz-info... 我们不仅在谈论 CSS 被忽略,而且 CSS 被错误地应用。
  • 对不起,我应该更清楚。我只是评论说我看到了类似的行为——这不是你的错误,还没有提出解决方案。如果我能解决问题,我会发布答案。
  • 好的。太感谢了。如果你解决了什么问题,请告诉我!!!

标签: python django pisa xhtml2pdf


【解决方案1】:

在幕后,xhtml2pdf 使用reportlab 实际创建 PDF。

进行一些调试 - 我跟踪了 xhtml2pdf 中 parser.py 的执行,看看我是否可以找出 CSS '丢失'或被应用于错误元素的位置。

我发现 CSS 已成功解析,并且转换为文档片段 (here in the code) 工作正常,将正确的 CSS 元素应用于正确的元素。

我相信问题出在reportlab pdf 渲染引擎上。它记录在这里。它在 CSS 和您可以传递给它的指令之间没有 1:1 的映射。

我第一次意识到它answering this question。例如,在reportlab documentation(开源用户指南,第 7 章,第 76 页)的表格渲染部分,很明显,border-style CSS 属性没有类似物 - 所以尽管理论上您可以指定边框样式并且不会抛出错误,该值将被忽略。

在研究 CSS 到 pdf 的映射时,我发现 this software (Javascript) 也将 HTML/CSS 映射到 pdf。我在这个问题的 HTML 和我链接的另一个问题上进行了尝试。这可以将页面正确地呈现到 pdf 文档中,因此我相信这不是 pdf 文档规范的基本限制,而是reportlab 模块的基本限制。

在我看来这是xhtml2pdf 的问题。这对表格来说似乎特别糟糕,但显然也会影响其他类型的文档。很抱歉没有解决您的问题,但我希望这有助于解释它。

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多