Absolutely! To generate a pure vector SVG file with an M×N matrix of QR codes sized to A4 paper (office size), each QR code representing numbers from 1 to M×N, we need to:
- Generate QR codes as vector data (not images).
- Represent the QR code modules (black squares) as SVG
<rect> elements.
- Lay out these QR codes in a grid on an SVG canvas sized as A4.
Approach and details:
- Use the qrcode library to generate QR code matrices (boolean module grids).
- For each QR, write SVG
<rect> elements for black modules only.
- The QR code size (number of modules) varies depending on version; for simplicity, fix version=1 or
fit=True will suffice (or pad each QR code to the same size).
- Position QR codes evenly spaced on A4 (297mm x 210mm), converting to pixels or SVG user units.
- Set DPI=96 or use mm units directly in SVG for better scaling.
- Use mm units in SVG (recommended for print/office).
Step-by-step:
- A4: 210mm width x 297mm height.
- Choose margins (eg. 10mm).
- Calculate usable width/height.
- Each QR code size = usable_width/N and usable_height/M (square).
- Each module size = QR_code_size_in_mm / modules_per_side.
Code implementation:
import qrcode
from qrcode.constants import ERROR_CORRECT_M
def qr_matrix(data, version=1, border=4):
"""
Generate QR code matrix (2D bool list) for the given data.
We fix version=1 to keep module size consistent for layout.
"""
qr = qrcode.QRCode(
version=version,
error_correction=ERROR_CORRECT_M,
box_size=1,
border=border
)
qr.add_data(str(data))
qr.make(fit=True)
# qr.modules is a 2D list of booleans (True=black module)
return qr.modules
def qr_module_count(modules, border):
"""Number of modules per side including border"""
return len(modules)
def generate_svg_a4_vector_qr_matrix(M, N, filename="qr_matrix_a4_vector.svg"):
# A4 in mm (width x height)
A4_WIDTH_MM = 210
A4_HEIGHT_MM = 297
# Margins in mm
margin_mm = 10
usable_width = A4_WIDTH_MM - 2 * margin_mm
usable_height = A4_HEIGHT_MM - 2 * margin_mm
# For all QR codes, use the same version to keep sizes consistent:
qr_version = 1
border_modules = 4
# Generate one QR code to get module count
sample_qr = qr_matrix(1, version=qr_version, border=border_modules)
modules_per_side = len(sample_qr) # e.g. 21 for version=1
# Calculate QR code size in mm (square)
qr_size_mm_w = usable_width / N
qr_size_mm_h = usable_height / M
qr_size_mm = min(qr_size_mm_w, qr_size_mm_h)
# size of one module in mm
module_mm = qr_size_mm / modules_per_side
# Start SVG document
svg_lines = [
'<?xml version="1.0" standalone="no"?>',
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ',
' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">',
f'<svg width="{A4_WIDTH_MM}mm" height="{A4_HEIGHT_MM}mm" ',
' version="1.1" xmlns="http://www.w3.org/2000/svg">',
f'<rect width="100%" height="100%" fill="white"/>'
]
# Start x and y positions for the whole matrix - centered inside margins
total_qr_width = qr_size_mm * N
total_qr_height = qr_size_mm * M
offset_x = margin_mm + (usable_width - total_qr_width) / 2
offset_y = margin_mm + (usable_height - total_qr_height) / 2
# Draw each QR code
for row in range(M):
for col in range(N):
number = row * N + col + 1
matrix = qr_matrix(number, version=qr_version, border=border_modules)
base_x = offset_x + col * qr_size_mm
base_y = offset_y + row * qr_size_mm
# For each black module, draw a black rect
for r, row_modules in enumerate(matrix):
for c, module in enumerate(row_modules):
if module:
x = base_x + c * module_mm
y = base_y + r * module_mm
svg_lines.append(f'<rect x="{x:.3f}mm" y="{y:.3f}mm" '
f'width="{module_mm:.3f}mm" height="{module_mm:.3f}mm" '
f'fill="black" />')
svg_lines.append('</svg>')
svg_content = "\n".join(svg_lines)
with open(filename, "w") as f:
f.write(svg_content)
print(f"Vector SVG with QR matrix saved as {filename}")
if __name__ == "__main__":
M = 10 # rows
N = 5 # columns
generate_svg_a4_vector_qr_matrix(M, N)
Explanation:
- We generate QR code matrices for each number.
- Each QR code is drawn as black
<rect> elements on a white background.
- The SVG uses mm units, ideal for printing.
- The QR matrix starts at margin + centering offset to position nicely on A4.
- The result is a scalable vector SVG file ready for print.
Result:
- Run this script and open the generated
qr_matrix_a4_vector.svg in any modern browser or vector editor (e.g., Inkscape).
- The QR codes will scan correctly and be sized for A4 paper.
Feel free to ask if you want to add labels, color, or multiple pages!