Skip to main content

PDF417 2D barcode generator for Python

Project description

https://img.shields.io/travis/ihabunek/pdf417-py.svg?maxAge=3600&style=flat-square https://img.shields.io/badge/author-%40ihabunek-blue.svg?maxAge=3600&style=flat-square https://img.shields.io/github/license/ihabunek/pdf417-py.svg?maxAge=3600&style=flat-square https://img.shields.io/pypi/v/pdf417gen.svg?maxAge=3600&style=flat-square

Easily encode your data into a 2D barcode using the PDF417 format.

https://raw.githubusercontent.com/ihabunek/pdf417-py/master/images/1_basic.jpg

Licensed under the MIT License, see LICENSE.

Installation

Install using pip:

pip install pdf417gen

Usage

Creating bar codes is done in two steps:

  • Encode a string to a list of code words using encode()

  • Render the barcode using one of the rendering functions: render_image(), render_svg().

Usage overview:

from pdf417gen import encode, render_image, render_svg

# Some data to encode
text = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated."""

# Convert to code words
codes = encode(text)

# Generate barcode as image
image = render_image(codes)  # Pillow Image object
image.save('barcode.jpg')

# Generate barcode as SVG
svg = render_svg(codes)  # ElementTree object
svg.write("barcode.svg")

Encoding data

The first step is to encode your data to a list of code words.

encode(data, columns=6, security_level=2˙)

Columns

The bar code size can be customized by defining the number of columns used to render the data, between 1 and 30, the default value is 6. A bar code can have a maximum of 90 rows, so for larger data sets you may need to increase the number of columns to decrease the rows count.

codes = encode(text, columns=12)
image = render_image(codes)
image.show()
https://raw.githubusercontent.com/ihabunek/pdf417-py/master/images/2_columns.jpg

Security level

Increasing the security level will produce stronger (and more numerous) error correction codes, making the bar code larger, but less prone to corruption. The security level can range from 0 to 8, and procuces 2^(level+1) error correction code words, meaning level 0 produces 2 code words and level 8 produces 512. The default security level is 2.

codes = encode(text, columns=12, security_level=6)
image = render_image(codes)
image.show()
https://raw.githubusercontent.com/ihabunek/pdf417-py/master/images/3_security_level.jpg

Render image

The render_image function takes the following options:

  • scale - module width, in pixels (default: 3)

  • ratio - module height to width ratio (default: 3)

  • padding - image padding, in pixels (default: 20)

  • fg_color - foreground color (default: #000000)

  • bg_color - background color (default: #FFFFFF)

The function returns a Pillow Image object containing the barcode.

Colors can be specified as hex codes or using HTML color names.

codes = encode(text, columns=3)
image = render_image(codes, scale=5, ratio=2, padding=5, fg_color="Indigo", bg_color="#ddd")
image.show()
https://raw.githubusercontent.com/ihabunek/pdf417-py/master/images/4_rendering.jpg

Render SVG

The render_svg function takes the following options:

  • scale - module width, in pixels (default: 3)

  • ratio - module height to width ratio (default: 3)

  • padding - image padding, in pixels (default: 20)

  • color - foreground color (default: #000000)

The function returns a ElementTree object containing the barcode in SVG format.

Unlike render_image, this function does not take a background color option. The background is left transparent.

codes = encode(text, columns=3)
svg = render_svg(codes, scale=5, ratio=2, color="Seaweed")
svg.write('barcode.svg')

## See also

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdf417gen-0.3.0.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdf417gen-0.3.0-py2.py3-none-any.whl (22.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pdf417gen-0.3.0.tar.gz.

File metadata

  • Download URL: pdf417gen-0.3.0.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pdf417gen-0.3.0.tar.gz
Algorithm Hash digest
SHA256 420e3e11c3dc106b8420d073306f4df877c696b9a36316731f2a50985390087f
MD5 e8a95a0c26f0418d44c5427bdd9a5cad
BLAKE2b-256 3a28efc0aa7d2009e85306df2b377fea2225284480fd735563b179776431cd94

See more details on using hashes here.

File details

Details for the file pdf417gen-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pdf417gen-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f192c54031f675bc39934d76fa766c8f496f3a1d404b15fc2760519641c4dfbb
MD5 df380af20295d8f6fb745ffb9dc739a0
BLAKE2b-256 d65fe9135edfe7a349a38c04668b2c991042a354b312a82ecb8bf29ca2bbffdf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page