Пример #1
0
    def test_insert_png_files(self):
        pdf = fpdf.FPDF(unit="pt")
        pdf.compress = False

        not_supported = [
            "e59ec0cfb8ab64558099543dc19f8378.png",  # Interlacing not supported:
            "6c853ed9dacd5716bc54eb59cec30889.png",  # 16-bit depth not supported:
            "ac6343a98f8edabfcc6e536dd75aacb0.png",  # Interlacing not supported:
            "93e6127b9c4e7a99459c558b81d31bc5.png",  # Interlacing not supported:
            "18f9baf3834980f4b80a3e82ad45be48.png",  # Interlacing not supported:
            "51a4d21670dc8dfa8ffc9e54afd62f5f.png",  # Interlacing not supported:
        ]

        images = [
            relative_path_to(f)
            for f in os.listdir(relative_path_to("."))
            if f.endswith(".png") and os.path.basename(f) not in not_supported
        ]
        images.sort()

        for image in images:
            if os.path.basename(image) in not_supported:
                self.assertRaises(Exception, pdf.image, x=0, y=0, w=0, h=0, link=None)
            else:
                pdf.add_page()
                pdf.image(image, x=0, y=0, w=0, h=0, link=None)

        assert_pdf_equal(self, pdf, "test_insert_png_files.pdf")
Пример #2
0
    def test_insert_gif(self):
        pdf = fpdf.FPDF()
        pdf.compress = False
        pdf.add_page()
        imagename = relative_path_to('circle.gif')
        pdf.image(imagename, x=15, y=15)

        set_doc_date_0(pdf)
        test = relative_path_to('insert_images_gif_test.pdf')
        pdf.output(test, 'F')

        test_hash = calculate_hash_of_file(test)
        self.assertEqual(test_hash, "be9994a5fadccca9d316c39d302f6248")
        os.unlink(test)
Пример #3
0
    def test_insert_png(self):
        pdf = fpdf.FPDF()
        pdf.compress = False
        pdf.add_page()
        imagename = relative_path_to('insert_images_insert_png.png')
        pdf.image(imagename, x=15, y=15, h=140)

        set_doc_date_0(pdf)
        test = relative_path_to('insert_images_png_test.pdf')
        pdf.output(test, 'F')

        test_hash = calculate_hash_of_file(test)
        self.assertEqual(test_hash, "17c98e10f5c0d95ae17cd31b0f6a0919")
        os.unlink(test)
Пример #4
0
    def test_insert_bmp(self):
        pdf = fpdf.FPDF()
        pdf.compress = False
        pdf.add_page()
        imagename = relative_path_to('circle.bmp')
        pdf.image(imagename, x=15, y=15, h=140)

        set_doc_date_0(pdf)
        test = relative_path_to('insert_images_bmp_test.pdf')
        pdf.output(test, 'F')

        test_hash = calculate_hash_of_file(test)
        self.assertEqual(test_hash, "49e5800162c7b019ac25354ce4708e35")
        os.unlink(test)
Пример #5
0
    def test_insert_png(self):
        pdf = fpdf.FPDF()
        pdf.compress = False
        pdf.add_page()
        imagename = relative_path_to('insert_images_insert_png.png')
        pdf.image(imagename, x=15, y=15, h=140)

        set_doc_date_0(pdf)
        test = relative_path_to('insert_images_png_test.pdf')
        pdf.output(test, 'F')

        test_hash = calculate_hash_of_file(test)
        self.assertEqual(test_hash, "dd20cb8689d5512b31753b6ab6aa6341")
        os.unlink(test)
Пример #6
0
    def test_insert_jpg(self):
        pdf = fpdf.FPDF()
        pdf.compress = False
        pdf.add_page()
        imagename = relative_path_to('insert_images_insert_jpg.jpg')
        pdf.image(imagename, x=15, y=15, h=140)

        set_doc_date_0(pdf)
        test = relative_path_to('insert_images_jpg_test.pdf')
        pdf.output(test, 'F')

        test_hash = calculate_hash_of_file(test)
        self.assertEqual(test_hash, "98e21803d01d686504238cb17a636c32")
        os.unlink(test)
