我想在谷歌应用引擎中使用 html 创建一个动态 pdf。我使用 pisa 将 html 转换为 pdf。除了 < img > 标签之外的所有东西都有效。即使 css 中的背景图像有效,我真正需要的是 < img > 标签。
import os
import urllib
import unicodedata
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from pdf_libs.pisa.ho import pisa
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/pdf'
self.response.headers['Content-Disposition'] = 'filename=mlskdf.pdf'
html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css" media="all">
@page {
margin: 0.5cm;
margin-top:1.8cm;
margin-bottom: 1cm;
background-image: url('logo.jpg');
@frame footer {
-pdf-frame-content: footerContent;
bottom: 0.5cm;
margin-left: 0.75cm;
margin-right: 0.75cm;
height: 1cm;
}
@frame header {
-pdf-frame-content: headerContent;
background-color: #f4f4f4;
top: 0.2cm;
margin-left: 0.75cm;
margin-right: 0.75cm;
height: 2cm;
}
}
.footer .head
{
position:absolute;
left:0;
top:4;
width:100%;
}
table {
-pdf-keep-in-frame-mode: shrink;
}
h1 {
page-break-before:always;
}
div.invoice_header {
background-color: black;
color: #FFF;
padding-bottom: 7px;
text-align: center;
}
div.address_field {
text-align: left;
background-color: #f4f4f4;
display: block;
font-family: 'Ubuntu';
font-size: 12px;
margin-right:450px;
}
</style>
</head>
<body>
<div class="invoice_header">
<label align="center" >
<font face="Ubuntu" size="16">
HEADER
</font>
</label>
</div>
<br><br>
<div class="address_field" >
Another field
</div>
<table width="100%" height="100px" border="1" >
<tr>
<td align="center" width="50%"><font face="Ubuntu" size="24">column1</font></td>
<td width="25%"><font face="Ubuntu" size="4">column2</font></td>
</tr>
</table>
<br/></br>
<div id="footerContent">
<div class="footer">
This is footer.. Page <pdf:pagenumber/>
</div>
</div>
</body>
</html>"""
pdf = pisa.CreatePDF(html, self.response.out)
logging.info(pdf)
application = webapp.WSGIApplication([
('/.*', MainHandler)
],debug=True)**
当我添加 < img > 标签时,出现以下错误
ERROR 2014-08-06 04:53:25,903 pisa_document.py:223] Document error
Traceback (most recent call last):
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/pisa/sx/pisa3/pisa_document.py", line 172, in pisaDocument
doc.build(c.story)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/doctemplate.py", line 877, in build
self.handle_flowable(flowables)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/doctemplate.py", line 762, in handle_flowable
if frame.add(f, canv, trySplit=self.allowSplitting):
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/frames.py", line 174, in _add
flowable.drawOn(canv, self._x + self._leftExtraIndent, y, _sW=aW-w)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/flowables.py", line 106, in drawOn
self._drawOn(canvas)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/flowables.py", line 87, in _drawOn
self.draw()#this is the bit you overload
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/pisa/sx/pisa3/pisa_reportlab.py", line 575, in draw
Paragraph.draw(self)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/paragraph.py", line 970, in draw
self.drawPara(self.debug)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/paragraph.py", line 1436, in drawPara
dpl( tx, offset, lines[0], noJustifyLast and nLines==1)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/paragraph.py", line 307, in _leftDrawParaLineX
_putFragLine(offset, tx, line)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/platypus/paragraph.py", line 216, in _putFragLine
tx._canvas.drawImage(cbDefn.image,cur_x_s,cur_y+iy0,w,h,mask='auto')
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/pdfgen/canvas.py", line 825, in drawImage
imgObj = pdfdoc.PDFImageXObject(name, image, mask=mask)
File "/home/developer/Dropbox/File Tx Ganadev/new_project_template/pdf_libs/reportlab/pdfbase/pdfdoc.py", line 2076, in __init__
ext = string.lower(os.path.splitext(source)[1])
File "/usr/lib/python2.7/posixpath.py", line 105, in splitext
return genericpath._splitext(p, sep, altsep, extsep)
File "/usr/lib/python2.7/genericpath.py", line 91, in _splitext
sepIndex = p.rfind(sep)
AttributeError: PmlImage instance has no attribute 'rfind'
ERROR 2014-08-06 04:53:25,911 webapp2.py:1528] PmlImage instance has no attribute 'rfind'