File: form.py

package info (click to toggle)
fpdf2 2.8.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,860 kB
  • sloc: python: 39,487; sh: 133; makefile: 12
file content (127 lines) | stat: -rw-r--r-- 3,441 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
An example of script that generates a one page invoice with barcode,
with data coming from a CSV file.
"""

import os

from fpdf import FPDF


class Form:
    def __init__(self, infile):
        keys = (
            "name",
            "type",
            "x1",
            "y1",
            "x2",
            "y2",
            "font",
            "size",
            "bold",
            "italic",
            "underline",
            "foreground",
            "background",
            "align",
            "text",
            "priority",
        )
        # parse form format file and create fields dict
        self.fields = {}
        with open(infile, encoding="utf8") as file:
            for linea in file.readlines():
                kargs = {}
                for i, v in enumerate(linea.split(";")):
                    if v in ("", "None"):
                        v = None
                    elif v.startswith("'"):
                        v = v.strip()[1:-1]
                    else:
                        v = float(v.replace(",", "."))
                    kargs[keys[i]] = v
                self.fields[kargs["name"].lower()] = kargs
        self.handlers = {
            "T": self.text,
            "L": self.line,
            "I": self.image,
            "B": self.rect,
            "BC": self.barcode,
        }

    def set(self, name, value):
        if name.lower() in self.fields:
            self.fields[name.lower()]["text"] = value

    def render(self, outfile):
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font("helvetica", style="B", size=16)

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

        pdf.output(outfile)

    @staticmethod
    def text(
        pdf,
        *_,
        x1=0,
        y1=0,
        x2=0,
        y2=0,
        text="",
        font="helvetica",
        size=10,
        bold=False,
        italic=False,
        underline=False,
        align="",
        **__
    ):
        if text:
            font = font.strip().lower()
            style = ""
            if bold:
                style += "B"
            if italic:
                style += "I"
            if underline:
                style += "U"
            align = {"I": "L", "D": "R", "C": "C", "": "", None: None}[align]
            pdf.set_font(font, style, size)
            # m_k = 72 / 2.54
            # h = (size/m_k)
            pdf.set_xy(x1, y1)
            pdf.cell(w=x2 - x1, h=y2 - y1, txt=text, align=align)
            # pdf.Text(x=x1,y=y1,txt=text)

    @staticmethod
    def line(pdf, *_, x1=0, y1=0, x2=0, y2=0, size=0, **__):
        pdf.set_line_width(size)
        pdf.line(x1, y1, x2, y2)

    @staticmethod
    def rect(pdf, *_, x1=0, y1=0, x2=0, y2=0, size=0, **__):
        pdf.set_line_width(size)
        pdf.rect(x1, y1, x2 - x1, y2 - y1)

    @staticmethod
    def image(pdf, *_, x1=0, y1=0, x2=0, y2=0, text="", **__):
        pdf.image(text, x1, y1, w=x2 - x1, h=y2 - y1)

    @staticmethod
    def barcode(pdf, *_, x1=0, y1=0, text="", font="helvetica", size=1, **__):
        font = font.lower().strip()
        if font == "interleaved 2of5 nt":
            pdf.interleaved2of5(text, x1, y1, w=size)


if __name__ == "__main__":
    os.chdir(os.path.dirname(os.path.realpath(__file__)))
    f = Form("invoice.csv")
    f.set("EMPRESA", "Saraza")
    f.set("logo", "logo.png")
    f.render("./invoice.pdf")