### Install PDFKit Package Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Installs the PDFKit npm package. This library is used for creating PDF documents. ```sh npm i pdfkit ``` -------------------------------- ### Install SwissQRBill Package Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Installs the SwissQRBill npm package. This is a prerequisite for generating QR bills. ```sh npm i swissqrbill ``` -------------------------------- ### Generate and Attach SwissQRBill to PDF (TypeScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md This example demonstrates how to create a SwissQRBill instance with provided data, attach it to a PDFDocument, and save it to a file. It requires the PDFDocument and SwissQRBill classes, along with a writable stream. ```typescript 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({ autoFirstPage: false }); const qrBill = new SwissQRBill(data); const stream = createWriteStream("qr-bill.pdf"); qrBill.attachTo(pdf); pdf.pipe(stream); pdf.end(); ``` -------------------------------- ### Importing SwissQRBill via Pre-built Bundle Source: https://github.com/schoero/swissqrbill/blob/main/docs/migration-v3-to-v4.md Shows the updated script tag requirements when using pre-built bundles, requiring separate inclusion of PDFKit and SwissQRBill. ```html ``` -------------------------------- ### Install SwissQRBill and PDFKit Source: https://context7.com/schoero/swissqrbill/llms.txt Installs the necessary npm packages for generating Swiss QR Bills and PDF documents. This includes the core swissqrbill library and pdfkit for PDF manipulation. ```bash npm install swissqrbill pdfkit ``` -------------------------------- ### Importing SwissQRBill in Node.js Source: https://github.com/schoero/swissqrbill/blob/main/docs/migration-v3-to-v4.md Demonstrates the shift from a single barrel export to modular imports for SwissQRBill and PDFKit in Node.js environments. ```typescript // SwissQRBill <= v3 import { PDF } from "swissqrbill"; // SwissQRBill >= v4 import PDFDocument from "pdfkit"; import { SwissQRBill, Table } from "swissqrbill/pdf"; ``` -------------------------------- ### Attaching SwissQRBill and Table to PDFDocument Source: https://github.com/schoero/swissqrbill/blob/main/docs/migration-v3-to-v4.md Illustrates the new pattern of instantiating standalone classes and using the attachTo method to integrate them into a PDFKit document. ```typescript const pdf = new PDFDocument(); const qrBill = new SwissQRBill({ /* ... */ }); qrBill.attachTo(pdf); const table = new Table({ /* ... */ }); table.attachTo(pdf); ``` -------------------------------- ### Generate QR Bill with CommonJS Source: https://context7.com/schoero/swissqrbill/llms.txt Shows how to use SwissQRBill in a Node.js environment using CommonJS requires. The example covers both PDF generation using PDFKit and SVG generation for file output. ```javascript const { createWriteStream } = require("node:fs"); const PDFDocument = require("pdfkit"); const { SwissQRBill, Table } = require("swissqrbill/pdf"); const { SwissQRBill: SwissQRBillSVG } = require("swissqrbill/svg"); const { formatIBAN, mm2pt } = require("swissqrbill/utils"); const data = { amount: 500.00, creditor: { account: "CH4431999123000889012", address: "Hauptstrasse", buildingNumber: 10, city: "Bern", country: "CH", name: "Example GmbH", zip: 3000 }, currency: "CHF" }; // PDF generation const pdf = new PDFDocument({ size: "A4" }); const qrBill = new SwissQRBill(data); qrBill.attachTo(pdf); pdf.pipe(createWriteStream("output.pdf")); pdf.end(); // SVG generation const svg = new SwissQRBillSVG(data); require("node:fs").writeFileSync("output.svg", svg.toString()); ``` -------------------------------- ### Registering Custom Fonts for PDFKit in TypeScript Source: https://github.com/schoero/swissqrbill/blob/main/docs/pdf/types.md Demonstrates how to register custom fonts with PDFKit for use in QR bill generation. This is necessary when using font names other than Helvetica. The example shows registering 'Liberation-Sans' and 'Liberation-Sans-Bold' and then instantiating SwissQRBill with a custom font. ```typescript const pdf = require('pdfkit'); // Register the font pdf.registerFont("Liberation-Sans", "path/to/LiberationSans-Regular.ttf"); pdf.registerFont("Liberation-Sans-Bold", "path/to/LiberationSans-Bold.ttf"); // Assuming 'data' is defined elsewhere // const qrBill = new SwissQRBill(data, { fontName: "Liberation-Sans" }); ``` -------------------------------- ### Create and Configure a Table for Invoices Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Demonstrates importing the Table class and defining a table structure with rows, columns, styling, and dimensions. The configuration object supports custom padding, background colors, and font settings for individual cells. ```typescript import { Table } from "swissqrbill/pdf"; const table = new Table({ rows: [ { backgroundColor: "#4A4D51", columns: [ { text: "Position", width: mm2pt(20) }, { text: "Anzahl", width: mm2pt(20) }, { text: "Bezeichnung" }, { text: "Total", width: mm2pt(30) } ], fontName: "Helvetica-Bold", height: 20, padding: 5, textColor: "#fff", verticalAlign: "center" }, { columns: [ { text: "1", width: mm2pt(20) }, { text: "14 Std.", width: mm2pt(20) }, { text: "Programmierung SwissQRBill" }, { text: "CHF 1'540.00", width: mm2pt(30) } ], padding: 5 } ], width: mm2pt(170) }); ``` -------------------------------- ### Generate PDF QR Bill with SwissQRBill Source: https://github.com/schoero/swissqrbill/blob/main/README.md Demonstrates how to initialize a SwissQRBill instance with billing data and attach it to a PDFKit document. This process requires the swissqrbill/pdf module and a configured PDFKit instance. ```javascript 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(); ``` -------------------------------- ### Create SwissQRCode Instance (TypeScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md This snippet illustrates the constructor for the SwissQRCode class. It requires data for the QR code and an optional size in millimeters. It can throw a ValidationError for invalid data. ```typescript const qrCode = new SwissQRCode(data); ``` -------------------------------- ### Get SwissQRBill SVG Element Source: https://github.com/schoero/swissqrbill/blob/main/docs/svg/index.md The element() getter of the SwissQRBill class returns the raw SVG element. This allows for direct manipulation or appending of the SVG to the DOM. ```typescript const svgElement = svg.element(); ``` -------------------------------- ### Create and attach a Table to a PDF document Source: https://github.com/schoero/swissqrbill/blob/main/docs/pdf/index.md Demonstrates how to initialize a Table instance with row data and attach it to a PDFKit document. The table automatically handles pagination if rows exceed the available page space. ```typescript const tableData = { rows: [ { backgroundColor: "#ECF0F1", columns: [ { text: "Row 1 cell 1" }, { text: "Row 1 cell 2" }, { text: "Row 1 cell 3" } ] }, { columns: [ { text: "Row 2 cell 1" }, { text: "Row 2 cell 2" }, { text: "Row 2 cell 3" } ] } ] }; const pdf = new PDFDocument(); const table = new Table(tableData); const stream = createWriteStream("table.pdf"); table.attachTo(pdf); pdf.pipe(stream); pdf.end(); ``` -------------------------------- ### Create SwissQRBill Instance (TypeScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md This snippet shows the constructor for the SwissQRBill class. It takes data for the QR bill and optional rendering options. It may throw a ValidationError if the provided data is invalid. ```typescript const qrBill = new SwissQRBill(data); ``` -------------------------------- ### SwissQRCode Constructor and Methods Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md Explains the constructor for the SwissQRCode class, which accepts data and an optional size in mm. It also details the toString() method for SVG output and the element() getter for the SVG element. This class may throw a ValidationError for invalid data. ```typescript // Constructor: new svg.SwissQRCode(data[, size]) // Method: svg.SwissQRCode.toString() // Getter: svg.SwissQRCode.element() ``` -------------------------------- ### Get SwissQRCode SVG Element Source: https://github.com/schoero/swissqrbill/blob/main/docs/svg/index.md The element() getter of the SwissQRCode class returns the SVG element representing the QR code. This allows for direct manipulation or integration into a web page's Document Object Model. ```typescript const qrCodeElement = qrCode.element(); ``` -------------------------------- ### Create and Attach PDF Table (JavaScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md Creates a PDF table from provided data and attaches it to a PDFKit document. The table can be positioned using x and y coordinates. It handles pagination by creating new pages as needed. Throws an error if no rows are provided. ```javascript const tableData = { rows: [ { backgroundColor: "#ECF0F1", columns: [ { text: "Row 1 cell 1" }, { text: "Row 1 cell 2" }, { text: "Row 1 cell 3" } ] }, { columns: [ { text: "Row 2 cell 1" }, { text: "Row 2 cell 2" }, { text: "Row 2 cell 3" } ] } ] }; const pdf = new PDFDocument(); const table = new Table(tableData); const stream = createWriteStream("table.pdf"); table.attachTo(pdf); pdf.pipe(stream); pdf.end(); ``` -------------------------------- ### PDF Table Constructor (JavaScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md Initializes a new Table instance for PDFKit documents. Accepts a data object defining the table's rows and columns. Returns a Table instance. ```javascript /** * Creates a new Table instance. * @param {PDFTable} data The rows and columns for the table. * @returns {Table} The Table instance. */ const table = new Table(data); ``` -------------------------------- ### Loading SwissQRBill via Browser CDN Source: https://github.com/schoero/swissqrbill/blob/main/docs/importing.md Shows how to include the pre-built bundles for PDFKit and SwissQRBill directly in an HTML file. The library is exposed via the global SwissQRBill variable. ```html ``` ```typescript const SwissQRBill = { errors, PDF, SVG, table, types, utils }; ``` -------------------------------- ### Create PDFDocument Instance Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Creates a new instance of PDFDocument with A4 page size. This initializes the PDF document object. ```ts const pdf = new PDFDocument({ size: "A4" }); ``` -------------------------------- ### Importing SwissQRBill in Node.js Source: https://github.com/schoero/swissqrbill/blob/main/docs/importing.md Demonstrates how to import the library using both modern ES modules and legacy CommonJS syntax. These methods require PDFKit as a dependency for PDF generation. ```typescript // ES Module import import PDFDocument from "pdfkit"; import { SwissQRBill } from "swissqrbill/pdf"; import { SwissQRBill } from "swissqrbill/svg"; ``` ```javascript // CommonJS import const PDFDocument = require("pdfkit"); const { SwissQRBill } = require("swissqrbill/pdf"); const { SwissQRBill } = require("swissqrbill/svg"); ``` -------------------------------- ### SwissQRBill Constructor and Methods Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md Details the constructor for the SwissQRBill class, which takes payment data and optional SVG options. It also outlines the toString() method for outputting the SVG as a string and the element() getter for retrieving the SVG element. ```typescript // Constructor: new svg.SwissQRBill(data[, options]) // Method: svg.SwissQRBill.toString() // Getter: svg.SwissQRBill.element() ``` -------------------------------- ### Namespace utils Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md Utility functions for validating and formatting Swiss IBANs, QR references, and amounts. ```APIDOC ## FUNCTION utils.isQRIBAN(iban) ### Description Checks if the provided string is a valid QR-IBAN. ### Parameters - **iban** (string) - Required - The IBAN string to validate. ## FUNCTION utils.formatAmount(amount) ### Description Formats a numeric amount into the standard Swiss QR bill currency format. ### Parameters - **amount** (number) - Required - The amount to format. ``` -------------------------------- ### Import PDFKit Library Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Imports the PDFDocument class from the PDFKit library. This is the first step in creating a PDF. ```ts import PDFDocument from "pdfkit"; ``` -------------------------------- ### Initialize and Attach QR Bill to PDF Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Shows how to instantiate the SwissQRBill class and attach both tables and QR bills to a PDFDocument instance. The attachTo method automatically manages page breaks if content exceeds the current page height. ```typescript import { SwissQRBill, Table } from "swissqrbill/pdf"; const stream = createWriteStream("qr-bill.pdf"); const pdf = new PDFDocument({ size: "A4" }); pdf.pipe(stream); const table = new Table({/* Table content */}); const qrBill = new SwissQRBill(data); table.attachTo(pdf); qrBill.attachTo(pdf); ``` -------------------------------- ### Create and Attach Swiss QR Bill to PDFKit Document (TypeScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/pdf/index.md Demonstrates how to create a SwissQRBill instance with payment data and attach it to a PDFKit document. Requires PDFKit and the SwissQRBill class. Outputs a PDF file with the QR bill. ```typescript import { createWriteStream } from "fs"; import PDFDocument from "pdfkit"; import { SwissQRBill } from "./src/pdf/swissqrbill"; 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({ autoFirstPage: false }); const qrBill = new SwissQRBill(data); const stream = createWriteStream("qr-bill.pdf"); qrBill.attachTo(pdf); pdf.pipe(stream); pdf.end(); ``` -------------------------------- ### Add vector logo paths to PDFDocument Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Demonstrates how to render a vector logo by defining individual SVG path strings and using the PDFKit path() method. This approach ensures high-quality, scalable graphics within the generated PDF. ```typescript const logoBackground = "M33 0H0v33h33V0z"; const logo = "M2 2h7v7H2V2zm1.05 5.95h4.9v-4.9h-4.9v4.9zM7 4H4v3h3V4zm16 13h1v2h-1v-2zm3 2v-1h-1v-1h-1v-1h-1v-1h3v1h1v2h1v1h1v-2h-1v-2h-1v-1h2v1h2v2h-1v3h-1v3h2v1h-1v1h1v4h-1v-2h-2v-1h1v-2h-1v2h-1v1h1v1h-2v-1h-2v1h-1v-1h-1v-1h-2v-3h1v1h1v-1h1v3h3v-3h-3v-2h1v1h4v-2h-1v-1h-1zm0 0h-1v1h-1v1h2v-2zm-6 9v-2h-1v-3h-1v2h-1v1h-1v1h-2v1h-2v-1h1v-2h1v-1h-1v-1h-2v-5H8v-1h2v-1h1v-2h-1v-1h1v-1h-1v-2h1v1h2v-1h2V6h1v1h4V5h-1V4h1V3h-1V2h-1v3h-2V4h1V2h-1v1h-3v1h-1v1h-2v1h1v1h1V5h1v1h1v3h-1V8h-1v1h-1V8h-1v2H7v1H6v-1H4v2H2v1h1v1h1v1h1v2h1v2H5v-1H4v-1H2v1h2v1h1v1H4v2H3v1h1v-1h1v1h5v3h2v1h-2v2h1v-1h1v1h2v2h4v-1h1v-1h-1v1h-1v-1h-1v1h-1v-1h-1v-1h6zM5 20h1v-1h4v3H8v-1h1v-1H7v1H5v-1zm3-6v1h1v1H7v1H6v-2H5v-2H4v-1h2v2h2zm1-1v1H8v-1h1zm0-1H7v-1h2v1zm0 0v1h1v-1H9zm5-7V4h-1v1h1zm0 0v1h1V5h-1zm-1 19v1h-2v-1h2zm7 4v1h1v2h2v-1h1v1h1v-1h1v-2h-1v1h-3v-2h-1v1h-1zm9 1v-1h-1v1h-1v2h1v-2h1zm0 0h1v2h-1v-2zm-2-18h-2v-1h-1v1h-1v2h2v1h2v-3zm0 0h1v-1h-1v1zm-2 13h-1v1h1v-1zM21 8v3h-2V9h1V8h1zm1-3v1h1v3h-1V8h-1V5h1zm0 0V4h1v1h-1zm-4 4V8h1v1h-1zm0 0h-1V8h-1v3h1v-1h1V9zm-3 16h1v-2h-1v2zM29 4h-3v3h3V4zM7 26H4v3h3v-3zM24 2h7v7h-7V2zm1.05 5.95h4.9v-4.9h-4.9v4.9zM2 24h7v7H2v-7zm1.05 5.95h4.9v-4.9h-4.9v4.9zM2 10h1v1H2v-1zm27 0v1h1v1h-2v1h2v1h1v-1h-1v-1h1v-2h-2zM3 20H2v1h1v-1zm7 10h3v1h-3v-1zm0-28h1v2h-1V2zm13 0h-2v1h2V2zm-9 14v2h2v2h2v-2h2v-2h-2v-2h-2v2h-2z"; const logoText = "M45.264 20.48c0 .907-.25 1.59-.752 2.048-.49.448-1.2.672-2.128.672h-2.112v-1.216h2.272c.395 0 .715-.117.96-.352s.368-.517.368-.848V19.12c0-.32-.085-.576-.256-.768-.16-.203-.405-.304-.736-.304h-.32c-.79 0-1.413-.23-1.872-.688-.459-.47-.688-1.147-.688-2.032v-.608c0-.907.245-1.584.736-2.032.501-.459 1.216-.688 2.144-.688h2.128v1.216H42.72c-.395 0-.715.117-.96.352a1.131 1.131 0 0 0-.368.848v1.216c0 .33.107.608.32.832.213.213.49.32.832.32h.32c.757 0 1.344.219 1.76.656.427.437.64 1.099.64 1.984v1.056zM53.851 23.2h-2.096l-1.008-4.336-1.104 4.336h-2.048l-1.6-8h1.408l1.296 6.976 1.424-5.536h1.36l1.328 5.536 1.296-6.976h1.328l-1.584 8zM56.634 13.44V12h1.52v1.44h-1.52zm.08 9.76v-8h1.36v8h-1.36zM64.22 21.2c0 .672-.208 1.173-.624 1.504-.406.33-.998.496-1.776.496h-2.064V22h2.224c.288 0 .506-.059.656-.176.149-.128.224-.31.224-.544v-.848c0-.48-.23-.72-.688-.72H61.9c-.747 0-1.307-.16-1.68-.48-.363-.33-.544-.848-.544-1.552v-.464c0-.715.202-1.227.608-1.536.405-.32 1.002-.48 1.792-.48H63.9v1.2h-1.984c-.267 0-.48.064-.64.192-.16.117-.24.293-.24.528v.576c0 .245.069.427.208.544.149.117.336.176.56.176h.224c.714 0 1.258.176 1.632.528.373.341.56.853.56 1.536v.72zM69.814 21.2c0 .672-.208 1.173-.624 1.504-.405.33-.997.496-1.776.496H65.35V22h2.224c.288 0 .507-.059.656-.176.15-.128.224-.31.224-.544v-.848c0-.48-.23-.72-.688-.72h-.272c-.747 0-1.307-.16-1.68-.48-.363-.33-.544-.848-.544-1.552v-.464c0-.715.203-1.227.608-1.536.405-.32 1.003-.48 1.792-.48h1.824v1.2H67.51c-.267 0-.48.064-.64.192-.16.117-.24.293-.24.528v.576c0 .245.07.427.208.544.15.117.336.176.56.176h.224c.715 0 1.259.176 1.632.528.373.341.56.853.56 1.536v.72zM76.237 22.896c-.416.203-.934.304-1.552.304h-.544c-.928 0-1.643-.224-2.144-.672-.491-.459-.736-1.141-.736-2.048v-5.76c0-.907.245-1.584.736-2.032.501-.459 1.216-.688 2.144-.688h.544c.928 0 1.637.23 2.128.688.501.448.752 1.125.752 2.032v5.76c0 .63-.128 1.147-.384 1.552l1.2 1.2-.912.912-1.232-1.248zm-.064-8.48c0-.33-.123-.613-.368-.848-.246-.235-.566-.352-.96-.352h-.864c-.395 0-.715.117-.96.352a1.131 1.131 0 0 0-.368.848v6.368c0 .33.122.613.368.848.245.235.565.352.96.352h.864c.405 0 .725-.101.96-.304.245-.213.368-.512.368-.896v-6.368zM82.3 18.56h-1.424v4.64h-1.392V12h2.864c.906 0 1.59.203 2.048.608.47.395.704 1.024.704 1.888v1.536c0 1.205-.48 1.957-1.44 2.256l2.224 4.912h-1.52L82.3 18.56zm1.408-4.208c0-.405-.096-.699-.288-.88-.182-.181-.475-.272-.88-.272h-1.664v4.16h1.664c.405 0 .698-.09.88-.272.192-.192.288-.49.288-.896v-1.84zM92.916 20.672c0 .917-.213 1.568-.64 1.952-.416.384-1.11.576-2.08.576h-3.04V12h2.848c.95 0 1.643.192 2.08.576.448.384.672 1.024.672 1.92v.912c0 .821-.293 1.44-.88 1.856.694.384 1.04 1.04 1.04 1.968v1.44zm-1.552-6.32c0-.405-.096-.699-.288-.88-.181-.181-.474-.272-.88-.272h-1.648v3.536h1.648c.33 0 .608-.107.832-.32.224-.213.336-.496.336-.848v-1.216zm.16 4" ``` -------------------------------- ### Check Space Sufficiency for SwissQRBill (TypeScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md This static method checks if a PDFKit document has enough space to accommodate the QR Bill at a specified position. It returns a boolean indicating sufficiency. ```typescript const isSufficient = SwissQRBill.isSpaceSufficient(pdf); ``` -------------------------------- ### Import createWriteStream Function Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Imports the createWriteStream function from Node.js's built-in 'fs' module. This function is used to create a writable stream to a file. ```ts import { createWriteStream } from "node:fs"; ``` -------------------------------- ### Pipe PDF Output to File Stream Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Creates a writable stream to 'qr-bill.pdf' and pipes the PDF document output to this stream. This writes the generated PDF content to a file. ```ts const stream = createWriteStream("qr-bill.pdf"); const pdf = new PDFDocument({ size: "A4" }); pdf.pipe(stream); ``` -------------------------------- ### Attach SwissQRBill to PDF Document (TypeScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md This method attaches a generated SwissQRBill to an existing PDFKit document. It can optionally take x and y coordinates for placement. If there isn't enough space, it might create a new page. ```typescript qrBill.attachTo(pdf); ``` -------------------------------- ### Reference Utility Functions Source: https://github.com/schoero/swissqrbill/blob/main/docs/utils/utils.md Functions for validating and processing QR-References and SCOR-References. ```APIDOC ## Function: isQRReference(reference) ### Description Checks whether the given reference is a QR-Reference. ### Parameters - **reference** (string) - Required - The Reference to be checked. ### Response - **Returns** (boolean) - true if the given reference is a QR-Reference, false otherwise. --- ## Function: isQRReferenceValid(reference) ### Description Validates the given QR-Reference. ### Parameters - **reference** (string) - Required - The reference to be checked. ### Response - **Returns** (boolean) - true if the given reference is valid, false otherwise. --- ## Function: isSCORReference(reference) ### Description Checks whether the given reference is a SCOR-Reference. ### Parameters - **reference** (string) - Required - The Reference to be checked. ### Response - **Returns** (boolean) - true if the given reference is a SCOR-Reference, false otherwise. ``` -------------------------------- ### PDFOptions Interface Source: https://github.com/schoero/swissqrbill/blob/main/docs/svg/types.md Configuration options for rendering a Swiss QR-Bill as a PDF document. ```APIDOC ## PDFOptions Interface ### Description Configuration settings for customizing the PDF output of a Swiss QR-Bill. ### Parameters - **fontName** (string) - Optional - Font used (Arial, Frutiger, Helvetica, Liberation Sans) - **language** (string) - Optional - Language (DE, EN, FR, IT, RM) - **outlines** (boolean) - Optional - Whether to render outlines - **scissors** (boolean) - Optional - Whether to show scissors icons - **separate** (boolean) - Optional - Whether to show separation text ### Request Example { "fontName": "Helvetica", "language": "EN", "outlines": true, "scissors": false } ``` -------------------------------- ### Render QR Bill as SVG Source: https://github.com/schoero/swissqrbill/blob/main/README.md Shows how to render the QR bill component as an SVG element for web applications. Note that this method only renders the QR bill part and not a full invoice document. ```javascript import { SwissQRBill } from "swissqrbill/svg"; const svg = new SwissQRBill(data); document.body.appendChild(svg.element); ``` -------------------------------- ### Create SwissQRCode SVG Source: https://github.com/schoero/swissqrbill/blob/main/docs/svg/index.md The SwissQRCode constructor creates a QR code based on the provided data and an optional size in mm. It can throw a ValidationError if the input data is invalid. The resulting QR code can be output as a string or element. ```typescript const qrCode = new SwissQRCode(data, 50); // Size in mm ``` -------------------------------- ### Generate QR Bill PDF in Browser Source: https://context7.com/schoero/swissqrbill/llms.txt Demonstrates how to generate a Swiss QR Bill PDF directly in the browser using CDN-hosted bundles. It utilizes PDFKit and blob-stream to render the document into an iframe preview. ```html Swiss QR Bill Generator ``` -------------------------------- ### Attach Swiss QR Code to PDFDocument (JavaScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md Attaches a Swiss QR Code to a PDF document at specified coordinates. Requires a PDFDocument instance and optionally accepts x and y positions. Returns void. ```javascript /** * Attaches the Swiss QR Code to a PDF document. * @param {PDFDocument} doc The PDF document to attach the Swiss QR Code to. * @param {number} [x] The horizontal position in points where the Swiss QR Code will be placed. * @param {number} [y] The vertical position in points where the Swiss QR Code will be placed. * @returns {void} */ function attachSwissQrCode(doc, x, y) { // Implementation details... } ``` -------------------------------- ### Interface: pdf.PDFTable Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md Defines the structure and styling options for a PDF table component. ```APIDOC ## Interface: pdf.PDFTable ### Description Defines the configuration for a table within a PDF document, including layout, styling, and content alignment. ### Parameters - **rows** (Array) - Required - Collection of table rows. - **align** (string) - Optional - Horizontal alignment: "center", "left", or "right". - **backgroundColor** (string) - Optional - Background color of the table. - **borderColor** (PDFBorderColor) - Optional - Border color configuration. - **borderWidth** (PDFBorderWidth) - Optional - Border width configuration. - **fontName** (string) - Optional - Font family name. - **fontSize** (number) - Optional - Size of the text font. - **padding** (PDFPadding) - Optional - Cell padding configuration. - **textColor** (string) - Optional - Text color. - **textOptions** (TextOptions) - Optional - PDFKit text styling options. - **verticalAlign** (string) - Optional - Vertical alignment: "bottom", "center", or "top". - **width** (number) - Optional - Total width of the table. ### Request Example { "rows": [], "align": "left", "fontSize": 12, "width": 500 } ``` -------------------------------- ### Create PDF Table with SwissQRBill Source: https://context7.com/schoero/swissqrbill/llms.txt Generates a formatted table for PDFKit documents, supporting headers, styling, and page breaks. It's useful for itemized lists in invoices. Requires PDFKit and swissqrbill libraries. ```javascript import { createWriteStream } from "node:fs"; import PDFDocument from "pdfkit"; import { Table, SwissQRBill } from "swissqrbill/pdf"; import { mm2pt } from "swissqrbill/utils"; const pdf = new PDFDocument({ size: "A4" }); const stream = createWriteStream("invoice-with-table.pdf"); const table = new Table({ width: mm2pt(170), rows: [ { backgroundColor: "#4A4D51", fontName: "Helvetica-Bold", textColor: "#fff", height: 20, padding: 5, header: true, // Repeats on new pages columns: [ { text: "Position", width: mm2pt(20) }, { text: "Qty", width: mm2pt(20) }, { text: "Description" }, { text: "Total", width: mm2pt(30) } ] }, { padding: 5, columns: [ { text: "1", width: mm2pt(20) }, { text: "14 Std.", width: mm2pt(20) }, { text: "Development Services" }, { text: "CHF 1'540.00", width: mm2pt(30) } ] }, { padding: 5, columns: [ { text: "2", width: mm2pt(20) }, { text: "8 Std.", width: mm2pt(20) }, { text: "Documentation" }, { text: "CHF 880.00", width: mm2pt(30) } ] }, { padding: 5, height: 40, columns: [ { text: "", width: mm2pt(20) }, { text: "", width: mm2pt(20) }, { text: "Total", fontName: "Helvetica-Bold" }, { text: "CHF 2'420.00", fontName: "Helvetica-Bold", width: mm2pt(30) } ] } ] }); table.attachTo(pdf, mm2pt(20), mm2pt(100)); const qrBillData = { amount: 2420.00, creditor: { account: "CH44 3199 9123 0008 8901 2", address: "Musterstrasse", buildingNumber: 7, city: "Musterstadt", country: "CH", name: "SwissQRBill", zip: 1234 }, currency: "CHF", reference: "21 00000 00003 13947 14300 09017" }; const qrBill = new SwissQRBill(qrBillData); qrBill.attachTo(pdf); pdf.pipe(stream); pdf.end(); // Output: Creates invoice-with-table.pdf with itemized table and QR payment slip ``` -------------------------------- ### Handle Validation Errors Source: https://context7.com/schoero/swissqrbill/llms.txt Demonstrates how to catch and handle ValidationError instances thrown by the library. It uses error codes to provide specific feedback for invalid input data like malformed IBANs. ```javascript import { SwissQRBill } from "swissqrbill/pdf"; import { ValidationError } from "swissqrbill/errors"; try { const qrBill = new SwissQRBill(invalidData); } catch (error) { if (error instanceof ValidationError) { console.log(error.code); switch (error.code) { case "CREDITOR_ACCOUNT_IS_INVALID": console.log("Please enter a valid IBAN"); break; } } } ``` -------------------------------- ### PDF Table attachTo Method (JavaScript) Source: https://github.com/schoero/swissqrbill/blob/main/docs/bundle/index.md Attaches an existing Table instance to a PDFKit document. Allows specifying the horizontal (x) and vertical (y) position for the table. Handles multi-page tables by creating new pages as necessary. Throws an error if no table rows are defined. ```javascript /** * Attaches the table to a PDFKit document instance beginning on the current page. * @param {PDFDocument} doc The PDFKit document instance. * @param {number} [x] The horizontal position in points where the table be placed. * @param {number} [y] The vertical position in points where the table will be placed. * @throws {Error} Throws an error if no table rows are provided. * @returns {void} */ table.attachTo(doc, x, y); ``` -------------------------------- ### Define QR Bill Data Structure Source: https://github.com/schoero/swissqrbill/blob/main/docs/how-to-create-a-complete-qr-bill.md Defines a JavaScript object containing all necessary information for generating a QR bill, including amount, creditor, debtor, currency, and reference number. ```ts 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" }; ``` -------------------------------- ### IBAN Utility Functions Source: https://github.com/schoero/swissqrbill/blob/main/docs/utils/utils.md Functions for validating and formatting Swiss IBANs and QR-IBANs. ```APIDOC ## Function: isQRIBAN(iban) ### Description Checks whether the given iban is a QR-IBAN or not. ### Parameters - **iban** (string) - Required - The IBAN to be checked. ### Response - **Returns** (boolean) - true if the given IBAN is a QR-IBAN, false otherwise. --- ## Function: isIBANValid(iban) ### Description Validates the given IBAN checksum. ### Parameters - **iban** (string) - Required - The IBAN to be checked. ### Response - **Returns** (boolean) - true if the checksum of the given IBAN is valid, false otherwise. --- ## Function: formatIBAN(iban) ### Description Formats the given IBAN according to specifications for readability. ### Parameters - **iban** (string) - Required - The IBAN to be formatted. ### Response - **Returns** (string) - The formatted IBAN. ``` -------------------------------- ### Create SwissQRBill SVG Source: https://github.com/schoero/swissqrbill/blob/main/docs/svg/index.md Instantiates the SwissQRBill class with provided data and options to generate a QR bill SVG. The resulting SVG can be output as a string using the toString() method. This requires the 'SwissQRBill' class and potentially 'writeFileSync' for saving. ```typescript 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 SwissQRBill(data); writeFileSync("qr-bill.svg", svg.toString()); ```