コード例 #1
0
def generate_resource_pdf(name, generator):
    """Return a response containing a generated PDF resource.

    Args:
        name: Name of resource to be created (str).
        generator: Instance of specific resource generator class.

    Returns:
        Tuple of PDF file of generated resource and filename.
    """
    from weasyprint import HTML, CSS

    context = dict()
    context["resource"] = name
    context["header_text"] = generator.requested_options.get("header_text", "")
    context["paper_size"] = generator.requested_options["paper_size"]

    num_copies = range(0, int(generator.requested_options.get("copies", 1)))
    context["all_data"] = []
    for copy in num_copies:
        context["all_data"].append(generate_resource_copy(generator))

    filename = "{} ({})".format(name, generator.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)
コード例 #2
0
 def test_generate_resources_copy_image_a4_not_resized(self):
     image = Image.new("1", (100, 100))
     generator = MagicMock()
     generator.data.return_value = [{"type": "image", "data": image}]
     generator.requested_options = {"paper_size": "a4"}
     copy = generate_resource_copy(generator, thumbnail=True)
     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_generate_resources_copy_valid_generator(self):
     resource = self.test_data.create_resource(
         "grid",
         "Grid",
         "resources/grid.html",
         "GridResourceGenerator",
     )
     query = QueryDict("paper_size=a4")
     generator = get_resource_generator(resource.generator_module, query)
     copy = generate_resource_copy(generator)
     self.assertEqual(len(copy), 1)
コード例 #4
0
 def test_generate_resources_copy_image_a4_resized(self):
     size = 3000
     ratio = (self.A4_HEIGHT * self.MM_TO_PIXEL_RATIO) / size
     expected_size = (int(size * ratio), int(size * ratio))
     image = Image.new("1", (size, size))
     generator = MagicMock()
     generator.data.return_value = [{"type": "image", "data": image}]
     generator.requested_options = {"paper_size": "a4"}
     copy = generate_resource_copy(generator, thumbnail=True)
     copy_data = BytesIO(base64.b64decode(copy[0]["data"]))
     copy_image = Image.open(copy_data)
     self.assertEqual(expected_size, copy_image.size)
コード例 #5
0
 def test_generate_resources_copy_for_thumbnail_valid_multiple_pages(self):
     generator = MagicMock()
     generator.data.return_value = [{
         "type": "html",
         "data": "Page 1"
     }, {
         "type": "html",
         "data": "Page 2",
         "thumbnail": True
     }]
     copy = generate_resource_copy(generator, thumbnail=True)
     self.assertEqual(len(copy), 1)
     self.assertEqual(copy[0]["data"], "Page 2")
コード例 #6
0
def generate_resource_thumbnail(name, generator):
    """Return image of thumbnail for of PDF resource.

    Args:
        name: Name of resource to be created (str).
        generator: Instance of specific resource generator class.

    Returns:
        Thumbnail of generated resource.
    """
    from weasyprint import HTML, CSS

    context = dict()
    context["resource"] = name
    context["paper_size"] = generator.requested_options["paper_size"]
    context["all_data"] = [generate_resource_copy(generator, thumbnail=True)]
    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_png(stylesheets=[base_css], resolution=72)