html เป็น pdf โดยใช้ pisa- ปัญหาเกี่ยวกับการใช้แท็ก img

ฉันต้องการสร้างไฟล์ PDF แบบไดนามิกโดยใช้ html ในเครื่องมือของ Google App ฉันใช้ 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'

person Prajapathy3165    schedule 05.08.2014    source แหล่งที่มา
comment
คุณสามารถโพสต์รหัสเพิ่มเติมได้หรือไม่? หรือติดแท็ก img? ข้อผิดพลาดกำลังบอกคุณว่าคุณกำลังพยายามรับแอตทริบิวต์ 'rfind' ที่ไม่มีอยู่ในอินสแตนซ์ PmlImage   -  person Gwell    schedule 06.08.2014
comment
ฉันคิดว่าโพสต์นี้สามารถช่วยคุณได้: stackoverflow.com/questions/8959622/ สองสามปีที่แล้ว ฉันปล่อย PISA สำหรับ xhtml2pdf   -  person olituks    schedule 06.08.2014
comment
@Gwell ข้อผิดพลาดอาจเกิดจากการตั้งค่าไดเรกทอรีใด ๆ ฉันประสบปัญหาในการติดตั้ง PIL และการอ้างอิงอื่นๆ ฉันจัดการเพื่อให้มันทำงานได้   -  person Prajapathy3165    schedule 06.08.2014


คำตอบ (1)


คุณได้เพิ่มไลบรารี่ Pillow ลงในไฟล์ 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() และส่งคืนส่วนหน้าได้

person Hui Lin    schedule 22.03.2018