Install the module, create a generator and render your first QR code as SVG, PNG or EPS.
Add the module:
go get github.com/bowerbird-si/laravel-go-qrcode@v0.1.0
Create a generator, then render text in the format you need:
g, err := qrcode.New(qrcode.Size(200), qrcode.Margin(4))
if err != nil {
return err
}
svg, err := g.SVG("https://example.com") // []byte, image/svg+xml
png, err := g.PNG("https://example.com") // []byte, image/png
eps, err := g.EPS("https://example.com") // []byte, application/postscript
Generate(text, format) does the same with a qrcode.Format value (SVGFormat, PNGFormat,
EPSFormat), and format.ContentType() gives its MIME type.
Save to a file
f, err := os.Create("ticket.svg")
if err != nil {
return err
}
defer f.Close()
err = g.WriteTo(f, "https://example.com", qrcode.SVGFormat)
WriteTo accepts any io.Writer, such as an HTTP response or a buffer. For a small file,
os.WriteFile("ticket.png", png, 0o644) works too.
Embed in HTML
DataURI returns a base64 data: URI for SVG or PNG, for use in an image source attribute.
src, err := g.DataURI("https://example.com", qrcode.PNGFormat)
// data:image/png;base64,iVBOR…Reuse and override
A Generator is a value. With returns a copy with more options applied, and leaves the original
untouched, so you can share one configured generator:
base, _ := qrcode.New(qrcode.Size(200))
large, err := base.With(qrcode.Size(600))
The zero value qrcode.Generator{} uses the defaults: 100 pixels, no margin, error correction level L,
ISO-8859-1, black on white, square modules. These match PHP.
Errors
| Error | When |
|---|---|
qrcode.ErrInvalidOption | An option value is out of range, or options don't fit the format (a logo with SVG, transparent colors with EPS) |
qrcode.ErrInvalidPayload | The text is empty, or a payload helper got an invalid field |
qrcode.ErrUnencodable | The text has characters the chosen encoding can't represent |
qrcode.ErrDataTooLong | The text doesn't fit in a QR code at the chosen error correction level |
Check them with errors.Is.
See Options and styles to configure the generator.