Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

swissqrbill

schoero12.7kMIT4.2.0TypeScript support: included

Swiss QR Bill generation in Node.js and browsers

swiss, schweiz, switzerland, qr, bill, invoice, rechnung, swiss-qr-invoice, swiss-qr-bill, qr-invoice, qr-rechnung, qr-fattura, qr-facture, fattura, facture, pdf, svg

readme

SwissQRBill

GitHub license npm version GitHub issues npm weekly downloads GitHub repo stars GitHub workflow status




With SwissQRBill you can easily generate the new QR Code payment slips as a PDF or SVG in Node.js and the browser. The new QR Code payment slips were introduced in Switzerland on June 30th, 2020 and replaces the old payment slips since October 1st, 2022. In addition to the payment section, you can generate a complete invoice with PDFKit by inserting your own content above the payment section.



QR bill



<picture> <source media="(prefers-color-scheme: dark)" srcset="./assets/sponsor-dark.svg"> <source media="(prefers-color-scheme: light)" srcset="./assets/sponsor-light.svg"> eslint-plugin-readable-tailwind logo </picture>

Invoices are a critical part of every business. Keeping this library up-to-date with the extensive specifications takes time and effort.
If this library has been valuable to you, please consider becoming a sponsor, or make a one time donation to support its ongoing development.




Features

  • Generate complete invoices, or only the QR Bill, as a PDF file.
  • Generate the QR Bill as a scalable vector graphic (SVG).
  • Works in browsers and Node.js.
  • Supports german, english, italian and french invoices.
  • Allows you to add other content above the invoice using PDFKit.
  • Easy to use.
  • Free and open source.

Installation

npm i swissqrbill

Importing the library

SwissQRBill is based around PDFKit. Depending on the environment you are using, you may need to import the libraries differently. Please read the importing documentation to find out the best way to import the libraries for your environment.


Quick start

Once you have imported SwissQRBill and PDFKit, it is quite easy to create a simple QR bill. All you have to do is to create a new SwissQRBill instance with your billing data object. You can then attach the QR bill to any PDFKit document.

import { createWriteStream } from "node:fs";

import PDFDocument from "pdfkit";
import { SwissQRBill } from "swissqrbill/pdf";

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 pdf = new PDFDocument({ size: "A4" });
const qrBill = new SwissQRBill(data);

const stream = createWriteStream("qr-bill.pdf");

qrBill.attachTo(pdf);
pdf.pipe(stream);
pdf.end();

This will create the PDF file above. You can pass an optional parameter containing options to the SwissQRBill constructor. A complete documentation for all methods and parameters can be found in the docs/ directory of this repository.



Alternatively, you could render the QR Bill as a scalable vector graphic (SVG). But keep in mind, using SVG you can only render the QR Bill part and not an entire invoice.

import { SwissQRBill } from "swissqrbill/svg";

const svg = new SwissQRBill(data);

document.body.appendChild(svg.element);


Creating a complete invoice

It is possible to create a complete invoice with SwissQRBill. This means that you can add your own content above the QR Bill.

SwissQRBill is based around PDFKit to generate the PDF files. You can use all the features of PDFKit to add your own content to the PDF file.

The documentation for PDFKit can be found on pdfkit.org.

A simple guide how to generate a complete bill can be found in docs/how-to-create-a-complete-qr-bill.md. You will learn how to create a PDF that looks like this:


Complete QR bill

changelog

Change Log

v4.2.0

compare changes

