How to Render Barcodes with Aspose.BarCode FOSS
Problem
After generating a barcode, you need to render it as an image file. Aspose.BarCode FOSS for Python renders barcodes to SVG and PNG formats via the to_svg() and to_png() methods on the Barcode object. The RenderOptions class controls visual properties such as scale, DPI, colors, quiet zone, and human-readable text display.
import aspose_barcode_foss as barcode
from aspose_barcode_foss import RenderOptions
bc = barcode.code128("RENDER-ME")
png = bc.to_png(options=RenderOptions(scale=2.0, dpi=300))Prerequisites
- Python 3.12 or later
- pip package manager
aspose-barcode-fossinstalled viapip install aspose-barcode-foss
SVG Output
Call to_svg() to get the barcode as an SVG string. SVG output is vector-based and scales without quality loss.
import aspose_barcode_foss as barcode
bc = barcode.qr("https://example.com")
svg_string = bc.to_svg()
with open("barcode.svg", "w") as f:
f.write(svg_string)PNG Output
Call to_png() to get the barcode as PNG bytes. PNG output is rasterized at the specified scale and DPI.
import aspose_barcode_foss as barcode
bc = barcode.code128("PNG-OUTPUT")
png_bytes = bc.to_png()
with open("barcode.png", "wb") as f:
f.write(png_bytes)Controlling Scale and DPI
Use RenderOptions to set scale (a float multiplier for module size) and dpi (dots per inch for raster output). You can also set module_width and module_height directly for precise control over individual module dimensions.
from aspose_barcode_foss import RenderOptions
options = RenderOptions(
scale=3.0,
dpi=300,
module_width=1.5,
module_height=25.0,
)
png = bc.to_png(options=options)Setting Colors
Set foreground_color and background_color as CSS color strings. Use transparent_background=True for PNG output with no background fill.
from aspose_barcode_foss import RenderOptions
options = RenderOptions(
foreground_color="#003366",
background_color="#FFFFFF",
)
svg = bc.to_svg(options=options)
# Transparent background for PNG
options_transparent = RenderOptions(
foreground_color="#000000",
transparent_background=True,
)
png = bc.to_png(options=options_transparent)Quiet Zone
The quiet_zone property controls the whitespace margin around the barcode in module units. Barcode standards typically require a minimum quiet zone for reliable scanning.
from aspose_barcode_foss import RenderOptions
options = RenderOptions(quiet_zone=10.0)
svg = bc.to_svg(options=options)Human-Readable Text
Set show_text=True to render human-readable text below the barcode. Control the font with font_family and font_size.
from aspose_barcode_foss import RenderOptions
options = RenderOptions(
show_text=True,
font_family="monospace",
font_size=12.0,
)
svg = bc.to_svg(options=options)RenderOptions Properties
| Property | Type | Description |
|---|---|---|
scale | float | Multiplier for module size |
dpi | int | Dots per inch (raster output) |
module_width | float | Width of one module |
module_height | float | Height of one module |
quiet_zone | float | Whitespace margin in module units |
foreground_color | str | CSS color for bars/modules |
background_color | str | CSS color for background |
transparent_background | bool | Transparent PNG background |
show_text | bool | Show human-readable text |
font_family | str | Font for human-readable text |
font_size | float | Font size for human-readable text |
Known Limitations
The to_pdf() method and PdfRenderer class exist in the API but raise NotImplementedError. Use to_svg() or to_png() instead. SVG output can be embedded in PDF documents using external tools.