goqrsvg

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 4 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

goqrsvg

goqrsvg is a Go library that converts QR Codes to SVG. It renders the QR Code as one <rect> per module so the result scales cleanly to any size.

QR codes are generated with boombuler/barcode and written with ajstarks/svgo.

Install

go get github.com/pschlump/goqrsvg

Import:

import (
	svg "github.com/ajstarks/svgo"
	"github.com/boombuler/barcode/qr"
	"github.com/pschlump/goqrsvg"
)

Library usage

package main

import (
	"os"

	svg "github.com/ajstarks/svgo"
	"github.com/boombuler/barcode/qr"
	"github.com/pschlump/goqrsvg"
)

func main() {
	// Create the QR Code
	qrCode, err := qr.Encode("Hello World", qr.M, qr.Auto)
	if err != nil {
		panic(err)
	}

	// Write QR code to SVG
	s := svg.New(os.Stdout)
	qs := goqrsvg.NewQrSVG(qrCode, 5) // 5 is the module ("pixel") size
	qs.StartQrSVG(s)                  // writes the <svg> start tag with a quiet zone
	if err := qs.WriteQrSVG(s); err != nil {
		panic(err)
	}
	s.End()
}

WriteQrSVGInverse renders the same QR Code with the coloring inverted — white modules on a black background:

	if err := qs.WriteQrSVGInverse(s); err != nil {
		panic(err)
	}
API
Function Description
NewQrSVG(qr barcode.Barcode, blockSize int) QrSVG Build a QrSVG from a rendered QR Code; blockSize is the size of one module in the SVG.
StartQrSVG(s *svg.SVG) Write the SVG start tag sized for the QR Code plus a 4-module quiet zone. Only use this when the SVG contains just the QR Code; otherwise call svg.Start() yourself and use SetStartPoint.
SetStartPoint(x, y int) Position the QR Code inside a larger SVG document.
WriteQrSVG(s *svg.SVG) error Write the QR Code modules — black on white. Returns an error if the barcode is not a QR Code.
WriteQrSVGInverse(s *svg.SVG) error Write the QR Code modules — white on black.

Command line tool

cmd/goqr-cli generates a QR Code SVG from the command line:

cd cmd/goqr-cli
go build
./goqr-cli -url "https://example.com/" >qr.svg
./goqr-cli -url "https://example.com/" -inverse >qr-inverse.svg

Flags: -url (data to encode), -inverse (invert the coloring), -size (module size, default 5).

Sample server

cmd/goqr-svr serves QR Code SVGs over HTTP. It listens on :2003 by default (-addr to change):

cd cmd/goqr-svr
go build
./goqr-svr

Then:

curl 'http://localhost:2003/?url=https://example.com/' >qr.svg
curl 'http://localhost:2003/?url=https://example.com/&inverse=1' >qr-inverse.svg
curl -d 'url=https://example.com/' http://localhost:2003/ >qr.svg

The url parameter comes from the query string on GET or the form body on POST. Any other method returns 405; a missing url returns 400.

Development

make test   # go vet + go test ./...
make lint   # golangci-lint run ./...

Documentation

See GoDoc.

License

MIT — see LICENSE.

Documentation

Overview

Package goqrsvg is an API that makes QR Code to SVG conversions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type QrSVG

type QrSVG struct {
	// contains filtered or unexported fields
}

QrSVG holds the data related to the size, location, and block size of the QR Code. Holds unexported fields.

func NewQrSVG

func NewQrSVG(qr barcode.Barcode, blockSize int) QrSVG

NewQrSVG constructs a QrSVG struct. It takes a QR Code in the form of barcode.Barcode and sets the "pixel" or block size of QR Code in the SVG file.

Example
s := svg.New(os.Stdout)

// Create the barcode
qrCode, err := qr.Encode("Hello World", qr.M, qr.Auto)
if err != nil {
	panic(err)
}

// Write QR code to SVG
qs := NewQrSVG(qrCode, 5)
qs.StartQrSVG(s)
if err := qs.WriteQrSVG(s); err != nil {
	panic(err)
}

s.End()

func (*QrSVG) SetStartPoint

func (qs *QrSVG) SetStartPoint(x, y int)

SetStartPoint sets the top left start point of QR Code. This takes an X and Y value and then adds four white "blocks" to create the "quiet zone" around the QR Code.

func (*QrSVG) StartQrSVG

func (qs *QrSVG) StartQrSVG(s *svg.SVG)

StartQrSVG creates a start for writing an SVG file that only contains a barcode. This is similar to the svg.Start() method. This function should only be used if you only want to write a QR code to the SVG. Otherwise use the regular svg.Start() method to start your SVG file.

func (*QrSVG) WriteQrSVG

func (qs *QrSVG) WriteQrSVG(s *svg.SVG) error

WriteQrSVG writes the QR Code to SVG with black modules on a white background.

func (*QrSVG) WriteQrSVGInverse added in v1.0.1

func (qs *QrSVG) WriteQrSVGInverse(s *svg.SVG) error

WriteQrSVGInverse writes the QR Code to SVG with the coloring inverted: white modules on a black background.

Directories

Path Synopsis
cmd
goqr-cli command
goqr-svr command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址