Features

  • Always output structured addresses to comply with spec updates (#454)

v4.1.2

compare changes

Refactors

  • Inline calculations (#452)

v4.1.1

compare changes

Fixes

  • Imports for TypeScript environments with moduleResolution set to node10 or node16. (#448)

v4.1.0

compare changes

Features

  • Error codes (#442)

v4.0.2

compare changes

Fixes

  • Type definitions for common js imports (cbe419a)

Refactors

  • Use imports with js file extensions (1c77d52)

Documentation

  • Regenerate documentation (9dd2281)

Build

Chore

v4.0.1

compare changes

Fixes

  • Visual pdf tests throwing error (7936151)
  • table: Throwing error when header row was enabled on a table with multiple columns (b096a51)
  • table: Setting padding to 0 not working in a tuple type (14ad3c3)
  • table: Continue table on the same horizontal position (e8e5aec)
  • table: Rendering of imaginary layers (9eb4f3d)
  • table: Converting of tuple shorthand syntax (99b1c95)

Documentation

Chore

Tests

  • Disable multi threading in debugger (85911a1)

v4.0.0 - 10.11.2023

With version 4, SwissQRBill has been completely reworked. The main goal was to make the API more flexible and reduce the complexity of the library. As a consequence, the API has changed quite significantly. However, migration should be fairly easy.

Please read the migration guide for more information and have a look at the updated documentation.

  • New features

    • SwissQRBill can now be attached to any PDFDocument instance from PDFKit.
    • New SwissQRCode class to create the standalone Swiss QR Code to attach to an existing PDFKit document.
    • New Table class to create a table to attach to an existing PDFKit document.
    • It is now possible to change the font of the QR Bill. The allowed fonts are "Arial" | "Frutiger" | "Helvetica" | "Liberation Sans"
    • Additional options for SVG renderings.
  • Breaking changes

    • The entrypoints have changed. Please read the importing documentation for more information.
    • SwissQRBill no longer extends PDFKit.
    • The table method has been extracted into it's own class.
    • Events have been removed from SwissQRBill.
    • Properties and options have been renamed.
    • Now requires Node.js >= 18.0.0
  • Fixes

    • Some slight rendering improvements.
  • Other improvements

    • Better examples. The examples are now automatically hosted on StackBlitz. Everyone can now easily try out the library in the browser without having to install anything.
    • Automated documentation. SwissQRBill now uses unwritten to automatically generate the documentation. This makes it easier to keep the documentation up to date.
    • Automated testing for even more stability.

Thanks to @danielpanero and @skch-17 for their contributions.

v3.2.3 - 16.04.2023

  • Updated dependencies.
  • Removed source maps from the pre-built bundle.

v3.2.2 - 14.01.2023

  • Updated dependencies.
  • Fixed CI status badge.

v3.2.1 - 31.08.2022

  • Performance improvements for in QR-Code rendering for PDF renderings. #384
  • Enforce minimum and maximum allowed QR-Code versions according to the specifications.

v3.2.0 - 05.03.2022

  • Changed PDF_._data property to be protected to allow extending the PDF_ class. #368

v3.1.5 - 21.02.2022

  • Fixed umlauts not correctly decoded with PostFinance scanner. #367
  • QR-Bills rendered as SVG now have a white background color.

v3.1.4 - 11.01.2022

  • Fixed rendering of debtor rectangle in when page size is A4. #361
  • Fixed rendering of country code for foreign payments. #362
  • Fixed missing of - character in svg text rendering. #363

v3.1.3 - 05.12.2021

  • Fixed rendering of AV1 and AV2. #357

v3.1.2 - 26.11.2021

  • Removed deprecated restrictions for AV1 and AV2. #355

v3.1.1 - 13.11.2021

  • Fixed swiss cross positioning for PDF rendering. #353

v3.1.0 - 12.11.2021

  • Added TSDoc comments. #348
  • Removed unwanted space in utils.formatAmount(). #349
  • Fixed the formatting of the reference in SVG renderings. #351
  • Added a new utils.formatReference() function that auto detects the reference type.
  • Improved the text spacing and positioning. #352

v3.0.1 - 14.10.2021

  • Changed build target to ES6 to support older Browsers and Node.js versions. #347

v3.0.0 - 03.10.2021

  • New features
    • SVG
      • Added support for SVG rendering. #343
    • Added ES6 module exports.
    • Support for tree shaking.
    • utils
      • Added mm2px() function.
      • Added px2mm() function.
      • Added pt2mm() function.
  • Breaking changes
    • imports
      • SwissQRBill is now available as a CommonJS and an ES module. This may change how the module has to be imported. Please take a look at the importing the library section in the readme.
    • data
      • the field houseNumber has been renamed to buildingNumber.
      • the deprecated field debitor has been removed. Use debtor instead.
    • utils
      • mmToPoints() function has been renamed to mm2pt().
    • PDF
      • Removed deprecated mmToPoints() export. Use utils.mm2pt() instead.
  • Fixes
    • PDF
      • The positioning of the box when no amount is provided has been slightly corrected.
      • The positioning iban on the payment part has been slightly corrected.
      • Fixed positioning of the debtor boxes when no debtor is provided and the creditor address break to multiple lines.

v2.4.2 - 26.08.2021

  • Fixed translation of additionalInformation and payableByName. #342

v2.4.1 - 25.08.2021

  • Fixed missing rendering of message. #340

v2.4.0 - 01.07.2021

  • Added an optional size parameter to specify the new page size in addQRBill(). #338

v2.3.3 - 05.04.2021

  • A string is now allowed for the zip code field. #294

v2.3.1 - 06.02.2021

  • Removed iban dependency and integrated IBAN validation directly into the library.
  • Fixed some misleading error messages.

v2.3.0 - 24.01.2021

  • Added options to disable rendering of outlines, scissors icons or text Separate before paying in, for use with perforated or preprinted paper.

v2.2.0 - 20.01.2021

  • Switched from fs.WriteStream to stream.Writable in order to provide more streaming flexibility. #207

v2.1.0 - 15.12.2020

  • Changed the implemented QR code library to fix an issue that caused QR code scanning to fail on some QR readers.
  • Added utility functions to simplify the creation of QR bills.
  • Added additional QR-Reference validation.

v2.0.3 - 09.12.2020

  • Fixed a problem with QR Code encoding that caused QR Code scanning to fail at certain banks.

v2.0.2 - 19.08.2020

  • Fixed an issue that caused reference to render incorrectly.
  • Fixed "Compte / Payable à" to display correctly in french QR bills.

v2.0.1 - 22.07.2020

  • Small bug fixes and code cleanup.

v2.0.0 - 12.07.2020

  • Added browser support.
    • Added new blobStream method.
  • Added possibility to stream the pdf into a buffer.

v1.3.1 - 27.06.2020

  • Fixed invalid QR Code field when the second decimal place in amount is a zero.

v1.3.0 - 25.06.2020

  • Tables
    • Added new header property on table rows to automatically insert a header row on new pages.
    • Table row height is now automatically calculated if not otherwise defined.
    • Added padding property to rows and cells.
  • Events
    • Added new beforeEnd event that could be used to add page numbers.
    • Added new pageAdded event that could be used to add a header to the pages.
  • Renamed debitor property to debtor.
  • Improved documentation.
  • TypeScript declaration improvements.
  • Fixed typos in examples.

v1.2.0 - 06.06.2020

  • Added optional callback function that gets executed once the pdf file has completed writing.
  • Emit finish event once the pdf file has completed writing.

v1.1.0 - 15.03.2020

  • Fixed some validation checks.
  • Improved error messages.
  • Added new method to generate tables.
  • Improved documentation.

v1.0.6 - 04.03.2020

  • Fixed QR Code fields.
  • Added user data validation.

v1.0.5 - 03.03.2020

  • Fixed french characters.
  • Removes new lines in user data.
  • Fixed some layout issues.