def test_constructor(self): with self.assertRaises(Exception) as e: fpdf.FPDF(unit="smiles") self.assertEqual(str(e.exception), "Incorrect unit: smiles") self.assertAlmostEqual(fpdf.FPDF(unit="pt").k, 1) self.assertAlmostEqual(fpdf.FPDF(unit="mm").k, 2.83464566929) self.assertAlmostEqual(fpdf.FPDF(unit="cm").k, 28.3464566929) self.assertAlmostEqual(fpdf.FPDF(unit="in").k, 72.0)
def test_constructor(self): with pytest.raises(FPDFException) as e: fpdf.FPDF(unit="smiles") assert str(e.value) == "Incorrect unit: smiles" assert fpdf.FPDF(unit="pt").k == pytest.approx(1) assert fpdf.FPDF(unit="mm").k == pytest.approx(2.83464566929) assert fpdf.FPDF(unit="cm").k == pytest.approx(28.3464566929) assert fpdf.FPDF(unit="in").k == pytest.approx(72)
def test_units(): with pytest.raises(ValueError) as e: fpdf.FPDF(unit="smiles") assert str(e.value) == "Incorrect unit: smiles" assert fpdf.FPDF(unit="pt").k == pytest.approx(1) assert fpdf.FPDF(unit="mm").k == pytest.approx(2.83464566929) assert fpdf.FPDF(unit="cm").k == pytest.approx(28.3464566929) assert fpdf.FPDF(unit="in").k == pytest.approx(72)
def test_constructor(self): with self.assertRaises(Exception) as e: fpdf.FPDF(unit='smiles') expected = 'FPDF error' contains = expected in str(e.exception) self.assertTrue(contains) self.assertEqual(str(e.exception), 'FPDF error: Incorrect unit: smiles') self.assertAlmostEqual(fpdf.FPDF(unit='pt').k, 1) self.assertAlmostEqual(fpdf.FPDF(unit='mm').k, 2.83464566929) self.assertAlmostEqual(fpdf.FPDF(unit='cm').k, 28.3464566929) self.assertAlmostEqual(fpdf.FPDF(unit='in').k, 72.0)
def test_error(self): with self.assertRaises(fpdf.errors.FPDFPageFormatException) as e: fpdf.fpdf.get_page_format('letter1') self.assertTrue('FPDFPageFormatException' in str(e.exception)) self.assertTrue('Unknown page format' in str(e.exception)) self.assertTrue('letter1' in str(e.exception)) with self.assertRaises(fpdf.errors.FPDFPageFormatException) as e: fpdf.fpdf.get_page_format(3) self.assertTrue('FPDFPageFormatException' in str(e.exception)) self.assertTrue('Only one argument given' in str(e.exception)) with self.assertRaises(fpdf.errors.FPDFPageFormatException) as e: fpdf.fpdf.get_page_format(4, 'a') self.assertTrue('FPDFPageFormatException' in str(e.exception)) self.assertTrue('Arguments must be numbers: ' in str(e.exception)) pdf = fpdf.FPDF() a = 'ali' b = pdf.alias_nb_pages(a) self.assertEqual(b, a) self.assertEqual(pdf.str_alias_nb_pages, a)
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)
def test_add_page_throws_without_page(): pdf = fpdf.FPDF() with pytest.raises(FPDFException) as e: pdf.text(1, 2, "ok") msg = "No page open, you need to call add_page() first" assert str(e.value) == msg
def test_portrait_landscape(self): l = fpdf.FPDF(orientation="l") landscape = fpdf.FPDF(orientation="landscape") p = fpdf.FPDF(orientation="p") portrait = fpdf.FPDF(orientation="portrait") self.assertEqual(l.w_pt, landscape.w_pt) self.assertEqual(l.h_pt, landscape.h_pt) self.assertEqual(l.def_orientation, landscape.def_orientation) self.assertEqual(p.w_pt, portrait.w_pt) self.assertEqual(p.h_pt, portrait.h_pt) self.assertEqual(p.def_orientation, portrait.def_orientation) self.assertEqual(landscape.w_pt, portrait.h_pt) self.assertEqual(landscape.h_pt, portrait.w_pt)
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)
def test_alt_text_and_title(tmp_path): pdf = fpdf.FPDF() pdf.add_page() pdf.image(IMG_FILE_PATH, alt_text=IMG_DESCRIPTION) pdf.image(IMG_FILE_PATH, title=IMG_DESCRIPTION) pdf.image(IMG_FILE_PATH, alt_text=IMG_DESCRIPTION, title=IMG_DESCRIPTION) assert_pdf_equal(pdf, HERE / "alt_text_and_title.pdf", tmp_path)
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)
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")
def make_pdf ( filename, text ) : pdf = fpdf.FPDF(format = "letter") pdf.set_font("Arial",size = 12) pdf.add_page() pdf.cell(200,10,txt = str(text), align = "C") pdf.output(filename) pdf.close()
def create_new_pdf(data, output_pdf, output_txt): pdf = fpdf.FPDF(format='letter') pdf.add_page() pdf.add_font('DejaVu', '', './font/DejaVuSansCondensed.ttf', uni=True) pdf.set_font('DejaVu', '', 12) fl_output = open(output_txt, "w+", encoding="utf-8") counter = 1 for match in data: url, text = convert_tweet_to_url(match.group(1)) text_with_number = f"{counter}. {text}" # write on pdf pdf.write(5, txt=text_with_number, link=url) pdf.write(5, txt="\n\n") # write on txt file fl_output.write(url) fl_output.write("\n\n") counter += 1 # output pdf.output(output_pdf)
def convertTextToPdf(text): pdf = fpdf.FPDF(format='letter') pdf.add_page() pdf.set_font("Arial", size=12) pdf.write(5, text) pdf.ln() pdf.output("temporary.pdf")
def withdraw_reciept(new_amount, withdrawed_amount): """This function create a recipt bill for withdraw balance and open the file. parameters: new_amount: the amount of money after depsoit withdrawd_amount: the amount of desposited money """ id = current_user().id name = current_user().name new_amount = number_format(new_amount) withdrawed_amount = number_format(withdrawed_amount) recipt = fpdf.FPDF('P', 'mm', (115, 115)) recipt.add_page() recipt.set_font('Arial', '', 9) text1 = "Welcome To Banking System".center(75, "-") text2 = f"User ID: {id} \n Name: {name} \n Date: {datetime.now()} \n" text3 = f"Withdraw Money \n" text4 = f"Withdrawed Amount: {withdrawed_amount} Afg \n" text5 = f"New Balance: {new_amount} Afg\n" text6 = "Thank You".center(80, "-") text = f"{text1} \n {text2} {text3} {text4} {text5} {text6}" recipt.multi_cell(100, 10, text, border=0, align='c') recipt.output(f"{id}{name}-recipt.pdf") #open the recipt file run_file(f"{id}{name}-recipt.pdf")
def export(self): filename = filedialog.asksaveasfilename( title="lipu pana li seme?", parent=self, defaultextension='.pdf', filetypes=(("lipu .pdf", "*.pdf"), ("lipu ali", "*.*"))) try: pdf = fpdf.FPDF() pdf.add_page() try: pdf.add_font( 'sitelen luka tu tu', '', (r'C:\Windows\Fonts\sitelen_luka_tu_tu.ttf' if os.name == 'nt' else '/usr/share/fonts/sitelen_luka_tu_tu.ttf'), uni=True) except RuntimeError: pdf.add_font( 'sitelen luka tu tu', '', (r'C:\Windows\Fonts\sitelen_luka_tu_tu.ttf_' if os.name == 'nt' else '/usr/share/fonts/sitelen_luka_tu_tu_.ttf'), uni=True) pdf.set_font('sitelen luka tu tu', size=20) for t in self.text.get('1.0', tk.END).split('\n'): pdf.cell(200, 10, txt=t, ln=1, align='L') pdf.output(filename) except Exception as e: messagebox.showerror('pakala!', 'ni li ike:\n' + str(e)) else: messagebox.showinfo('pona!', 'lipu li pana!')
def test_portrait_landscape(self): l = fpdf.FPDF(orientation='l') landscape = fpdf.FPDF(orientation='landscape') p = fpdf.FPDF(orientation='p') portrait = fpdf.FPDF(orientation='portrait') self.assertEqual(l.w_pt, landscape.w_pt) self.assertEqual(l.h_pt, landscape.h_pt) self.assertEqual(l.def_orientation, landscape.def_orientation) self.assertEqual(p.w_pt, portrait.w_pt) self.assertEqual(p.h_pt, portrait.h_pt) self.assertEqual(p.def_orientation, portrait.def_orientation) self.assertEqual(landscape.w_pt, portrait.h_pt) self.assertEqual(landscape.h_pt, portrait.w_pt)
def test_dash_pattern(tmp_path): pdf = fpdf.FPDF() pdf.add_page() pdf.set_font("helvetica", "", 10) def draw_stuff(x, y): pdf.line(x, y, x + 50, y + 50) pdf.polyline( ((x, y), (x + 40, y + 20), (x + 10, y + 30), (x + 50, y + 50))) pdf.polygon(((x + 5, y + 20), (x + 25, y + 45), (x + 40, y + 10))) pdf.rect(x, y, 50, 50) pdf.ellipse(x, y, 50, 50) pdf.set_xy(x, y + 55) pdf.cell(w=50, h=5, txt="cell", border=1) # solid line draw_stuff(20, 20) # simple dash pdf.set_dash_pattern(3) draw_stuff(100, 20) # dashdot by overlap pdf.set_dash_pattern(4, 6) draw_stuff(20, 100) pdf.set_dash_pattern(0.5, 9.5, 3.25) # coverage: repeating the same pattern should not add it again pdf.set_dash_pattern(0.5, 9.5, 3.25) draw_stuff(20, 100) # reset to solid pdf.set_dash_pattern() draw_stuff(100, 100) assert_pdf_equal(pdf, HERE / "dash_pattern.pdf", tmp_path)
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)
def test_alt_text_on_two_pages(tmp_path): pdf = fpdf.FPDF() pdf.add_page() pdf.image(IMG_FILE_PATH, alt_text=IMG_DESCRIPTION) pdf.add_page() pdf.image(IMG_FILE_PATH, alt_text=IMG_DESCRIPTION) assert_pdf_equal(pdf, HERE / "test_alt_text_on_two_pages.pdf", tmp_path)
def test_insert_png(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)
def test_throws_without_page(self): pdf = fpdf.FPDF() with self.assertRaises(Exception) as e: pdf.text(1, 2, "ok") msg = "No page open, you need to call add_page() first" self.assertEqual(str(e.exception), msg)
def test_insert_pillow(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, h=140) assert_pdf_equal(pdf, HERE / "image_types_insert_png.pdf", tmp_path)
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)
def test_orientation_portrait_landscape(): l = fpdf.FPDF(orientation="l") landscape = fpdf.FPDF(orientation="landscape") p = fpdf.FPDF(orientation="p") portrait = fpdf.FPDF(orientation="portrait") assert l.w_pt == landscape.w_pt assert l.h_pt == landscape.h_pt assert l.def_orientation == landscape.def_orientation assert p.w_pt == portrait.w_pt assert p.h_pt == portrait.h_pt assert p.def_orientation == portrait.def_orientation assert landscape.w_pt == portrait.h_pt assert landscape.h_pt == portrait.w_pt
def test_insert_gif(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)
def merge_images_to_pdf(images_path: list[str], output_path: str): pdf = fpdf.FPDF() for img_path in images_path: pdf.add_page() print(f"Embedding image: {img_path}") pdf.image(img_path, x=0, y=0, w=210, h=297) pdf.output(output_path, "F")
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")
def test_insert_bmp(self, 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)