Пример #7
0
    def test_insert_jpg(self):
        pdf = fpdf.FPDF()
        pdf.compress = False
        pdf.add_page()
        imagename = relative_path_to('insert_images_insert_jpg.jpg')
        pdf.image(imagename, x=15, y=15, h=140)

        set_doc_date_0(pdf)
        test = relative_path_to('insert_images_jpg_test.pdf')
        pdf.output(test, 'F')

        test_hash = calculate_hash_of_file(test)
        self.assertEqual(test_hash, "c6997071c5e5f4191327603607d2a32c")
        os.unlink(test)
Пример #8
0
 def test_insert_gif(self):
     pdf = fpdf.FPDF()
     pdf.compress = False
     pdf.add_page()
     imagename = relative_path_to("circle.gif")
     pdf.image(imagename, x=15, y=15)
     assert_pdf_equal(self, pdf, "test_insert_gif.pdf")
Пример #9
0
 def test_insert_pillow(self):
     pdf = fpdf.FPDF()
     pdf.add_page()
     file_path = relative_path_to("insert_images_insert_png.png")
     img = Image.open(file_path)
     pdf.image(img, x=15, y=15)
     assert_pdf_equal(self, pdf, "test_insert_pillow.pdf")
Пример #10
0
    def test_load_text_file(self):
        file = relative_path_to('__init__.py')
        contents = '"""This package contains image tests"""\n'
        bc = contents.encode('utf-8')

        resource = fpdf.image_parsing.load_resource(file).getvalue()
        self.assertEqual(bytes(resource), bc)
Пример #11
0
  def test_line(self):
    pdf = self.PDFClass(unit = 'mm')
    set_doc_date_0(pdf)
    pdf.add_page()

    def draw_diagonal_line(pdf, x, y):
      pdf.line(x, y, x + size, y + (size / 2.0))

    for width in [.71, 1, 2]:
      pdf.set_line_width(width)
      draw_diagonal_line(pdf, pdf.get_x(), pdf.get_y())
      pdf.set_x(pdf.get_x() + size + margin)
    next_row(pdf)

    for color in [70, 140, 200]:
      pdf.set_draw_color(color)
      draw_diagonal_line(pdf, pdf.get_x(), pdf.get_y())
      pdf.set_x(pdf.get_x() + size + margin)
    next_row(pdf)

    outfile = relative_path_to('line_output.pdf')
    pdf.output(outfile)
    # print(calculate_hash_of_file(outfile))
    known_good_hash = "684bb1210caf57a77021124e1b8a81ef"
    self.assertEqual(known_good_hash, calculate_hash_of_file(outfile))
    os.unlink(outfile)
Пример #12
0
 def test_nominal_csv(self):
     "Taken from docs/Templates.md"
     tmpl = Template(format="A4", title="Sample Invoice")
     tmpl.parse_csv(relative_path_to("mycsvfile.csv"), delimiter=";")
     tmpl.add_page()
     tmpl["company_name"] = "Sample Company"
     assert_pdf_equal(self, tmpl, "test_nominal_csv.pdf")
Пример #13
0
    def test_first_999_chars(self):
        for fontpath in (
                "DejaVuSans.ttf",
                "DroidSansFallback.ttf",
                "Roboto-Regular.ttf",
                "cmss12.ttf",
        ):
            with self.subTest(fontpath=fontpath):
                fontname = os.path.splitext(fontpath)[0]
                fontpath = relative_path_to(fontpath)

                pdf = fpdf.FPDF()
                pdf.add_page()
                pdf.add_font(fontname, fname=fontpath, uni=True)
                pdf.set_font(fontname, size=10)

                ttf = MyTTFontFile()
                ttf.getMetrics(fontpath)

                # Create a PDF with the first 999 charters defined in the font:
                for counter, character in enumerate(ttf.saveChar, 0):
                    pdf.write(
                        8,
                        u"%03d) %03x - %c" % (counter, character, character))
                    pdf.ln()
                    if counter >= 999:
                        break

                assert_pdf_equal(self, pdf,
                                 "test_first_999_chars-" + fontname + ".pdf")
