-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package pt | ||
|
||
import ( | ||
"github.com/invopop/gobl" | ||
"github.com/invopop/gobl/regimes/pt" | ||
) | ||
|
||
templ ATQR(env *gobl.Envelope) { | ||
if atcud := atATCUD(env); atcud != "" { | ||
if qr := atQR(env); qr != "" { | ||
@generateATQR(atcud, qr) | ||
} | ||
} | ||
} | ||
|
||
templ generateATQR(atcud, qr string) { | ||
<style type="text/css"> | ||
.at { | ||
break-inside: avoid; | ||
text-align: center; | ||
padding: 3mm; | ||
border: 1px solid #E5E7EB; | ||
width: 6cm; | ||
} | ||
.at .label { | ||
font-family: monospace; | ||
font-size: 7pt; | ||
text-align: center; | ||
margin-bottom: 2mm; | ||
} | ||
.at img { | ||
width: 30mm; | ||
height: 30mm; | ||
} | ||
</style> | ||
<section> | ||
<div class="at"> | ||
<div class="label"> | ||
ATCUD: { atcud } | ||
</div> | ||
<div class="qr"> | ||
<img src={ generateQR(qr) }/> | ||
</div> | ||
</div> | ||
</section> | ||
} | ||
|
||
func atATCUD(env *gobl.Envelope) string { | ||
for _, stamp := range env.Head.Stamps { | ||
switch stamp.Provider { | ||
case pt.StampProviderATATCUD: | ||
return stamp.Value | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func atQR(env *gobl.Envelope) string { | ||
for _, stamp := range env.Head.Stamps { | ||
switch stamp.Provider { | ||
case pt.StampProviderATQR: | ||
return stamp.Value | ||
} | ||
} | ||
return "" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package pt | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
|
||
go_qr "github.com/piglig/go-qr" | ||
) | ||
|
||
// Parameters required by the AT for the QR code generator | ||
const ( | ||
// Minimum version | ||
atQRMinVer = 9 | ||
|
||
// Error correction level | ||
atQRCorLvl = go_qr.Medium | ||
) | ||
|
||
// generateQR implements a custom QR code generator that complies with the AT spec. | ||
func generateQR(qrval string) string { | ||
// The AT spec requires the QR code to be encoded in binary/bytes mode | ||
seg, err := go_qr.MakeBytes([]byte(qrval)) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
segs := []*go_qr.QrSegment{seg} | ||
|
||
// Encode according to the AT specs | ||
qr, err := go_qr.EncodeSegments(segs, atQRCorLvl, atQRMinVer, go_qr.MaxVersion, -1, false) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
conf := go_qr.NewQrCodeImgConfig(10, 4) | ||
|
||
buf := new(bytes.Buffer) | ||
if err := qr.WriteAsSVG(conf, buf, "#FFFFFF", "#000000"); err != nil { | ||
return "" | ||
} | ||
|
||
str := base64.StdEncoding.EncodeToString(buf.Bytes()) | ||
return "data:image/svg+xml;base64," + str | ||
} |