File: writers.rst

package info (click to toggle)
python-barcode 0.15.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: python: 1,661; makefile: 23; sh: 9
file content (126 lines) | stat: -rw-r--r-- 3,432 bytes parent folder | download
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
Writers
=======

Common Writer Options
---------------------

All writer take the following options (specified as keyword arguments to
``Barcode.save(filename, options)`` or set via ``Writer.set_options(options)``, where
``options`` is a dictionary where keys are option names and values are option values to
be set).

.. note::
   See the documentation of the specific writer for special options,
   only available for this writer.

:module_width:
    The width of one barcode module in mm as *float*.
    Defaults to **0.2**.

:module_height:
    The height of the barcode modules in mm as *float*.
    Defaults to **15.0**.

:quiet_zone:
    Distance on the left and on the right from the border to the first
    (last) barcode module in mm as *float*.
    Defaults to **6.5**.

:font_path:
    Path to the font file to be used. Defaults to **DejaVuSansMono** (which
    is bundled with this package).

:font_size:
    Font size of the text under the barcode in pt as *integer*.
    Font size zero suppresses text.
    Defaults to **10**.

:text_distance:
    Distance between the barcode and the text under it in mm as *float*.
    Defaults to **5.0**.

:background:
    The background color of the created barcode as *string*.
    Defaults to **white**.

:foreground:
    The foreground and text color of the created barcode as *string*.
    Defaults to **black**.

:center_text:
    If true (the default) the text is centered under the barcode else
    left aligned.

    .. versionadded:: 0.6

.. note::
   Some barcode classes change the above defaults to fit in some kind
   of specification.

SVGWriter
---------

Renders barcodes as [optionally, compressed] SVG objects.

In addition to the common writer options you can give the following
special option.

:compress:
    Boolean value to output a compressed SVG object (.svgz).
    Defaults to ``False``

ImageWriter
-----------

.. versionadded:: 0.4b1

Renders barcodes as image. Supports all the image formats supported by Pillow.

In addition to the common writer options you can give the following special options:

:format:
    The image file format as ``str``. All formats supported by Pillow are
    valid (e.g. PNG, JPEG, BMP, ...).  Defaults to ``PNG``.

:dpi:
    DPI as ``int`` to calculate the image size in pixel. This value is
    used for all mm to px calculations.
    Defaults to ``300``

Custom writers
--------------

It's possible to create your own writer by inheriting from `barcode.writer.BaseWriter`.

In your ``__init__`` method call BaseWriter's ``__init__`` and give your callbacks for:

 - ``initialize(raw_code)``
 - ``paint_module(xpos, ypos, width, color)``
 - ``paint_text(xpos, ypos)``
 - ``finish()``

Now instantiate a new barcode and give an instance of your new writer as argument. If
you now call ``render`` on the barcode instance your callbacks get called.

Creating compressed SVGs
------------------------

Saving a compressed SVG (SVGZ):

.. code:: pycon

    >>> import barcode
    >>> ean = barcode.get('ean13', '123456789102')
    # Now we look if the checksum was added
    >>> ean.get_fullcode()
    '1234567891026'
    >>> filename = ean.save('ean13')
    >>> filename
    'ean13.svg'
    >>> options = dict(compress=True)
    >>> filename = ean.save('ean13', options)
    >>> filename
    'ean13.svgz'

Now you have ean13.svg and the compressed ean13.svgz in your current
working directory. Open it and see the result.