Пример #14
0
    def test_html_images(self):
        pdf = MyFPDF()
        pdf.add_page()

        initial = 10
        mm_after_image = initial + px2mm(300)
        self.assertEqual(round(pdf.get_x()), 10,
                         "Initial x margin is not expected")
        self.assertEqual(round(pdf.get_y()), 10,
                         "Initial y margin is not expected")
        self.assertEqual(round(pdf.w), 210, "Page width is not expected")

        img_path = relative_path_to(
            "../image/png_images/c636287a4d7cb1a36362f7f236564cef.png")
        pdf.write_html(
            "<center><img src=\"%s\" height='300' width='300'></center>" %
            img_path)
        # Unable to text position of the image as write html moves to a new line after
        # adding the image but it can be seen in the produce test.pdf file.
        self.assertEqual(round(pdf.get_x()), 10,
                         "Have not moved to beginning of new line")
        self.assertAlmostEqual(
            pdf.get_y(),
            mm_after_image,
            places=2,
            msg="Image height has moved down the page",
        )

        assert_pdf_equal(self, pdf, "test_html_images.pdf")
Пример #15
0
 def test_insert_png(self):
     pdf = fpdf.FPDF()
     pdf.compress = False
     pdf.add_page()
     imagename = relative_path_to("insert_images_insert_png.png")
     pdf.image(imagename, x=15, y=15, h=140)
     assert_pdf_equal(self, pdf, "test_insert_png.pdf")
Пример #16
0
 def test_insert_bmp(self):
     pdf = fpdf.FPDF()
     pdf.compress = False
     pdf.add_page()
     file_path = relative_path_to("circle.bmp")
     pdf.image(file_path, x=15, y=15, h=140)
     assert_pdf_equal(self, pdf, "test_insert_bmp.pdf")
Пример #17
0
    def test_insert_png_files(self):
        pdf = fpdf.FPDF(unit='pt')
        pdf.compress = False

        not_supported = [
            "e59ec0cfb8ab64558099543dc19f8378.png",  # Interlacing not supported:
            "6c853ed9dacd5716bc54eb59cec30889.png",  # 16-bit depth not supported:
            "ac6343a98f8edabfcc6e536dd75aacb0.png",  # Interlacing not supported:
            "93e6127b9c4e7a99459c558b81d31bc5.png",  # Interlacing not supported:
            "18f9baf3834980f4b80a3e82ad45be48.png",  # Interlacing not supported:
            "51a4d21670dc8dfa8ffc9e54afd62f5f.png",  # Interlacing not supported:
        ]

        images = [
            relative_path_to(f) for f in os.listdir(relative_path_to('.'))
            if f.endswith(".png") and os.path.basename(f) not in not_supported
        ]
        images.sort()

        for image in images:
            if os.path.basename(image) in not_supported:
                pass
                self.assertRaises(Exception,
                                  pdf.image,
                                  x=0,
                                  y=0,
                                  w=0,
                                  h=0,
                                  type='',
                                  link=None)
            else:
                pdf.add_page()
                pdf.image(image, x=0, y=0, w=0, h=0, type='', link=None)

        set_doc_date_0(pdf)
        outfile = relative_path_to('insert_images_png_test_files.pdf')
        pdf.output(outfile, 'F')
        # print(calculate_hash_of_file(outfile))

        test_hash = calculate_hash_of_file(outfile)
        # ordered the images for reproduceability
        self.assertEqual(test_hash, "0085260bea512b9394ce1502b196240a")

        # self.assertEqual(test_hash, "4f65582566414202a12ed86134de10a7")
        os.unlink(outfile)
Пример #18
0
 def test_rotation(self):
     pdf = FPDF()
     pdf.add_page()
     x, y = 60, 60
     img_filepath = relative_path_to(
         "image/png_images/66ac49ef3f48ac9482049e1ab57a53e9.png")
     with pdf.rotation(45, x=x, y=y):
         pdf.image(img_filepath, x=x, y=y)
     pdf.image(img_filepath, x=150, y=150)
     assert_pdf_equal(self, pdf, "test_rotation.pdf")
Пример #19
0
    def test_insert_png_files(self):
        pdf = fpdf.FPDF(unit='pt')
        pdf.compress = False

        images = [
            relative_path_to(f) for f in os.listdir(relative_path_to('.'))
            if f.endswith(".eps") or f.endswith(".ai")
        ]
        images.sort()

        for image in images:
            pdf.add_page()
            with open(image) as img:
                pdf.eps(img.read(), x=0, y=0, w=0, h=0, link=None)

        set_doc_date_0(pdf)
        outfile = relative_path_to('insert_eps_test.pdf')
        pdf.output(outfile, 'F')
        # print(calculate_hash_of_file(outfile))

        test_hash = calculate_hash_of_file(outfile)
