schoero / swissqrbill

Swiss QR Bill generation in Node.js and browsers
MIT License
155 stars 29 forks source link

Generate QR Code only [as SVG] #391

Closed simonmumenthaler closed 8 months ago

simonmumenthaler commented 1 year ago

I'm looking for a way to generate the QR Code only as SVG - the method used in the SVG Class is the generateQRCode Unfortunately this function is not exported (also no deep import possible as the package.json does not list this file).

Therefore: could you export this function?

Fabiansson commented 1 year ago

This would be very helpful indeed! Any updates on this?

schoero commented 1 year ago

Unfortunately not. I have a few days off at the end of the month in which I would like to overhaul this module and implement this.

Coman907014 commented 1 year ago

Hey, Roger. Instead of adding the entire "render only QR Code" functionality, would it be easier just to add a static ID to the SVG inside the _renderQRCode method? @simonmumenthaler, I used height & width targeting to identify the QR Code, as it is statically defined inside the library.

const svg = new SwissQRBill.SVG(data.qrCodeData, data.options); const findQRCode = useCallback((element) => { if (element && element.hasChildNodes()) { return Array.from(element.children).find( (el) => el.width?.baseVal?.valueAsString === "46mm" && el.height?.baseVal?.valueAsString === "46mm" as Node; } return

; }, []); findQRCode(svg.element)`

Hope it helps.

All the best! Alex

schoero commented 8 months ago

SwissQRBill v4 now offers a separate class SwissQRCode for PDF and SVG to make this possible. This will create a "SwissQRCode" including the swiss cross in the center.

Example:

import { writeFileSync } from "node:fs";

import { SwissQRCode } from "swissqrbill/svg";

const data = {
  amount: 1994.75,
  creditor: {
    account: "CH44 3199 9123 0008 8901 2",
    address: "Musterstrasse",
    buildingNumber: 7,
    city: "Musterstadt",
    country: "CH",
    name: "SwissQRBill",
    zip: 1234
  },
  currency: "CHF",
  debtor: {
    address: "Musterstrasse",
    buildingNumber: 1,
    city: "Musterstadt",
    country: "CH",
    name: "Peter Muster",
    zip: 1234
  },
  reference: "21 00000 00003 13947 14300 09017"
};

const svg = new SwissQRCode(data);

writeFileSync("qr-code.svg", svg.toString());