Barcode Rendering
Barcode Rendering
Aspose.BarCode FOSS for Python separates encoding from rendering. After a barcode is encoded into an EncodedSymbol, a Renderer implementation converts the module matrix into visual output. The library includes SvgRenderer and PngRenderer.
RenderOptions
RenderOptions controls the visual output of all renderers. Pass it to to_svg() or to_png() on any Barcode object:
import aspose_barcode_foss as barcode
from aspose_barcode_foss import RenderOptions
bc = barcode.code128("Hello World")
svg = bc.to_svg(options=RenderOptions(
scale=2.0,
foreground_color="#003366",
quiet_zone=10.0,
show_text=True,
))| Property | Type | Description |
|---|---|---|
scale | float | Uniform scale multiplier for the entire barcode |
dpi | int | Resolution for PNG output (has no effect on SVG) |
module_width | float | Width of one barcode module in user units |
module_height | float | Height of one barcode module in user units |
quiet_zone | float | Whitespace padding around the barcode |
foreground_color | str | Hex color for bars and modules (e.g., "#000000") |
background_color | str | Hex color for background |
transparent_background | bool | Transparent background for PNG output |
show_text | bool | Show human-readable text below the barcode |
font_family | str | Font for human-readable text |
font_size | float | Font size for human-readable text |
ResolvedRenderOptions
ResolvedRenderOptions is the fully merged configuration produced by OptionsResolver.resolve(). It combines user-supplied RenderOptions with the SymbologyProfile defaults. All properties have concrete values (no None).
The Barcode.default_render_options property holds the RenderOptions passed at generation time (or None if not supplied).
SVG Output
SvgRenderer produces SVG output. Call to_svg() on a Barcode object:
bc = barcode.ean13("590123412345")
svg_string = bc.to_svg()
# With custom options
svg_custom = bc.to_svg(options=RenderOptions(
scale=3.0,
foreground_color="#1a1a1a",
show_text=True,
font_family="Helvetica",
font_size=12.0,
))
with open("ean13.svg", "w") as f:
f.write(svg_custom)to_svg() returns a str containing the complete SVG document.
PNG Output
PngRenderer produces PNG output. Call to_png() on a Barcode object:
bc = barcode.qr("https://example.com")
png_bytes = bc.to_png()
# With custom options
png_custom = bc.to_png(options=RenderOptions(
scale=4.0,
dpi=300,
transparent_background=True,
))
with open("qr.png", "wb") as f:
f.write(png_custom)to_png() returns bytes containing the PNG image data.
Renderer Abstraction
The Renderer abstract base class defines the interface for all renderers:
class Renderer(ABC):
def render(self, symbol, layout, options) -> RenderedArtifact: ...SvgRenderer and PngRenderer both implement this interface. You can call Barcode.render(renderer, options) to use a specific renderer directly:
from aspose_barcode_foss import SvgRenderer, RenderOptions
bc = barcode.code128("DATA")
artifact = bc.render(SvgRenderer(), RenderOptions(scale=2.0))
print(artifact.media_type) # "image/svg+xml"
print(type(artifact.data)) # <class 'str'>RenderedArtifact
The RenderedArtifact returned by Renderer.render() contains:
| Property | Type | Description |
|---|---|---|
data | str | bytes | The rendered output (SVG string or PNG bytes) |
media_type | str | MIME type ("image/svg+xml" or "image/png") |
backend | str | Renderer identifier |
Text Layout
Each symbology has a TextLayoutPolicy that determines how human-readable text is positioned below the barcode. The create_layout(symbol, options) method returns a TextLayout containing TextSegment objects with position and text data.
QR Code uses QrTextLayoutPolicy, which provides a no-op layout (QR codes do not display human-readable text).
Tips and Best Practices
- Use
scalefor uniform sizing andmodule_width/module_heightfor precise dimension control — do not combine both. - Set
dpi=300for print-quality PNG output; the default is screen resolution. - Use
transparent_background=Truefor PNG barcodes that will be overlaid on colored backgrounds. foreground_colorandbackground_coloraccept standard hex color strings ("#RRGGBB").- SVG output is resolution-independent — prefer SVG for web and PDF embedding.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| PNG appears too small | Default scale is 1.0 | Increase scale or set dpi |
| Text not visible | show_text defaults to False | Set show_text=True |
| QR code has no text | QR symbology does not support human-readable text | Expected behavior — QR uses no-op text layout |
| Colors not applied | Property name misspelled | Use exact property names: foreground_color, background_color |