Example #1
0
    def test_html_images(self):
        pdf = MyFPDF()
        set_doc_date_0(pdf)
        pdf.add_page()
        rel = os.path.dirname(os.path.abspath(__file__))
        img_path = rel + "/image/png_images/c636287a4d7cb1a36362f7f236564cef.png"

        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')
        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')
        pdf.output('test/test.pdf', "F")

        # comment to see view output after test
        known_good_hash = "663ecbb2c23d55d4589629039d394911"
        self.assertEqual(calculate_hash_of_file('test/test.pdf'),
                         known_good_hash)
        os.unlink('test/test.pdf')
Example #2
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)
Example #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, "dd20cb8689d5512b31753b6ab6aa6341")
        os.unlink(test)
Example #4
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)
Example #5
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
Example #6
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)
Example #7
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)
Example #8
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)
Example #9
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)
Example #10
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)
Example #11
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
Example #12
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)
Example #13
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)
Example #14
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)
Example #15
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)
Example #16
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)
Example #17
0
    def test_RenderYML(self):
        f = Template(paperformat="A4", title="testCSV")

        f.parse_YML("testTemplateFile.yaml")
        f.add_page("testTemplateFile.yaml")

        pdf = f.getFPDF()
        set_doc_date_0(pdf)

        outfile = relative_path_to("testTemplateFile.pdf")
        pdf.output(outfile, 'F')

        test_hash = calculate_hash_of_file(outfile)
        # Must reproduce the same file than test_RenderCSV
        self.assertEqual(test_hash, "8d4e2060e5d8264d03ebca707a2ed1ca")

        os.unlink(outfile)
Example #18
0
    def test_RenderCSV(self):
        f = Template(paperformat="A4", title="testCSV")

        f.parse_csv("testTemplateFile.csv", delimiter=";")
        f.add_page("testTemplateFile.csv")

        pdf = f.getFPDF()
        set_doc_date_0(pdf)

        outfile = relative_path_to("testTemplateFile.pdf")
        pdf.output(outfile, 'F')

        test_hash = calculate_hash_of_file(outfile)
        # Assert versus a well know hash
        self.assertEqual(test_hash, "8d4e2060e5d8264d03ebca707a2ed1ca")

        os.unlink(outfile)
Example #19
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)
Example #20
0
  def test_ellipse_draw_color(self):
    pdf = self.PDFClass(unit = 'mm')
    set_doc_date_0(pdf)
    pdf.add_page()

    # Colors
    pdf.set_line_width(.5)
    for gray in [70, 140, 210]:
      pdf.set_draw_color(gray)
      pdf.ellipse(x=pdf.get_x(), y=pdf.get_y(), w=size, h=size, style = None)
      pdf.set_x(pdf.get_x() + size + margin)

    outfile = relative_path_to("output3.pdf")
    pdf.output(outfile)
    
    known_good_hash = "ad08d121648ee2b6e38982cdcce01688"
    self.assertEqual(known_good_hash, calculate_hash_of_file(outfile))
    os.unlink(outfile)
Example #21
0
  def test_rect_draw_color(self):
    pdf = self.PDFClass(unit = 'mm')
    set_doc_date_0(pdf)
    pdf.add_page()

    # Colors
    pdf.set_line_width(.5)
    for gray in [70, 140, 210]:
      pdf.set_draw_color(gray)
      pdf.rect(x=pdf.get_x(), y=pdf.get_y(), w=size, h=size, style = None)
      pdf.set_x(pdf.get_x() + size + margin)

    outfile = relative_path_to("output3.pdf")
    pdf.output(outfile)
    # print(calculate_hash_of_file(outfile))
    known_good_hash = "ab22c2b23e19e09387da55fd534d4f4c"
    self.assertEqual(known_good_hash, calculate_hash_of_file(outfile))
    os.unlink(outfile)
Example #22
0
    def test_insert_png_files(self):
        pdf = fpdf.FPDF(unit='pt')
        pdf.compress = False

        for image in goodFiles():
            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)
Example #23
0
  def test_dash(self):
    pdf = self.PDFClass(unit = 'mm')
    set_doc_date_0(pdf)
    pdf.add_page()

    def draw_diagonal_dash(pdf, x, y, *a, **k):
      pdf.dashed_line(x, y, x + size, y + (size / 2.0), *a, **k)

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

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

    pdf.set_draw_color(0)
    pdf.set_line_width(.2)
    draw_diagonal_dash(pdf, pdf.get_x(), pdf.get_y(), margin, margin)
    
    pdf.set_x(pdf.get_x() + size + margin)
    draw_diagonal_dash(pdf, pdf.get_x(), pdf.get_y(), margin / 2.0, margin)
    
    next_row(pdf)
    pdf.set_line_width(1)
    x, y = pdf.get_x(), pdf.get_y()
    pdf.dashed_line(x, y, x + 100, y + 80, 10, 3)
    pdf.set_x(pdf.get_x() + 20)
    x, y = pdf.get_x(), pdf.get_y()
    pdf.dashed_line(x, y, x + 100, y + 80, 3, 20)
    pdf.set_x(pdf.get_x() + 20)
    x, y = pdf.get_x(), pdf.get_y()
    pdf.dashed_line(x, y, x + 100, y + 80, 6, 17)

    outfile = relative_path_to('line_output1.pdf')
    pdf.output(outfile)
    # print(calculate_hash_of_file(outfile))
    known_good_hash = "4cf8faa9baf3f1835c03fa4ac1e6eb29"
    self.assertEqual(known_good_hash, calculate_hash_of_file(outfile))
    os.unlink(outfile)
Example #24
0
  def test_ellipse_fill_color(self):
    pdf = self.PDFClass(unit = 'mm')
    set_doc_date_0(pdf)
    pdf.add_page()

    pdf.set_fill_color(240)
    for color in [[230, 30, 180], [30, 180, 30], [30, 30, 70]]:
      pdf.set_draw_color(*color)
      pdf.ellipse(x=pdf.get_x(), y=pdf.get_y(), w=size, h=size, style = 'FD')
      pdf.set_x(pdf.get_x() + size + margin)

    next_row(pdf)

    outfile = relative_path_to("output4.pdf")
    pdf.output(outfile)
    
    known_good_hash = "2719bf0278757bb684d5d8e6e9cea5f5"
    self.assertEqual(known_good_hash, calculate_hash_of_file(outfile))
    os.unlink(outfile)
Example #25
0
  def test_rect_fill_color(self):
    pdf = self.PDFClass(unit = 'mm')
    set_doc_date_0(pdf)
    pdf.add_page()

    pdf.set_fill_color(240)
    for color in [[230, 30, 180], [30, 180, 30], [30, 30, 70]]:
      pdf.set_draw_color(*color)
      pdf.rect(x=pdf.get_x(), y=pdf.get_y(), w=size, h=size, style = 'FD')
      pdf.set_x(pdf.get_x() + size + margin)

    next_row(pdf)

    outfile = relative_path_to("output4.pdf")
    pdf.output(outfile)
    # print(calculate_hash_of_file(outfile))
    known_good_hash = "b3a94f3b3c0282dcbbb5f6127b3dfaab"
    self.assertEqual(known_good_hash, calculate_hash_of_file(outfile))
    os.unlink(outfile)
Example #26
0
    def test_insert_png_files_From_PIL(self):
        pdf = fpdf.FPDF(unit='pt')
        pdf.compress = False
        for image in goodFiles():
            pdf.add_page()
            im = Image.open(image)
            pdf.image(im, 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, "3cfa70ad39cd595562b726fc16b8510d")

        # self.assertEqual(test_hash, "4f65582566414202a12ed86134de10a7")
        os.unlink(outfile)
Example #27
0
    def test_ln_positioning_and_page_breaking_for_cell(self):
        doc = fpdf.FPDF(format='letter', unit='pt')
        set_doc_date_0(doc)
        doc.add_page()

        my_text_size = 36
        doc.set_font('Arial', size=my_text_size)
        text = (
            'Lorem ipsum Ut nostrud irure reprehenderit anim nostrud dolore sed '
            'ut Excepteur dolore ut sunt irure consectetur tempor eu tempor '
            'nostrud dolore sint exercitation aliquip velit ullamco esse dolore '
            'mollit ea sed voluptate commodo amet eiusmod incididunt Excepteur '
            'Excepteur officia est ea dolore sed id in cillum incididunt quis ex '
            'id aliqua ullamco reprehenderit cupidatat in quis pariatur ex et '
            'veniam consectetur et minim minim nulla ea in quis Ut in '
            'consectetur cillum aliquip pariatur qui quis sint reprehenderit '
            'anim incididunt laborum dolor dolor est dolor fugiat ut officia do '
            'dolore deserunt nulla voluptate officia mollit elit consequat ad '
            'aliquip non nulla dolor nisi magna consectetur anim sint officia '
            'sit tempor anim do laboris ea culpa eu veniam sed cupidatat in anim '
            'fugiat culpa enim Ut cillum in exercitation magna nostrud aute '
            'proident laboris est ullamco nulla occaecat nulla proident '
            'consequat in ut labore non sit id cillum ut ea quis est ut dolore '
            'nisi aliquip aute pariatur ullamco ut cillum Duis nisi elit sit '
            'cupidatat do Ut aliqua irure sunt sunt proident sit aliqua in '
            'dolore Ut in sint sunt exercitation aliquip elit velit dolor nisi '
            '') * 100

        for i in range(20):
            doc.cell(w=72,
                     h=my_text_size * 1.5,
                     border=1,
                     ln=2,
                     txt=text[i * 100:i * 100 + 99],
                     fill=0,
                     link='')

        outfile = relative_path_to('output.pdf')
        doc.output(outfile)
        self.assertEqual(calculate_hash_of_file(outfile), known_good_hash)
        os.unlink(outfile)
Example #28
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)
Example #29
0
  def test_ellipse_not_circle(self):
    pdf = self.PDFClass(unit = 'mm')
    set_doc_date_0(pdf)
    pdf.add_page()

    # Styles
    counter = 0
    for style in ['', 'F', 'FD', 'DF', None]:
      counter += 1
      pdf.ellipse(x=pdf.get_x(), y=pdf.get_y(), w=size/2, h=size, style = style)
      pdf.set_x(pdf.get_x() + (size / 2) + margin)
      
      if counter % 3 == 0:
        next_row(pdf)

    outfile = relative_path_to("output.pdf")
    pdf.output(outfile)

    known_good_hash = "169345cb25b662b236d35aa0b473092f"
    self.assertEqual(known_good_hash, calculate_hash_of_file(outfile))
    os.unlink(outfile)
Example #30
0
  def test_ellipse_style(self):
    pdf = self.PDFClass(unit = 'mm')
    set_doc_date_0(pdf)
    pdf.add_page()

    # Styles
    counter = 0
    for style in ['', 'F', 'FD', 'DF', None]:
      counter += 1
      pdf.ellipse(x=pdf.get_x(), y=pdf.get_y(), w=size, h=size, style = style)
      pdf.set_x(pdf.get_x() + size + margin)
      
      if counter % 3 == 0:
        next_row(pdf)

    outfile = relative_path_to("output1.pdf")
    pdf.output(outfile)
    
    known_good_hash = "2f08ed8338d7d421fe2a286ef6c00daf"
    self.assertEqual(known_good_hash, calculate_hash_of_file(outfile))
    os.unlink(outfile)