Skip to main content

PDF417 barcode generator for Python

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

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

https://raw.githubusercontent.com/mosquito/pdf417/master/images/1_basic.jpg

Licensed under the MIT License, see LICENSE.

Installation

Install using pip:

pip install pdf417

CLI

The pdf417gen command can be used to generate a barcode from commandline. It takes the input either as an argument or from stdin.

# Show help
pdf417gen encode --help

# Encode given text and display the barcode
pdf417gen encode "Beautiful is better than ugly"

# Encode given text and save barcode to a file (extension determines format)
pdf417gen encode -o barcode.png "Explicit is better than implicit"

# Input from a file
pdf417gen encode < input.txt

# Piped input
python -c "import this" | pdf417gen encode

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 pdf417 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")

Supports strings (unicode in py2) and byte arrays (str in py2):

# These two inputs encode to the same code words
encode(u"love 💔")
encode(b"love \xf0\x9f\x92\x94")

# Default encoding is UTF-8, but you can specify your own
encode(u"love 💔", encoding="utf-8")

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/mosquito/pdf417/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/mosquito/pdf417/master/images/3_security_level.jpg

Auto Numeric Compaction Mode

This mode can pack almost 3 digits (2.93) info a symbol character. The words with length less 13 symbols will be invoked as Text

codes = encode(text, numeric_compaction=True)

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/mosquito/pdf417/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

Metadata

Release files for pdf417 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdf417 0.8.1
File Size Uploaded Provenance Actions
pdf417-0.8.1.tar.gz 24.0 kB Without provenance Details for pdf417-0.8.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pdf417 0.8.1
File Interpreter ABI Platform Actions
pdf417-0.8.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for pdf417-0.8.1-py2.py3-none-any.whl

Total release size: 47.7 kB

Release files / pdf417-0.8.1.tar.gz

Download URL pdf417-0.8.1.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2d75d5f6e3c1e89bb5cf2b01ddb1c69dd0db1f3dfeb403dab8745e64f95b3758
BLAKE2b-256 checksum
How to use checksums
e81e13b568b43e610b462613b83ff55f938d1520f799c31ac0d2c01b3089c61e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

Release files / pdf417-0.8.1-py2.py3-none-any.whl

Download URL pdf417-0.8.1-py2.py3-none-any.whl
Size 23.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7d29114a87e19454a3e0e10aeb2297db46429e4ac7fb92671f6632455f0f76d3
BLAKE2b-256 checksum
How to use checksums
33190c89e3264ecba4733fa5f5bc6bf4abe332512c60de59d622f7c2359e1cd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

Security for pdf417 0.8.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page