Пример #20
0
 def test_insert_jpg(self):
     pdf = fpdf.FPDF()
     pdf.compress = False
     pdf.add_page()
     imagename = relative_path_to("insert_images_insert_jpg.jpg")
     pdf.image(imagename, x=15, y=15, h=140)
     if sys.platform in ("cygwin", "win32"):
         # Pillow uses libjpeg-turbo on Windows and libjpeg elsewhere,
         # leading to a slightly different image being parsed and included in the PDF:
         assert_pdf_equal(self, pdf, "test_insert_jpg_windows.pdf")
     else:
         assert_pdf_equal(self, pdf, "test_insert_jpg.pdf")
Пример #21
0
def goodFiles():
    not_supported = [
        "e59ec0cfb8ab64558099543dc19f8378.png",  # Interlacing not supported:
        "6c853ed9dacd5716bc54eb59cec30889.png",  # 16-bit depth not supported:
        "ac6343a98f8edabfcc6e536dd75aacb0.png",  # Interlacing not supported:
        "93e6127b9c4e7a99459c558b81d31bc5.png",  # Interlacing not supported:
        "18f9baf3834980f4b80a3e82ad45be48.png",  # Interlacing not supported:
        "51a4d21670dc8dfa8ffc9e54afd62f5f.png",  # Interlacing not supported:
    ]

    images = [
        relative_path_to(f) for f in os.listdir(relative_path_to('.'))
        if f.endswith(".png") and os.path.basename(f) not in not_supported
    ]
    images.sort()
    rtn = []
    for image in images:
        if os.path.basename(image) in not_supported:
            pass
        else:
            rtn.append(image)
    return rtn
Пример #22
0
  def test_first_999_chars(self):
    # fontpath = 'Roboto-Regular.ttf'
    fontpath = relative_path_to('DejaVuSans.ttf')

    pdf = fpdf.FPDF()
    pdf.compression = True
    pdf.add_page()
    pdf.add_font('font', '', fontpath, uni = True)
    pdf.set_font('font', '', 10)

    ttf = MyTTFontFile()
    ttf.getMetrics(fontpath)

    # the next bit throws four insignificant warnings
    import warnings
    warnings.filterwarnings("ignore")
    
    for counter, character in enumerate(ttf.saveChar, 0):
      # print (counter, character)
      pdf.write(8, u"%03d) %06x - %c" % (counter, character, character))
      pdf.ln()

      if counter >= 999: break

    testing_output = relative_path_to('charmap_test_output.pdf')

    set_doc_date_0(pdf)
    pdf.output(testing_output)

    output_hash = calculate_hash_of_file(testing_output)
    known_output_hash = "f2f5784207ad1f26d230bdf40af9d3e0"
    self.assertEqual(known_output_hash, output_hash)
    os.unlink(testing_output)

    # clear out all the pkl files
    pklList = [f for f in relative_path_to('.') if f.endswith(".pkl")]
    for f in pklList:
      os.remove(relative_path_to(f))
Пример #23
0
    def test_issue65(self):
        pdf = fpdf.FPDF()
        pdf.compress = False
        pdf.add_page()
        png = "https://g.twimg.com/Twitter_logo_blue.png"
        pdf.image(png, x=15, y=15, w=30, h=25)

        set_doc_date_0(pdf)
        test = relative_path_to('issue65_test.pdf')
        pdf.output(test, 'F')

        known_good_hash = "ed3d7f6430a8868d3e9587170aa2f678"
        self.assertEqual(known_good_hash, calculate_hash_of_file(test))
        os.unlink(test)
Пример #24
0
  def test_setting_all_layout(self):
    for layout_input in ['single', 'continuous', 'two', 'default']:
      doc = fpdf.FPDF()
      document_operations(doc)

      doc.set_display_mode(zoom='default', layout=layout_input)

      output = relative_path_to('default ' + layout_input + ' output.pdf')
      doc.output(output)
      # print(calculate_hash_of_file(output), 'layout_' + layout_input)
      known_good_hash = known_good_hashes['layout_' + layout_input]
      self.assertEqual(known_good_hash, calculate_hash_of_file(output))
      os.unlink(output)
      del doc
