Python 使用pisa将html转换为pdf-使用img标记的问题

Python 使用pisa将html转换为pdf-使用img标记的问题,python,image,google-app-engine,pisa,xhtml2pdf,Python,Image,Google App Engine,Pisa,Xhtml2pdf,我想在谷歌应用程序引擎中使用html创建一个动态pdf。我正在使用pisa将html转换为pdf。除了标签以外的所有东西都可以工作。尽管css中的背景图像可以工作,但我真正需要的是标记 import os import urllib import unicodedata import logging from google.appengine.ext import webapp from google.appengine.ext.webapp import util from pdf_libs.

我想在谷歌应用程序引擎中使用html创建一个动态pdf。我正在使用pisa将html转换为pdf。除了标签以外的所有东西都可以工作。尽管css中的背景图像可以工作,但我真正需要的是标记

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)**

您是否将枕头库添加到requirements.txt文件中? 请将以下代码添加到requirements.txt文件:

Pillow==2.0.0
xhtml2pdf==0.2.1.
这是我的一些代码

import unicodedata
import StringIO
import xhtml2pdf.pisa as pisa
.......
packet = StringIO.StringIO()
pisa.CreatePDF(source_html, dest=packet)
.......

转换后,您可以使用packet.getvalue()获取数据包并将其返回前端。

您可以发布更多代码吗?还是用img标签?错误告诉您,您试图获取的属性“rfind”在PmlImage实例上不存在。我认为这篇文章可以帮助您:几年前,我删除了xhtml2pdf的PISA。@Gwell错误可能是由任何目录设置引起的。我在安装PIL和其他依赖项时遇到了一些困难。我设法使它运转起来。
import unicodedata
import StringIO
import xhtml2pdf.pisa as pisa
.......
packet = StringIO.StringIO()
pisa.CreatePDF(source_html, dest=packet)
.......