Example #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')
Example #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')
Example #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")
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')
Example #5
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')
Example #6
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')
Example #7
0
def dotest(outputname, nostamp):

    pdf = FPDF()
    pdf.set_font('Arial', '', 14)
    s = 'Texto largo que no cabe en esta celda pero que será ajustado'
    w = pdf.get_string_width(s)
    if not nostamp:
        print(s, w)
    assert round(w, 2) == 135.90
    font = os.path.join(common.basepath, 'font', 'DejaVuSans.ttf')
    pdf.add_font('DejaVu', '', font, uni=True)
    pdf.set_font('DejaVu', '', 14)
    s = u('Texto largo que no cabe en esta celda pero que será ajustado')
    w = pdf.get_string_width(s)
    if not nostamp:
        print(s, w)
    assert round(w, 2) == 153.64
Example #8
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!")
Example #9
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')
Example #10
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')
Example #11
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')
Example #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

    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')
Example #13
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')
Example #14
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)
Example #15
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"
Example #16
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')

Example #17
0
#!/usr/bin/env python
# -*- coding: utf8 -*-

from gaefpdf import FPDF
import sys

fn = 'unicode.pdf'

pdf = FPDF()
pdf.add_page()

# Add a DejaVu Unicode font (uses UTF-8)
# Supports more than 200 languages. For a coverage status see:
# http://dejavu.svn.sourceforge.net/viewvc/dejavu/trunk/dejavu-fonts/langcover.txt
pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True)
pdf.set_font('DejaVu', '', 14)

text = u"""
English: Hello World
Greek: Γειά σου κόσμος
Polish: Witaj świecie
Portuguese: Olá mundo
Russian: Здравствуй, Мир
Vietnamese: Xin chào thế giới
Arabic: مرحبا العالم
Hebrew: שלום עולם
"""

for txt in text.split('\n'):
    pdf.write(8, txt)
    pdf.ln(8)
Example #18
0
# -*- coding: iso-8859-1 -*-

import os
from gaefpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.set_font('helvetica', '', 13.0)
pdf.set_xy(105.0, 8.0)
pdf.cell(ln=0, h=22.0, align='C', w=75.0, txt='Sample Invoice', border=0)
pdf.set_line_width(0.0)
pdf.rect(15.0, 15.0, 170.0, 245.0)
pdf.set_line_width(0.0)
pdf.rect(95.0, 15.0, 10.0, 10.0)
pdf.image('../tutorial/logo.png', 20.0, 17.0, link='', type='', w=13.0, h=13.0)
pdf.set_font('arial', 'B', 16.0)
pdf.set_xy(95.0, 18.0)
pdf.cell(ln=0, h=2.0, align='C', w=10.0, txt='X', border=0)
pdf.set_font('arial', '', 8.0)
pdf.set_xy(105.0, 21.0)
pdf.cell(ln=0, h=4.0, align='C', w=75.0, txt='Original', border=0)
pdf.set_font('arial', 'B', 7.0)
pdf.set_xy(95.0, 21.5)
pdf.cell(ln=0, h=4.5, align='C', w=10.0, txt='COD.00', border=0)
pdf.set_line_width(0.0)
pdf.line(100.0, 25.0, 100.0, 57.0)
pdf.set_font('arial', 'B', 14.0)
pdf.set_xy(125.0, 25.5)
pdf.cell(ln=0, h=9.5, align='L', w=60.0, txt='00000001', border=0)
pdf.set_xy(115.0, 27.5)
pdf.cell(ln=0, h=5.5, align='L', w=10.0, txt='N\xba: ', border=0)
Example #19
0
#font_dir = fpdf.FPDF_FONT_DIR
font_dir = os.path.join(base, 'font')

with open(os.path.join(base, 'HelloWorld.txt')) as file:
    txt = file.read()

# Add a Unicode font (uses UTF-8)
for font in os.listdir(font_dir):
    if font.lower().endswith('.ttf'):
        fontpath = os.path.join(font_dir, font)
        print(fontpath)
        t0 = time.time()
        pdf.add_font(font, '', fontpath, uni=True)
        t1 = time.time()
        pdf.set_font(font, '', 14)
        t2 = time.time()
        pdf.write(8, font)
        pdf.ln()
        pdf.write(8, txt)
        pdf.ln()
        t3 = time.time()
        print("ttf loading time", t1 - t0)
        print("ttf total time", t3 - t0)
        print()

fn = 'unifonts.pdf'
pdf.output(fn, 'F')
import os
try:
    os.startfile(fn)
Example #20
0

pdf = FPDF()
pdf.compression = True
pdf.add_page()

#fontpath = "font/DroidSansFallback.ttf"
fontpath = "font/DejaVuSans.ttf"
pdf.add_font("font", '', fontpath, uni=True)
ttf = MyTTFontFile()
ttf.getMetrics(fontpath)

if sys.version_info >= (3, 0):
    unichr = chr

pdf.set_font("font", '', 10)

# create PDF with first 999 charters in font
cnt = 0
for char in ttf.saveChar:
    cnt += 1
    pdf.write(8, u"%03d) %06x - %c" % (cnt, char, char))
    pdf.ln()
    if cnt >= 999:
        break

fn = 'charmap.pdf'
pdf.output(fn, 'F')

try:
    os.startfile(fn)