예제 #1
0
    def generate_thumbnail(self):
        """Create thumbnail for resource request.

        Raises:
            ThumbnailPageNotFoundError: If resource with more than one page does
                                   not provide a thumbnail page.
            MoreThanOneThumbnailPageFoundError: If resource provides more than
                                           one page as the thumbnail.

        Returns:
            Dictionary of thumbnail data.
        """
        thumbnail_data = self.data()
        if not isinstance(thumbnail_data, list):
            thumbnail_data = [thumbnail_data]

        if len(thumbnail_data) > 1:
            thumbnail_data = list(
                filter(lambda thumbnail_data: thumbnail_data.get("thumbnail"),
                       thumbnail_data))

            if len(thumbnail_data) == 0:
                raise ThumbnailPageNotFoundError(self)
            elif len(thumbnail_data) > 1:
                raise MoreThanOneThumbnailPageFoundError(self)

        thumbnail_data = resize_encode_resource_images(
            self.options["paper_size"].value, thumbnail_data)
        return thumbnail_data[0]
예제 #2
0
 def test_resize_encode_resource_images_image_letter_not_resized(self):
     image = Image.new("1", (100, 100))
     data = [{"type": "image", "data": image}]
     copy = resize_encode_resource_images("letter", data)
     copy_data = BytesIO(base64.b64decode(copy[0]["data"]))
     copy_image = Image.open(copy_data)
     self.assertEqual(image.size, copy_image.size)
예제 #3
0
 def test_resize_encode_resource_images_image_letter_resized(self):
     size = 3000
     ratio = (self.LETTER_HEIGHT * self.MM_TO_PIXEL_RATIO) / size
     expected_size = (int(size * ratio), int(size * ratio))
     image = Image.new("1", (size, size))
     data = [{"type": "image", "data": image}]
     copy = resize_encode_resource_images("letter", data)
     copy_data = BytesIO(base64.b64decode(copy[0]["data"]))
     copy_image = Image.open(copy_data)
     self.assertEqual(expected_size, copy_image.size)
    def pdf(self, resource_name):
        """Return PDF for resource request.

        The PDF is returned (compared to the thumbnail which is directly saved)
        as the PDF may be either saved to the disk, or returned in a HTTP
        response.

        Args:
            resource_name: Name of the resource (str).

        Return:
            PDF file of resource.
        """
        # Only import weasyprint when required as production environment
        # does not have it installed.
        from weasyprint import HTML, CSS
        context = dict()
        context["resource"] = resource_name
        context["header_text"] = self.options["header_text"].value
        context["paper_size"] = self.options["paper_size"].value

        if self.copies:
            num_copies = self.options["copies"].value
        else:
            num_copies = 1
        context["all_data"] = []
        for copy in range(num_copies):
            copy_data = self.data()
            if not isinstance(copy_data, list):
                copy_data = [copy_data]
            copy_data = resize_encode_resource_images(
                self.options["paper_size"].value,
                copy_data
            )
            context["all_data"].append(copy_data)

        filename = "{} ({})".format(resource_name, self.subtitle)
        context["filename"] = filename

        pdf_html = render_to_string("resources/base-resource-pdf.html", context)
        html = HTML(string=pdf_html, base_url=settings.BUILD_ROOT)
        css_file = finders.find("css/print-resource-pdf.css")
        css_string = open(css_file, encoding="UTF-8").read()
        base_css = CSS(string=css_string)
        return (html.write_pdf(stylesheets=[base_css]), filename)