class SignatureImage_v1
A custom ReportLab Flowable class that renders signature images in PDF documents with automatic fallback to placeholder text when images are unavailable or cannot be loaded.
/tf/active/vicechatdev/document_auditor/src/audit_page_generator.py
26 - 52
simple
Purpose
SignatureImage extends ReportLab's Flowable class to provide a specialized component for embedding signature images in PDF documents. It handles image loading, rendering, and gracefully degrades to placeholder rectangles with descriptive text when the signature image file doesn't exist or cannot be loaded. This is particularly useful for generating documents that require signature fields, such as contracts, forms, or audit reports.
Source Code
class SignatureImage(Flowable):
"""Custom flowable for signature images"""
def __init__(self, img_path, width=2*inch, height=0.75*inch):
Flowable.__init__(self)
self.img_path = img_path
self.width = width
self.height = height
def draw(self):
if os.path.exists(self.img_path):
try:
img = Image(self.img_path, self.width, self.height)
img.drawOn(self.canv, 0, 0)
except Exception as e:
# Draw a placeholder if image can't be loaded
self.canv.setStrokeColor(colors.grey)
self.canv.rect(0, 0, self.width, self.height)
self.canv.setFont("Helvetica", 8)
self.canv.drawString(5, self.height/2, "Signature Unavailable")
else:
# Draw a placeholder if file doesn't exist
self.canv.setStrokeColor(colors.grey)
self.canv.rect(0, 0, self.width, self.height)
self.canv.setFont("Helvetica", 8)
self.canv.drawString(5, self.height/2, "No Signature on File")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Flowable | - |
Parameter Details
img_path: String path to the signature image file. Can be absolute or relative path. The class checks if this file exists before attempting to load it.
width: Width of the signature image area in ReportLab units. Defaults to 2 inches (2*inch). This determines the rendered width regardless of the actual image dimensions.
height: Height of the signature image area in ReportLab units. Defaults to 0.75 inches (0.75*inch). This determines the rendered height regardless of the actual image dimensions.
Return Value
Instantiation returns a SignatureImage object that can be added to ReportLab document templates. The draw() method does not return a value but renders the signature image or placeholder directly to the canvas.
Class Interface
Methods
__init__(self, img_path, width=2*inch, height=0.75*inch)
Purpose: Initializes the SignatureImage flowable with image path and dimensions
Parameters:
img_path: String path to the signature image filewidth: Width of the signature area in ReportLab units (default: 2 inches)height: Height of the signature area in ReportLab units (default: 0.75 inches)
Returns: None (constructor)
draw(self)
Purpose: Renders the signature image or placeholder to the canvas. Called automatically by ReportLab during document generation.
Returns: None - draws directly to self.canv (canvas object provided by Flowable parent class)
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
img_path |
str | Path to the signature image file to be rendered | instance |
width |
float | Width of the signature image area in ReportLab units | instance |
height |
float | Height of the signature image area in ReportLab units | instance |
canv |
reportlab.pdfgen.canvas.Canvas | Canvas object inherited from Flowable parent class, used for drawing operations | instance |
Dependencies
reportlabos
Required Imports
import os
from reportlab.platypus import Flowable
from reportlab.platypus import Image
from reportlab.lib import colors
from reportlab.lib.units import inch
Usage Example
from reportlab.platypus import SimpleDocTemplate, Flowable, Image
from reportlab.lib.units import inch
from reportlab.lib import colors
import os
# Create a signature image flowable
sig_image = SignatureImage(
img_path='/path/to/signature.png',
width=2*inch,
height=0.75*inch
)
# Add to a PDF document
doc = SimpleDocTemplate('document.pdf')
story = []
story.append(sig_image)
doc.build(story)
# Example with non-existent file (will show placeholder)
sig_placeholder = SignatureImage(
img_path='/path/to/nonexistent.png',
width=1.5*inch,
height=0.5*inch
)
story.append(sig_placeholder)
Best Practices
- Always verify the signature image path exists before creating the document if you want to avoid placeholders
- Use consistent width and height dimensions across signature images in the same document for visual consistency
- The class automatically handles missing or corrupted images by displaying placeholders, so no additional error handling is needed
- The draw() method is called automatically by ReportLab's document building process - do not call it manually
- Ensure image files are in formats supported by ReportLab (PNG, JPG recommended)
- Consider the aspect ratio when setting width and height to avoid distorted signatures
- The class inherits from Flowable, so it can be used anywhere a Flowable is expected in ReportLab documents
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class SignatureImage 95.5% similar
-
class SignatureGenerator 60.9% similar
-
class SignatureManager 55.9% similar
-
function main_v30 51.5% similar
-
function add_formatted_content_to_pdf 49.9% similar