Introduction
Aspose.BarCode FOSS for Python generates barcodes in seven symbologies and renders them to SVG and PNG. This post focuses on the rendering pipeline — how to control output format, dimensions, colors, and human-readable text using RenderOptions, SvgRenderer, and PngRenderer.
The library is available on PyPI as aspose-barcode-foss, released under the MIT license with no native dependencies. Every barcode object exposes to_svg() and to_png() convenience methods, plus a lower-level render() method that accepts any Renderer implementation.
Key Features
SVG Output with SvgRenderer
SvgRenderer produces resolution-independent SVG markup. The Barcode.to_svg() method is a shortcut that uses SvgRenderer internally.
from aspose_barcode_foss import BarcodeService, RenderOptions
from aspose_barcode_foss.rendering import SvgRenderer
service = BarcodeService()
barcode = service.generate("code128", "RENDER-TEST-001")
# Direct convenience method
svg_string = barcode.to_svg(RenderOptions(scale=2.0))
# Explicit renderer usage
renderer = SvgRenderer()
result = barcode.render(renderer, RenderOptions(scale=2.0, show_text=True))
PNG Output with PngRenderer
PngRenderer produces raster PNG images. Control resolution with the dpi property on RenderOptions. The Barcode.to_png() method is the convenience shortcut.
from aspose_barcode_foss import BarcodeService, RenderOptions
from aspose_barcode_foss.rendering import PngRenderer
service = BarcodeService()
barcode = service.generate("ean13", "590123412345")
# Convenience method — returns bytes
png_bytes = barcode.to_png(RenderOptions(dpi=300, scale=3.0))
with open("barcode.png", "wb") as f:
f.write(png_bytes)
Controlling Scale and DPI
RenderOptions provides scale (multiplier for module dimensions) and dpi (dots per inch for raster output). These work together: scale affects the logical size of bars and spaces, while dpi controls pixel density in PNG output.
from aspose_barcode_foss import RenderOptions
# High-resolution print label
print_options = RenderOptions(scale=4.0, dpi=600)
# Screen display
screen_options = RenderOptions(scale=1.0, dpi=96)
Colors and Background
Set foreground_color and background_color as CSS color strings. Enable transparent_background to omit the background fill (useful for overlaying on existing designs).
from aspose_barcode_foss import BarcodeService, RenderOptions
service = BarcodeService()
barcode = service.generate("qrcode", "https://example.com")
options = RenderOptions(
foreground_color="#003366",
background_color="#FFFFFF",
scale=3.0,
)
svg = barcode.to_svg(options)
Quiet Zone Control
The quiet_zone property sets the blank margin around the barcode in module-width units. Most barcode standards require a minimum quiet zone for reliable scanning.
from aspose_barcode_foss import BarcodeService, RenderOptions
service = BarcodeService()
barcode = service.generate("code39", "QUIET-ZONE")
# Wider quiet zone for print use
options = RenderOptions(quiet_zone=10.0, scale=2.0)
svg = barcode.to_svg(options)
Human-Readable Text
Set show_text=True to display the encoded data below the barcode. The font_family and font_size properties control the text appearance.
from aspose_barcode_foss import BarcodeService, RenderOptions
service = BarcodeService()
barcode = service.generate("upca", "01234567890")
options = RenderOptions(
show_text=True,
font_family="monospace",
font_size=12.0,
scale=2.5,
)
svg = barcode.to_svg(options)
Quick Start
pip install aspose-barcode-foss
from aspose_barcode_foss import BarcodeService, RenderOptions
service = BarcodeService()
# Generate a Code 128 barcode
barcode = service.generate("code128", "HELLO-2026")
# Render to SVG with custom options
options = RenderOptions(
scale=2.0,
show_text=True,
foreground_color="#000000",
background_color="#FFFFFF",
quiet_zone=4.0,
)
svg_output = barcode.to_svg(options)
with open("hello.svg", "w") as f:
f.write(svg_output)
# Render to PNG at 300 DPI
png_bytes = barcode.to_png(RenderOptions(dpi=300, scale=3.0))
with open("hello.png", "wb") as f:
f.write(png_bytes)
Supported Formats
| Format | Extension | Read | Write |
|---|---|---|---|
| SVG | .svg | — | ✓ |
| PNG | .png | — | ✓ |
Note: PdfRenderer exists in the API but raises NotImplementedError in the current release.
Open Source & Licensing
Aspose.BarCode FOSS for Python is released under the MIT License. The source code is hosted on GitHub. You can use, modify, and redistribute the library in both open-source and commercial projects.