【发布时间】: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>
我的代码非常基本,没有什么特别之处,它们只是从网络上逐字复制而来。我遇到了很多奇怪的问题:
- 字体大小、背景颜色适用于外部 css,但仅适用于正文或 html 标记。
- 无论是外部、内部还是内联,宽度、行高等都不起作用。
- 父 div 上的 margin-bottom 被应用到每个子 div 而不是父 div...
- 各种其他随机问题...
除了认为 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