コード例 #1
0
    def generate_pdf(self):
        buffer = self.buffer
        doc = BaseDocTemplate(
            buffer,
            rightMargin=0.3 * inch,
            leftMargin=0.3 * inch,
            topMargin=0.3 * inch,
            bottomMargin=inch / 4,
        )

        land_frame = Frame(0, 0, self.height, self.width, id='LFrame')
        land_frame._topPadding = 22
        land_frame._leftPadding = 11
        land_frame._bottomPadding = 22

        port_frame = Frame(0, 0, self.width, self.height, id='PFrame')
        port_frame._topPadding = 22
        port_frame._leftPadding = 11
        port_frame._bottomPadding = 22

        landscape_temp = PageTemplate(id='landscape_temp',
                                      frames=[
                                          land_frame,
                                      ],
                                      pagesize=landscape(self.pagesize))

        portrait_temp = PageTemplate(id='portrait_temp',
                                     frames=[
                                         port_frame,
                                     ],
                                     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))
        # Draw things on the PDF. Here's where the PDF generation happens.
        # See the ReportLab documentation for the full list of functionality.
        template_list = []
        for result in self.results:
            # image = 'http://127.0.0.1:8000' + result.image.url
            image = self.path_relative_to_file(__file__,
                                               '..' + result.image.url)
            # image = result.image.url
            im = self.scale_image(image)
            im.hAlign = 'CENTER'
            im.vAlign = 'MIDDLE'
            img = utils.ImageReader(image)
            width, height = img.getSize()

            if width > height:
                next_page = NextPageTemplate('landscape_temp')
                next_page.hAlign = 'CENTER'
                next_page.vAlign = 'MIDDLE'
                next_page.height = self.width
                next_page.width = self.height
                template_list.append(landscape_temp)
                elements.append(next_page)
            else:
                next_page = NextPageTemplate('portrait_temp')
                next_page.hAlign = 'CENTER'
                next_page.vAlign = 'MIDDLE'
                next_page.height = self.height
                next_page.width = self.width
                template_list.append(portrait_temp)
                elements.append(next_page)

            elements.append(im)
            #elements.append(PageBreak())

        doc.addPageTemplates(template_list)

        doc.build(elements)
        # Get the value of the BytesIO buffer and write it to the response.
        pdf = buffer.getvalue()
        buffer.close()
        return pdf