Пример #25
0
  def test_setting_all_zoom(self):
    for zoom_input in ['fullpage', 'fullwidth', 'real', 'default']:
      doc = fpdf.FPDF()
      document_operations(doc)

      doc.set_display_mode(zoom=zoom_input, layout='continuous')

      output = relative_path_to('continuous ' + zoom_input + ' output.pdf')
      doc.output(output)
      # print(calculate_hash_of_file(output), 'zoom_' + zoom_input)
      known_good_hash = known_good_hashes['zoom_' + zoom_input]
      self.assertEqual(known_good_hash, calculate_hash_of_file(output))
      os.unlink(output)
      del doc
Пример #26
0
    def test_setting_old_date(self):
        doc = fpdf.FPDF()
        doc.add_page()
        # 2017, April 18th, almost 7:09a
        date = datetime.datetime(2017, 4, 18, 7, 8, 55)
        doc.set_creation_date(date)

        output = relative_path_to('output.pdf')
        doc.output(output)

        known_good_hash = "b4b210a31bcb3d741508f3d2d005adf9"
        result_hash = calculate_hash_of_file(output)
        self.assertEqual(known_good_hash, result_hash)
        os.unlink(output)
Пример #27
0
    def test_unequal_dates(self):
        old_doc = fpdf.FPDF()
        old_doc.add_page()
        # 2017, April 18th, almost 7:09a
        date = datetime.datetime(2017, 4, 18, 7, 8, 55)
        old_doc.set_creation_date(date)

        output = relative_path_to('output.pdf')
        old_doc.output(output)

        # known_good_hash = "b4b210a31bcb3d741508f3d2d005adf9"
        old_result_hash = calculate_hash_of_file(output)
        os.unlink(output)

        new_doc = fpdf.FPDF()
        new_doc.add_page()
        new_doc.set_creation_date()  # now
        output = relative_path_to('output.pdf')
        new_doc.output(output)

        new_result_hash = calculate_hash_of_file(output)
        os.unlink(output)

        self.assertNotEqual(old_result_hash, new_result_hash)
Пример #28
0
    def test_jpg(self):
        pdf = fpdf.FPDF()
        pdf.compress = False
        pdf.add_page()
        jpg = ("https://upload.wikimedia.org/wikipedia/commons/8/8c/"
               "JPEG_example_JPG_RIP_025.jpg")
        pdf.image(jpg, x=15, y=15)

        set_doc_date_0(pdf)
        test = relative_path_to('issue65_test.pdf')
        pdf.output(test, 'F')

        known_good_hash = "0486acfcd75597cc52ca2eb69e74c59c"
        self.assertEqual(known_good_hash, calculate_hash_of_file(test))
        os.unlink(test)
Пример #29
0
  def test_put_info_all(self):
    doc = fpdf.FPDF()
    document_operations(doc)
    doc.set_title('sample title')
    doc.set_subject('sample subject')
    doc.set_author('sample author')
    doc.set_keywords('sample keywords')
    doc.set_creator('sample creator')
    # doc.set_creation_date()

    output = relative_path_to("put_info_test.pdf")
    doc.output(output)
    # print(calculate_hash_of_file(output))
    known_good_hash = "64d87472bd5e369441dac2b092a249d8"
    self.assertEqual(calculate_hash_of_file(output), known_good_hash)
    os.unlink(output)
Пример #30
0
  def test_put_info_some(self):
    doc = fpdf.FPDF()
    document_operations(doc)
    doc.set_title('sample title')
    #doc.set_subject('sample subject')
    #doc.set_author('sample author')
    doc.set_keywords('sample keywords')
    doc.set_creator('sample creator')
    # doc.set_creation_date()

    output = relative_path_to("put_info_test.pdf")
    doc.output(output)
    # print(calculate_hash_of_file(output))
    known_good_hash = "bcc272f353be1acb76c5caf3f662b9af"
    self.assertEqual(calculate_hash_of_file(output), known_good_hash)
    os.unlink(output)