2014-04-01 105 views
0

即時得到錯誤 - 「海峽」對象有沒有屬性「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 

我的圖像文件在同一目錄中這兩個文件。

+0

任何想法該錯誤指向哪條線?或者,更好的是,你可以發佈完整的追溯? –

回答

1

您只能將reportlab的Flowable添加到故事中。如果要附加圖像,您必須將ReportLab的方式做到這一點:

變化

elements.append('<img src="el45.jpg" height="5">')

elements.append(Image('el45.jpg', width=None, height=5))

您有寬度和高度參數玩。此外,你應該總是將大小乘以想要的單位,例如5*inch。有關其他信息,請參閱reportlab用戶指南的第9.3章!

+0

香港專業教育學院補充說,但現在我得到一個錯誤 IO錯誤在/ print_products 無法打開資源「el45.jpg」 https://www.dropbox.com/s/8gka3apvj8zmmsu/Screen%20Shot%202014-04- 01%20at%2015.06.23.png 查看圖像的錯誤細節 –

+0

這是因爲圖像不在當前工作目錄中。你可以指定文件的整個路徑來解決這個問題。 – Fookatchu

+0

我的目錄結構是https://www.dropbox.com/s/san4dd91pk88nib/Screen%20Shot%202014-04-01%20at%2016.15.13.png,所以它在與printing.py文件 –

相關問題