def dotest(outputname, nostamp): try: from bidi.algorithm import get_display except ImportError: from unittest import SkipTest raise SkipTest("Need python-bidi") pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.compress = False pdf.add_page() pdf.add_font('DejaVu', '', \ os.path.join(common.basepath, 'font/DejaVuSans.ttf'), uni=True) pdf.set_font('DejaVu', '', 14) # this will be displayed wrong as actually it is stored LTR: text= u"این یک متن پارسی است. This is a Persian text !!" pdf.write(8, text) pdf.ln(8) # Reverse the RLT using the Bidirectional Algorithm to be displayed correctly: # (http://unicode.org/reports/tr9/) rtl_text = get_display(text) pdf.write(8, rtl_text) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): # filename - output filename # nostamp - do no use stamp in result file pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 16) pdf.write(8, "Test template") pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF(orientation = "L", format = "A5") pdf.compress = False if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.set_font('Arial', '', 14) for i in range(10): o = ["p", "L", "P", "l"][i % 4] page(pdf, "Page %d from 10\nOrientation: %s" % (i + 1, o), o) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.dashed_line(10, 10, 110, 10) pdf.dashed_line(10, 20, 110, 20, 5, 5) pdf.dashed_line(10, 30, 110, 30, 1, 10) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() for i in range(1,41): # for flow mode, do not pass x or y: pdf.image(os.path.join(common.basepath, '../tutorial/logo_pb.png')) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF(orientation = "L", format = (100, 250)) pdf.compress = False if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.set_font('Arial', '', 14) for i in range(16): o = ["p", "l"][i % 2] f = ["a3", "a4", "a5", "letter", "legal", "", (100, 250), (320, 240)][i % 8] page(pdf, "Page %d from 16\nFormat: %s\nOrientation: %s" % (i + 1, f, o), o, f) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): # Portrait, millimeter units, A4 page size pdf = FPDF("P", "mm", "A4") if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) # Set font: Times, normal, size 10 pdf.add_page() if not nostamp: # do not show picture in batch url = "http://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Code_3_of_9.svg/262px-Code_3_of_9.svg.png" pdf.image(url, 10, 10) pdf.code39("*wikipedia*", 12.75, 7, 1.49) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() # Add a Unicode font (uses UTF-8) pdf.add_font("DejaVu", "", os.path.join(common.basepath, "font", "DejaVuSansCondensed.ttf"), uni=True) pdf.set_font("DejaVu", "", 14) # Load a UTF-8 string from a file and print it with open(os.path.join(common.basepath, "HelloWorld.txt"), "rb") as file: txt = file.read().decode("UTF-8") pdf.write(8, txt) pdf.output(outputname, "F")
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 14) pdf.text(10, 57, 'DeviceGray') pdf.image(os.path.join(common.basepath, "img_gray.jpg"), 55, 5) pdf.text(10, 157, 'DeviceRGB') pdf.image(os.path.join(common.basepath, "img_rgb.jpg"), 55, 105) pdf.text(10, 257, 'DeviceCMYK') pdf.image(os.path.join(common.basepath, "img_cmyk.jpg"), 55, 205) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() # Add a Unicode font (uses UTF-8) pdf.add_font('DejaVu', '', \ os.path.join(common.basepath, "font", 'DejaVuSansCondensed.ttf'), \ uni = True) pdf.set_font('DejaVu', '', 14) # Load a UTF-8 string from a file and print it txt = open(os.path.join(common.basepath, "HelloWorld.txt"), "rb").\ read().decode("UTF-8") pdf.write(8, txt) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 14) pdf.ln(10) if nostamp: data = "TEST-TEST-TEST" else: data = sys.version #áéíóúüñ # This string converted with errors in py2.x pdf.write(5, ('hello world %s áéíóúüñ' % data)) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 14) pdf.ln(10) if nostamp: data = "TEST-TEST-TEST" else: data = sys.version pdf.write(5, 'hello world %s' % data) pdf.image("../tutorial/logo.png", 50, 50) pdf.image("flower2.jpg", 100, 50) pdf.image("lena.gif", 50, 75) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 14) pdf.ln(10) if nostamp: data = "TEST-TEST-TEST" else: data = sys.version pdf.write(5, 'hello world %s' % data) path = os.path.join(common.basepath, os.pardir, "tutorial", "logo.png") pdf.image(path, 50, 50) pdf.image(os.path.join(common.basepath, "flower2.jpg"), 100, 50) pdf.image(os.path.join(common.basepath, "lena.gif"), 50, 75) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() # Add a Unicode font (uses UTF-8) pdf.add_font('DejaVu', '', \ os.path.join(common.basepath, "font", 'DejaVuSansCondensed.ttf'), \ uni = True) pdf.set_font('DejaVu','',14) # Load a UTF-8 string from a file and print it txt = open(os.path.join(common.basepath, "HelloWorld.txt"), "rb").\ read().decode("UTF-8") pdf.write(8, txt) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): plane = genbar() palette = (0,0,0, 255,255,255) + (128,128,128)*254 try: img = Image.frombytes("P", plane.size, plane.tobytes()) except AttributeError: # note: https://github.com/python-pillow/Pillow/issues/63 img = Image.fromstring("P", plane.size, plane.tostring()) img.putpalette(palette) with tempfile.NamedTemporaryFile(delete = False, suffix = ".gif") as f: gif1 = f.name with tempfile.NamedTemporaryFile(delete = False, suffix = ".gif") as f: gif2 = f.name img.save(gif1, "GIF", optimize = 0) img.save(gif2, "GIF", transparency = 1, optimize = 0) pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 16) pdf.write(8, "Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.image(gif1, x = 15, y = 15) pdf.write(8, "Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.image(gif2, x = 15, y = 39) pdf.output(outputname, 'F') os.unlink(gif1) os.unlink(gif2)
def dotest(outputname, nostamp): plane = genbar() palette = (0, 0, 0, 255, 255, 255) + (128, 128, 128) * 254 try: img = Image.frombytes("P", plane.size, plane.tobytes()) except AttributeError: # note: https://github.com/python-pillow/Pillow/issues/63 img = Image.fromstring("P", plane.size, plane.tostring()) img.putpalette(palette) with tempfile.NamedTemporaryFile(delete=False, suffix=".gif") as f: gif1 = f.name with tempfile.NamedTemporaryFile(delete=False, suffix=".gif") as f: gif2 = f.name img.save(gif1, "GIF", optimize=0) img.save(gif2, "GIF", transparency=1, optimize=0) pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 16) pdf.write(8, "Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.image(gif1, x=15, y=15) pdf.write(8, "Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.image(gif2, x=15, y=39) pdf.output(outputname, 'F') os.unlink(gif1) os.unlink(gif2)
def dotest(outputname, nostamp): plane = genbar() palette = (0,0,0, 255,255,255) + (128,128,128)*254 img = Image.fromstring("P", plane.size, plane.tostring()) img.putpalette(palette) f = tempfile.NamedTemporaryFile(delete = False, suffix = ".gif") gif1 = f.name f.close() f = tempfile.NamedTemporaryFile(delete = False, suffix = ".gif") gif2 = f.name f.close() img.save(gif1, "GIF") img.save(gif2, "GIF", transparency = 1) pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 16) pdf.write(8, "Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.image(gif1, x = 15, y = 15) pdf.write(8, "Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.image(gif2, x = 15, y = 39) pdf.output(outputname, 'F') os.unlink(gif1) os.unlink(gif2)
def dotest(outputname, nostamp): plane = genbar() palette = (0,0,0, 255,255,255) + (128,128,128)*254 img = Image.fromstring("P", plane.size, plane.tostring()) img.putpalette(palette) f = tempfile.NamedTemporaryFile(delete = False, suffix = ".gif") gif1 = f.name f.close() f = tempfile.NamedTemporaryFile(delete = False, suffix = ".gif") gif2 = f.name f.close() img.save(gif1, "GIF", optimize = 0) img.save(gif2, "GIF", transparency = 1, optimize = 0) pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 16) pdf.write(8, "Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.image(gif1, x = 15, y = 15) pdf.write(8, "Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.write(8, " Transparency") pdf.ln() pdf.image(gif2, x = 15, y = 39) pdf.output(outputname, 'F') os.unlink(gif1) os.unlink(gif2)
def dotest(outputname, nostamp): fpdf.set_global("FPDF_CACHE_MODE", 1) pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() # Add a Unicode font (uses UTF-8) pdf.add_font('DejaVu', '', \ os.path.join(common.basepath, "font", 'DejaVuSans.ttf'), \ uni = True) pdf.set_font('DejaVu', '', 14) with open(os.path.join(common.basepath, "HelloWorld.txt"), "rb") as file: txt = file.read().decode("UTF-8") if not nostamp: text(pdf, txt, 100, nostamp) text(pdf, txt, 75, nostamp) text(pdf, txt, 125, nostamp) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF(orientation="L", unit="in") if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() img_path = os.path.join(common.basepath, '../tutorial/logo.png') img = Image.open(img_path) w, h = img.size width = 8 height = width * (h // w) with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as f: img_new = f.name # convert to RGBA img2 = img.convert(mode="RGBA") img2.save(img_new) # create pdf pdf.image(img_new, x=1, y=1, w=width, h=height) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF(orientation = "L", unit = "in") if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() img_path = os.path.join(common.basepath, '../tutorial/logo.png') img = Image.open(img_path) w, h = img.size width = 8 height = width * (h // w) with tempfile.NamedTemporaryFile(delete = False, suffix = ".png") as f: img_new = f.name # convert to RGBA img2 = img.convert(mode = "RGBA") img2.save(img_new) # create pdf pdf.image(img_new, x = 1, y = 1, w = width, h = height) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', '', 14) for i in range(0, 270, 5): pdf.text(i % 40 + 20, i + 20, "Image masking " * 4) mask = pdf.image(os.path.join(common.basepath, "masking.png"), is_mask = True) pdf.image(os.path.join(common.basepath, "lena.gif"), 40.0, 20.0, w = 120, mask_image = mask) pdf.image(os.path.join(common.basepath, "img_gray.jpg"), 20.0, 200.0, w = 50, mask_image = mask) pdf.image(os.path.join(common.basepath, "img_rgb.jpg"), 80.0, 200.0, w = 50, mask_image = mask) pdf.image(os.path.join(common.basepath, "img_cmyk.jpg"), 140.0, 200.0, w = 50, mask_image = mask) pdf.output(outputname, 'F')
def dotest(outputname, nostamp=True): pdf = FPDF(unit="pt") pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font("Times", size=12) pdf.cell(0, 12, "Dummy content") # Get the PDF data the usual way via a real file pdf.output(outputname) with open(outputname, "rb") as file: data = file.read(1000) assert len(data) == 966, "Unexpected PDF file size" try: # Python < 3 (Python 2.5 does not have the "io" module) from cStringIO import StringIO capture = StringIO() detach = lambda: capture except ImportError: # Python >= 3.1 from io import TextIOWrapper, BytesIO # Ensure that no text encoding is actually done capture = TextIOWrapper(BytesIO(), "undefined") detach = lambda: capture.detach() # Compare data when output() writes to stdout original_stdout = sys.stdout try: sys.stdout = capture pdf.output() capture = detach() finally: sys.stdout = original_stdout assert capture.getvalue() == data, "Unexpected stdout data" # Compare data when output() returns a byte string returned = pdf.output(dest="S") assert isinstance(returned, bytes), "output() should return bytes" assert returned == data, "Unexpected PDF data returned"
def dotest(outputname, nostamp): pdf = FPDF() if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.add_font('DejaVu', '', os.path.join(common.basepath, 'font/DejaVuSansCondensed.ttf'), uni = True) pdf.set_font('DejaVu', '', 14) # Note: this line cause syntax error in Python 3.0-3.2 text = u""" Veľké písmená A Á Ä B C Č D Ď DZ DŽ E É F G H CH I Í J K L Ĺ Ľ Malé písmená a á ä b c č d ď dz dž e é f g h ch i í j k l ĺ ľ Veľké písmená M N Ň O Ó Ô P Q R Ŕ S Š T Ť U Ú V W X Y Ý Z Ž Malé písmená m n ň o ó ô p q r ŕ s š t ť u ú v w x y ý z ž """ pdf.write(8, text) pdf.ln(8) pdf.output(outputname, 'F')
def dotest(outputname, nostamp): pdf = FPDF() pdf.unifontsubset = False if nostamp: pdf._putinfo = lambda: common.test_putinfo(pdf) pdf.add_page() pdf.set_font('Arial', 'I', 8) # 1. Test: # with txt = txt.encode('latin1') in function normalize_text of module fpdf.py # and using # # txt = all_pdf_chars # # you will get # "UnicodeEncodeError: 'latin-1' codec can't encode character" # for each of the chars in chars_not_in_latin_1 # 2. Test: # with txt = txt.encode('windows-2') in function normalize_text of module fpdf.py # and using # # txt = all_pdf_chars # # you will get # "UnicodeEncodeError: 'windows-1252' codec can't encode character" # for each of the chars in chars_not_in_windows_1252 # 3. Test # with txt = txt.encode('latin1') in function normalize_text of module fpdf.py # and using # # txt = latin_1_chars # # you don't get these errors # 4. Test # with txt = txt.encode('windows-1252') in function normalize_text of module fpdf.py # and using txt = windows_1252_chars # you either don't get these errors # to summarize # all_pdf_chars = windows_1252_chars + chars_not_in_windows_1252 # all_pdf_chars = latin_1_chars + chars_not_in_latin_1 def nrOfChars(var): if var in [all_pdf_chars,windows_1252_chars,latin_1_chars]: return len(var.split('\n'))-2-6 elif var == chars_not_in_windows_1252: return len(var.split('\n'))-2 elif var == chars_not_in_latin_1: return len(var.split('\n'))-2-1 chartexts = {'all':all_pdf_chars, 'windows-1252':windows_1252_chars, 'latin1':latin_1_chars, 'not_in_windows_1252':chars_not_in_windows_1252, 'not_in_latin_1':chars_not_in_latin_1} for k in chartexts.keys(): print(k,nrOfChars(chartexts[k])) # Output: #('all', 229) #('windows-1252', 215) #('latin1', 188) #('not_in_windows_1252', 14) #('not_in_latin_1', 41) # # => not_in_latin_1 - not_in_windows_1252 = 27 pdf.write(8, txt) pdf.output(outputname, 'F')