即時得到錯誤 - 「海峽」對象有沒有屬性「getKeepWithNext」試圖將圖像添加到PDF IM生成使用ReportLab的Django的ReportLab的錯誤 - 「海峽」對象有沒有屬性「getKeepWithNext」
林時Djnago 1.6。
這裏是我的文件
printing.py
import os.path
from reportlab.lib.pagesizes import letter, A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from reportlab.lib.units import inch
from reportlab.lib.enums import TA_CENTER
from reportlab.lib.enums import TA_RIGHT
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from django.conf import settings
from catalog.models import Product
class MyPrint:
def __init__(self, buffer, pagesize):
self.buffer = buffer
if pagesize == 'A4':
self.pagesize = A4
elif pagesize == 'Letter':
self.pagesize = letter
self.width, self.height = self.pagesize
@staticmethod
def _header_footer(canvas, doc):
# Save the state of our canvas so we can draw on it
canvas.saveState()
styles = getSampleStyleSheet()
# Header
header = Paragraph('This is a multi-line header. It goes on every page. ' * 5, styles['Normal'])
w, h = header.wrap(doc.width, doc.topMargin)
header.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin - h)
# Footer
footer = Paragraph('This is a multi-line footer. It goes on every page. ' * 5, styles['Normal'])
w, h = footer.wrap(doc.width, doc.bottomMargin)
footer.drawOn(canvas, doc.leftMargin, h)
# Release the canvas
canvas.restoreState()
def print_products(self):
buffer = self.buffer
doc = SimpleDocTemplate(buffer,
rightMargin=inch/4,
leftMargin=inch/4,
topMargin=inch/2,
bottomMargin=inch/4,
pagesize=self.pagesize)
# Our container for 'Flowable' objects
elements = []
# A large collection of style sheets pre-made for us
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='centered', alignment=TA_CENTER))
styles.add(ParagraphStyle(name='RightAlign', fontName='Arial', align=TA_RIGHT))
# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of functionality.
products = Product.objects.all()
elements.append(Paragraph('My Products', styles['Heading1']))
elements.append('<img src="el45.jpg" height="5">')
for i, product in enumerate(products):
elements.append(Paragraph(product.info(), styles['Normal']))
doc.build(elements, onFirstPage=self._header_footer, onLaterPages=self._header_footer, canvasmaker=NumberedCanvas)
# Get the value of the BytesIO buffer and write it to the response.
pdf = buffer.getvalue()
buffer.close()
return pdf
class NumberedCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs):
canvas.Canvas.__init__(self, *args, **kwargs)
self._saved_page_states = []
def showPage(self):
self._saved_page_states.append(dict(self.__dict__))
self._startPage()
def save(self):
"""add page info to each page (page x of y)"""
num_pages = len(self._saved_page_states)
for state in self._saved_page_states:
self.__dict__.update(state)
self.draw_page_number(num_pages)
canvas.Canvas.showPage(self)
canvas.Canvas.save(self)
def draw_page_number(self, page_count):
# Change the position of this to wherever you want the page number to be
self.drawRightString(211 * mm, 15 * mm + (0.2 * inch),
"Page %d of %d" % (self._pageNumber, page_count))
views.py
def print_products(request):
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'filename="Product.pdf"'
buffer = BytesIO()
report = MyPrint(buffer, 'Letter')
pdf = report.print_products()
response.write(pdf)
return response
我的圖像文件在同一目錄中這兩個文件。
任何想法該錯誤指向哪條線?或者,更好的是,你可以發佈完整的追溯? –