我正在尝试创建我的客户开始为他们的客户开具发票的PDF。我在过去处理过创建PDF,记得创建它们是一个巨大的痛苦。在我的Django应用程序中,我有两个视图函数,一个只是呈现我用来设计发票的模板,另一个则将其转换为pdf并呈现PDF。我有两个选项卡并排打开,其中一个指向每个版本的模板,HTML和PDF,他们看起来完全不同。有没有一种特殊的标记语言我必须用来创建PDF,或者他们的python包是不是可以将HTML转换成PDF中的HTML的精确副本?
视图函数
from django.shortcuts import render, HttpResponse
from django.template.loader import get_template
from io import BytesIO
from xhtml2pdf import pisa
def generate_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render({})
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
def ronin(request):
template = get_template('ronin.html')
context = {}
html = template.render(context)
pdf = generate_pdf('ronin.html', context)
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Invoice_%s.pdf" % ("12341231")
content = "inline; filename='%s'" % (filename)
download = request.GET.get("download")
if download:
content = "attachment; filename='%s'" % (filename)
response['Content-Disposition'] = content
return response
return HttpResponse("Not found")
def ronin_html(request):
template = 'ronin.html'
context = {}
return render(request, template, context)HTML
<!DOCTYPE html>
<html>
<head></head>
<body style="margin: 0;">
<style>
#invoice-template {
padding: 100px 100px 50px 100px
}
table {
border-collapse: collapse;
}
table#main {
border-bottom: 1px solid darkgray; width: 100%;
}
td.header.first {
border-bottom: 1px solid darkgray;
border-right: 1px solid darkgray;
padding: 1% 4%;
width: 20%;
}
td.header.second {
padding: 0;
vertical-align: top;
}
td.header.second .contact-details td {
padding: 15px;
}
td.header.second .address td {
padding: 15px;
}
td.header.second p {
margin: 0;
}
</style>
<div id="invoice-template">
<table id="main" cellspacing="0">
<tr>
<td class="header first">
<img src="/static/images/logo_dark.png" alt="">
</td>
<td class="header second">
<table>
<tr class="address">
<td>
<p>ADDRESS</p>
<p>ADDRESS EXAMPLE St. CITY, STATE 11111</p>
</td>
</tr>
<tr class="contact-details">
<td>
<p>PHONE</p>
<p>+1 111 222 3333</p>
</td>
<td>
<p>WEBSITE</p>
<p>Website.com</p>
</td>
<td>
<p>FOCUS</p>
<p>WEBSITES / MOBILE APPS</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>HTML视图

PDF视图

发布于 2018-02-18 05:19:33
你将会遇到大多数HTML库的渲染问题,特别是当它们使用WebKit作为引擎的时候。我推荐使用Puppeteer (创建页面的无头chrome实例并截取屏幕截图,这将是页面的真正1:1副本)和PDFKit的组合来获取这些图像并将它们附加到pdf文档。
当然,这取决于您是否知道发票不会比整个PDF页面大,如果是这样,您需要知道如何将内容分成单独的页面。
如果你真的想要PDF格式的1:1复制品,这些都是需要解决的难题。
否则,我建议使用wkhtmltopdf之类的东西来获得一个相当好的近似值。
https://stackoverflow.com/questions/48845897
复制相似问题