繁体   English   中英

xhtml2pdf 比萨 css 坏了没有功能

[英]xhtml2pdf Pisa css broken none functional

我正在尝试使用 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. font-size, background-color 适用于外部 css,但仅​​适用于 body 或 html 标签。
  2. 宽度、行高等无论如何都不起作用,无论是外部的、内部的还是内联的。
  3. 父 div 上的 margin-bottom 应用于每个子 div 而不是父 div ...
  4. 各种其他随机问题...

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

xhtml2pdfxhtml2pdf使用reportlab实际创建PDF。

做一些调试 - 我在xhtml2pdf执行了parser.py,看看我是否可以解决CSS “缺失”或应用于错误元素的问题。

我发现CSS已成功解析,并且文档片段(在代码中)的转换工作正常,正确的CSS元素应用于正确的元素。

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

我第一次意识到它回答了这个问题 例如,在reportlab文档的表格渲染部分(开源用户指南,第7章,第76页)中,很明显border-style CSS属性没有模拟 - 所以尽管理论上你可以指定一个边框样式并且不会抛出任何错误,该值将被忽略。

在调查CSS到pdf映射时,我发现这个软件(Javascript)也将HTML / CSS映射到pdf。 我在这个问题的HTML和我链接的另一个问题上尝试过。 这样可以将页面正确地呈现到pdf文档中,因此我认为这不是pdf文档规范的基本限制,而是reportlab模块的基本限制。

在我看来,这是xhtml2pdf一个问题。 这对表来说似乎特别糟糕,但显然也会影响其他类型的文档。 很抱歉不解决您的问题,但我希望这可以解释一下。

如本答案中所述,仅支持少数 css 属性,并且您无法链接到外部 css 文件。 您必须将您的 css 直接放在您的 html 页面中(在您的 html 页面的 head 标签内声明一个样式标签)。

就我而言,我会考虑通过 pdfkit 使用 wkhtmltopdf。 pdfkit 是 wkhtmltopdf 的包装器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM