Exemple #1
0
def test_multi_cell_ln_1(tmp_path):
    doc = fpdf.FPDF()
    doc.add_page()
    doc.set_font("helvetica", size=TEXT_SIZE)
    doc.multi_cell(w=100, h=LINE_HEIGHT, border=1, txt="Lorem ipsum", ln=1)
    doc.multi_cell(w=100, h=LINE_HEIGHT, border=1, txt="Ut nostrud irure")
    assert_pdf_equal(doc, HERE / "multi_cell_ln_1.pdf", tmp_path)
Exemple #2
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")
Exemple #3
0
 def test_code39(self):
     pdf = FPDF()
     pdf.add_page()
     pdf.code39("fpdf2", x=50, y=50, w=4, h=20)
     pdf.set_font("courier", "B", size=36)
     pdf.text(x=80, y=80, txt="fpdf2")
     assert_pdf_equal(self, pdf, "test_code39.pdf")
Exemple #4
0
 def test_setting_old_date(self):
     doc = fpdf.FPDF()
     doc.add_page()
     # 2017, April 18th, almost 7:09a
     date = datetime(2017, 4, 18, 7, 8, 55)
     doc.set_creation_date(date)
     assert_pdf_equal(self, doc, "test_setting_old_date.pdf")
Exemple #5
0
 def test_interleaved2of5(self):
     pdf = FPDF()
     pdf.add_page()
     pdf.interleaved2of5("1337", x=65, y=50, w=4, h=20)
     pdf.set_font("courier", "B", size=36)
     pdf.text(x=80, y=80, txt="1337")
     assert_pdf_equal(self, pdf, "test_interleaved2of5.pdf")
Exemple #6
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")
Exemple #7
0
 def test_code39(self, tmp_path):
     pdf = FPDF()
     pdf.add_page()
     pdf.code39("fpdf2", x=50, y=50, w=4, h=20)
     pdf.set_font("courier", "B", size=36)
     pdf.text(x=80, y=80, txt="fpdf2")
     assert_pdf_equal(pdf, HERE / "barcodes_code39.pdf", tmp_path)
Exemple #8
0
 def test_insert_pillow(self, tmp_path):
     pdf = fpdf.FPDF()
     pdf.add_page()
     file_path = HERE / "insert_images_insert_png.png"
     img = Image.open(file_path)
     pdf.image(img, x=15, y=15)
     assert_pdf_equal(pdf, HERE / "image_types_insert_pillow.pdf", tmp_path)
Exemple #9
0
 def test_insert_png(self, tmp_path):
     pdf = fpdf.FPDF()
     pdf.compress = False
     pdf.add_page()
     file_path = HERE / "insert_images_insert_png.png"
     pdf.image(file_path, x=15, y=15, h=140)
     assert_pdf_equal(pdf, HERE / "image_types_insert_png.pdf", tmp_path)
Exemple #10
0
 def test_insert_gif(self, tmp_path):
     pdf = fpdf.FPDF()
     pdf.compress = False
     pdf.add_page()
     file_path = HERE / "circle.gif"
     pdf.image(file_path, x=15, y=15)
     assert_pdf_equal(pdf, HERE / "image_types_insert_gif.pdf", tmp_path)
Exemple #11
0
def test_insert_bmp(tmp_path):
    pdf = fpdf.FPDF()
    pdf.compress = False
    pdf.add_page()
    file_path = HERE / "circle.bmp"
    pdf.image(file_path, x=15, y=15, h=140)
    assert_pdf_equal(pdf, HERE / "image_types_insert_bmp.pdf", tmp_path)
Exemple #12
0
 def test_setting_all_zoom(self):
     for zoom_input in ["fullpage", "fullwidth", "real", "default"]:
         with self.subTest(zoom_input=zoom_input):
             doc = fpdf.FPDF()
             document_operations(doc)
             doc.set_display_mode(zoom=zoom_input, layout="continuous")
             assert_pdf_equal(self, doc, f"catalog-zoom-{zoom_input}.pdf")
Exemple #13
0
def test_setting_all_layout(layout_input, tmp_path):
    """This test executes some possible inputs to FPDF#set_display_mode."""
    doc = fpdf.FPDF()
    document_operations(doc)
    doc.set_display_mode(zoom="default", layout=layout_input)
    assert_pdf_equal(doc, HERE / f"catalog-layout-{layout_input}.pdf",
                     tmp_path)
Exemple #14
0
 def test_set_core_font(self):
     pdf = FPDF()
     pdf.add_page()
     for i, font_name in enumerate(pdf.core_fonts.keys()):
         pdf.set_font(font_name, "", 36)
         pdf.text(10, 10 + 10 * i, "Hello World!")
     assert_pdf_equal(self, pdf, "test_set_core_font.pdf")
Exemple #15
0
 def test_nominal_csv(self, tmp_path):
     "Taken from docs/Templates.md"
     tmpl = Template(format="A4", title="Sample Invoice")
     tmpl.parse_csv(HERE / "mycsvfile.csv", delimiter=";")
     tmpl.add_page()
     tmpl["company_name"] = "Sample Company"
     assert_pdf_equal(tmpl, HERE / "template_nominal_csv.pdf", tmp_path)
Exemple #16
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")
Exemple #17
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")
Exemple #18
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")
Exemple #19
0
 def test_setting_all_layout(self):
     for layout_input in ["single", "continuous", "two", "default"]:
         with self.subTest(layout_input=layout_input):
             doc = fpdf.FPDF()
             document_operations(doc)
             doc.set_display_mode(zoom="default", layout=layout_input)
             assert_pdf_equal(self, doc, f"catalog-layout-{layout_input}.pdf")
Exemple #20
0
 def test_png_url(self, tmp_path):
     pdf = fpdf.FPDF()
     pdf.compress = False
     pdf.add_page()
     png = "https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png"
     pdf.image(png, x=15, y=15, w=30, h=25)
     assert_pdf_equal(pdf, HERE / "image_png_url.pdf", tmp_path)
Exemple #21
0
def test_multi_cell_ln_3_table(tmp_path):
    """
    Test rendering of a table with multi-lines cell contents
    cf. https://github.com/PyFPDF/fpdf2/issues/63
    """
    pdf = fpdf.FPDF()
    pdf.add_page()
    pdf.set_font("Times", size=10)
    data = (
        ("First name", "Last name", "Age", "City"),
        ("Juleskfgjhfdkgfdhkghfdghfd;grdfgdfhghf", "Smith", 34, "San Juan"),
        ("Marydgfsfsfgdgvfdggfd", "Ramos", 45, "Orlando"),
        ("Carlsonfdgfdgsfdxhfggjdfgfgu", "Banks", 19, "Los Angeles"),
    )
    line_height = pdf.font_size * 2.5
    # Set column width to 1/4 of effective page width to distribute content
    # evenly across table and page
    col_width = pdf.epw / 4
    for row in data:
        for datum in row:
            pdf.multi_cell(
                col_width,
                line_height,
                str(datum),
                border=1,
                ln=3,
                max_line_height=pdf.font_size,
            )
        pdf.ln(line_height)
    assert_pdf_equal(pdf, HERE / "multi_cell_ln_3_table.pdf", tmp_path)
Exemple #22
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")
Exemple #23
0
 def test_interleaved2of5(self, tmp_path):
     pdf = FPDF()
     pdf.add_page()
     pdf.interleaved2of5("1337", x=65, y=50, w=4, h=20)
     pdf.set_font("courier", "B", size=36)
     pdf.text(x=80, y=80, txt="1337")
     assert_pdf_equal(pdf, HERE / "barcodes_interleaved2of5.pdf", tmp_path)
Exemple #24
0
def test_setting_old_date(tmp_path):
    doc = fpdf.FPDF()
    doc.add_page()
    # 2017, April 18th, almost 7:09a
    date = datetime(2017, 4, 18, 7, 8, 55)
    doc.set_creation_date(date)
    assert_pdf_equal(doc, HERE / "setting_old_date.pdf", tmp_path)
Exemple #25
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")
Exemple #26
0
def test_add_font_unicode_with_str_fname_ok(tmp_path):
    pdf = FPDF()
    font_file_path = HERE / "../end_to_end_legacy/charmap/Roboto-Regular.ttf"
    pdf.add_font("Roboto-Regular", fname=str(font_file_path), uni=True)
    pdf.set_font("Roboto-Regular", size=64)
    pdf.add_page()
    pdf.cell(pdf.epw, 20, "Hello World!")
    assert_pdf_equal(pdf, HERE / "add_font_unicode.pdf", tmp_path)
Exemple #27
0
 def test_alias_nb_pages(self, tmp_path):
     pdf = fpdf.FPDF()
     pdf.set_font("Times")
     pdf.add_page()
     pdf.cell(0, 10, f"Page {pdf.page_no()}/{{nb}}", align="C")
     pdf.add_page()
     pdf.cell(0, 10, f"Page {pdf.page_no()}/{{nb}}", align="C")
     assert_pdf_equal(pdf, HERE / "alias_nb_pages.pdf", tmp_path)
Exemple #28
0
 def test_jpg_url(self, tmp_path):
     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)
     assert_pdf_equal(pdf, HERE / "image_jpg_url.pdf", tmp_path)
Exemple #29
0
def test_recorder_replay_ok(tmp_path):
    recorder = FPDFRecorder(init_pdf())
    recorder.add_page()
    recorder.cell(w=recorder.epw, h=10, txt="Hello again!", align="C")
    expected = recorder.output()
    recorder.rewind()
    recorder.replay()
    assert_pdf_equal(recorder, expected, tmp_path)
Exemple #30
0
def test_multi_cell_ln_3(tmp_path):
    doc = fpdf.FPDF()
    doc.add_page()
    doc.set_font("helvetica", size=TEXT_SIZE)
    doc.multi_cell(w=45, h=LINE_HEIGHT, border=1, ln=3, txt="Lorem")
    doc.multi_cell(w=45, h=LINE_HEIGHT, border=1, ln=3, txt="ipsum")
    doc.multi_cell(w=45, h=LINE_HEIGHT, border=1, ln=3, txt="Ut")
    doc.multi_cell(w=45, h=LINE_HEIGHT, border=1, ln=3, txt="nostrud")
    assert_pdf_equal(doc, HERE / "multi_cell_ln_3.pdf", tmp_path)