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): 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(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): # 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() 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() 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 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.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) 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): gaefpdf.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): plane = genbar() palette = (0, 0, 0, 255, 255, 255) + (128, 128, 128) * 254 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=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"