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,
))
PropertyTypeDescription
scalefloatUniform scale multiplier for the entire barcode
dpiintResolution for PNG output (has no effect on SVG)
module_widthfloatWidth of one barcode module in user units
module_heightfloatHeight of one barcode module in user units
quiet_zonefloatWhitespace padding around the barcode
foreground_colorstrHex color for bars and modules (e.g., "#000000")
background_colorstrHex color for background
transparent_backgroundboolTransparent background for PNG output
show_textboolShow human-readable text below the barcode
font_familystrFont for human-readable text
font_sizefloatFont 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:

PropertyTypeDescription
datastr | bytesThe rendered output (SVG string or PNG bytes)
media_typestrMIME type ("image/svg+xml" or "image/png")
backendstrRenderer 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 scale for uniform sizing and module_width/module_height for precise dimension control — do not combine both.
  • Set dpi=300 for print-quality PNG output; the default is screen resolution.
  • Use transparent_background=True for PNG barcodes that will be overlaid on colored backgrounds.
  • foreground_color and background_color accept standard hex color strings ("#RRGGBB").
  • SVG output is resolution-independent — prefer SVG for web and PDF embedding.

Common Issues

IssueCauseFix
PNG appears too smallDefault scale is 1.0Increase scale or set dpi
Text not visibleshow_text defaults to FalseSet show_text=True
QR code has no textQR symbology does not support human-readable textExpected behavior — QR uses no-op text layout
Colors not appliedProperty name misspelledUse exact property names: foreground_color, background_color

See Also

 English