Пример #1
0
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')
Пример #2
0
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')
Пример #3
0
    def render(self, outfile):
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font('Arial', 'B', 16)

        for field in self.fields.values():
            self.handlers[field['type'].upper()](pdf, **field)

        pdf.output(outfile, "F")
Пример #4
0
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')
Пример #5
0
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')
Пример #6
0
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')
Пример #7
0
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')
Пример #8
0
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')
Пример #9
0
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')
Пример #10
0
def dotest(outputname, nostamp):
    try:
        # Portrait, millimeter units, A4 page size     
        pdf = FPDF("P", "mm", "A4")
        # Set font: Times, normal, size 10
        pdf.set_font('Times','', 12)
        ##pdf.add_page()
        # Layout cell: 0 x 5 mm, text, no border, Left
        pdf.cell(0,5,'Input 1 : ',border=0,align="L")
        pdf.cell(0,5,'Input 2 : ', border=0,align="L")
        pdf.cell(0,5,'Recomendation : ', border=0, align="L")
        pdf.cell(0,5,'Data 1 :', border=0, align="L" )
        pdf.cell(0,5,'Data 2 :', border=0, align="L" )
        pdf.output(outputname,'F')
    except RuntimeError as e:
        assert e.args[0] == "FPDF error: No page open, you need to call add_page() first"
    else:
        raise RuntimeError("Exception not raised!")
Пример #11
0
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')
Пример #12
0
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')
Пример #13
0
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')
Пример #14
0
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')
Пример #15
0
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')
Пример #16
0
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)
Пример #17
0
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"
Пример #18
0
from gaefpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, 'Hello World!')
pdf.output('tuto1.pdf', 'F')

Пример #19
0
pdf.set_font('arial', '', 10.0)
pdf.set_xy(20.0, 238.0)
pdf.cell(ln=0, h=5.0, align='L', w=95.0, txt='Fecha Vto. CAE:', border=0)
pdf.set_xy(55.0, 238.0)
pdf.cell(ln=0, h=5.0, align='L', w=30.0, txt='19/02/2009', border=0)
pdf.set_font('arial', '', 12.0)
pdf.set_xy(125.0, 241.0)
pdf.cell(ln=0, h=9.0, align='R', w=25.0, txt='VAT 21%:', border=0)
pdf.set_font('arial', 'B', 12.0)
pdf.set_xy(145.0, 241.0)
pdf.cell(ln=0, h=9.0, align='R', w=33.0, txt='21,00', border=0)
pdf.interleaved2of5('012345678905', 20.0, 243.5, w=0.75)
pdf.set_font('arial', 'B', 12.0)
pdf.set_xy(105.0, 251.0)
pdf.cell(ln=0, h=9.0, align='R', w=73.0, txt='121,00', border=0)
pdf.set_font('arial', '', 12.0)
pdf.set_xy(125.0, 251.0)
pdf.cell(ln=0, h=9.0, align='R', w=25.0, txt='Total:', border=0)
pdf.set_line_width(0.0)
pdf.rect(155.0, 252.0, 25.0, 7.0)
pdf.set_font('courier', '', 10.0)
pdf.set_xy(20.0, 253.0)
pdf.cell(ln=0, h=7.0, align='L', w=120.0, txt='012345678905', border=0)
pdf.output('./invoice.pdf', 'F')

import sys
if sys.platform.startswith("linux"):
    os.system("xdg-open ./invoice.pdf")
else:
    os.system("./invoice.pdf")
Пример #20
0
pdf.set_font('fireflysung', '', 14)
pdf.write(8, u'Chinese: 你好世界\n')
pdf.write(8, u'Japanese: こんにちは世界\n')
pdf.ln(10)

# Add a Alee Unicode font (uses UTF-8)
# General purpose Hangul truetype fonts that contain Korean syllable
# and Latin9 (iso8859-15) characters.
pdf.add_font('eunjin', '', 'Eunjin.ttf', uni=True)
pdf.set_font('eunjin', '', 14)
pdf.write(8, u'Korean: 안녕하세요')
pdf.ln(20)

# Add a Fonts-TLWG (formerly ThaiFonts-Scalable) (uses UTF-8)
pdf.add_font('waree', '', 'Waree.ttf', uni=True)
pdf.set_font('waree', '', 14)
pdf.write(8, u'Thai: สวัสดีชาวโลก')
pdf.ln(20)

# Select a standard font (uses windows-1252)
pdf.set_font('Arial', '', 14)
pdf.ln(10)
pdf.write(5, 'This is standard built-in font')

pdf.output(fn, 'F')
import os
try:
    os.startfile(fn)
except:
    os.system("xdg-open \"%s\"" % fn)