我得到错误 - ' str'对象没有属性&get; getKeepWithNext'尝试将图像添加到使用ReportLab生成的PDF时
我正在使用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
我的图片文件与这两个文件位于同一目录中。
答案 0 :(得分:2)
您只能在报道中添加reportlab&#39 {s} Flowable
。如果要附加图像,则必须以报告方式执行:
变化
elements.append('<img src="el45.jpg" height="5">')
到
elements.append(Image('el45.jpg', width=None, height=5))
您必须使用width和height参数。此外,您应始终将大小乘以所需单位,例如5*inch
。有关更多信息,请参阅reportlab用户指南的第9.3章。