SwissQRBill

repository·main·Indexed 42 days ago

https://github.com/schoero/swissqrbill

A library for generating Swiss QR Code payment slips as PDF files or SVG graphics in Node.js and browser environments. Version 4.4.1 supports multiple languages (German, English, Italian, French, and Romansh) and provides utilities for IBAN and reference validation, as well as PDF table generation via PDFKit.

Tokens
25.9K
Snippets
60
Records
111
Agent score
48%

What's inside SwissQRBill

  1. Explore the SwissQRBill API surface

    main

    The API is organized into several modules based on the output format and utility requirements:

    • PDF Generation: Use SwissQRBill and SwissQRCode classes to generate PDF documents, including Table components.
    • SVG Generation: Use SwissQRBill and SwissQRCode classes to generate vector-based SVG files.
    • Utilities: Access helper functions in the utils module.
    • Error Handling: Reference the errors module for specific error types.
    • Bundles: Information regarding library bundles is available in the bundle module.
  2. Use the `types` namespace for data structures

    main
    The types namespace defines the core data interfaces and type aliases used throughout the library. This includes the primary Data interface (which contains Debtor and Creditor information), Currency, Language, and configuration options like PDFOptions and SVGOptions.
  3. Use the `pdf` namespace for PDF generation

    main
    The pdf namespace provides classes for generating and manipulating Swiss QR Bill components within PDF documents. It includes specialized classes for the full bill, individual QR codes, and tables, along with supporting types for styling and layout.
  4. Use the `svg` namespace for SVG generation

    main
    The svg namespace provides classes for generating Swiss QR Bill components as SVG elements. This is useful for web environments or vector-based graphics where you need to manipulate the SVG DOM or export the component as a string.
  5. Use the `utils` namespace for validation and formatting

    main

    The utils namespace contains helper functions for common Swiss QR Bill tasks, such as:

    • Validation: Checking IBANs (isIBANValid), QR References (isQRReferenceValid), and SCOR References (isSCORReferenceValid).
    • Formatting: Formatting IBANs, QR/SCOR references, and amounts (formatAmount).
    • Unit Conversion: Converting between millimeters, points, and pixels (mm2pt, pt2mm, mm2px, px2mm).
  6. Import SwissQRBill in Node.js using CommonJS

    main

    For legacy Node.js applications using CommonJS, use the require syntax to import the library and its dependencies.

    // PDFKit
    const PDFDocument = require("pdfkit");
    // PDF
    const { SwissQRBill } = require("swissqrbill/pdf");
    // SVG
    const { SwissQRBill } = require("swissqrbill/svg");
  7. Import SwissQRBill via CDN in the Browser (v4+)

    main

    If using the library in a browser via CDN, you can no longer use the single SwissQRBill bundle. You must include pdfkit (standalone), blob-stream, and the swissqrbill bundle separately.

    <!-- SwissQRBill >= v4 -->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/pdfkit@0/js/pdfkit.standalone.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/blob-stream@latest/+esm"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/swissqrbill@latest/lib/bundle/swissqrbill.js"></script
  8. Import SwissQRBill in Node.js using ES Modules

    main

    To use SwissQRBill in a modern Node.js environment with ES Modules (ESM), import the specific sub-modules for your desired output format (PDF or SVG). Note that you must also install and import pdfkit if you are generating PDFs.

    // PDFKit
    import PDFDocument from "pdfkit";
    // PDF
    import { SwissQRBill } from "swissqrbill/pdf";
    // SVG
    import { SwissQRBill } from "swissqrbill/svg";
  9. Create a complete invoice with PDFKit

    main
    SwissQRBill allows you to create complete invoices by adding your own content above the QR Bill section. Since the library is built on PDFKit, you can use any PDFKit features to design the top part of your document before calling qrBill.attachTo(pdf). This enables you to include logos, text, and other business information alongside the mandatory payment section.