Socket
Socket
Sign inDemoInstall

swissqrbill

Package Overview
Dependencies
Maintainers
1
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swissqrbill - npm Package Compare versions

Comparing version 4.0.0-beta.2 to 4.0.0-beta.3

45

lib/pdf/swissqrbill.d.ts

@@ -6,2 +6,36 @@ /// <reference types="pdfkit" />

* using the {@link SwissQRBill.attachTo} method.
* @example
* ```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"
* };
*
* 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();
* ```
*/

@@ -21,12 +55,13 @@ export declare class SwissQRBill {

* @param options Options to define how the QR Bill should be rendered.
* @throws { ValidationError } Throws an error if the data is invalid.
*/
constructor(data: Data, options?: PDFOptions);
/**
* Adds the QR Bill to the bottom of the current page if there is enough space,
* otherwise it will create a new page for the QR Bill.
* Attaches the QR-Bill to a PDFKit document instance. It will create a new page with the size of the QR-Slip if not
* enough space is left on the current page.
* @param doc The PDFKit instance
* @param xPosition The horizontal position where the QR Bill will be placed.
* @param yPosition The vertical position where the QR Bill will be placed.
* @param x The horizontal position in points where the QR Bill will be placed.
* @param y The vertical position in points where the QR Bill will be placed.
*/
attachTo(doc: PDFKit.PDFDocument, xPosition?: number, yPosition?: number): void;
attachTo(doc: PDFKit.PDFDocument, x?: number, y?: number): void;
/**

@@ -33,0 +68,0 @@ * Checks whether there is enough space on the current page to add the QR Bill.

33

lib/pdf/swissqrbill.js

@@ -30,2 +30,3 @@ var __defProp = Object.defineProperty;

* @param options Options to define how the QR Bill should be rendered.
* @throws { ValidationError } Throws an error if the data is invalid.
*/

@@ -40,8 +41,7 @@ constructor(data, options) {

this._y = 0;
this.data = data;
this.data = cleanData(this.data);
void validateData(this.data);
this.data = cleanData(data);
validateData(this.data);
this.language = (options == null ? void 0 : options.language) !== void 0 ? options.language : this.language;
this.outlines = (options == null ? void 0 : options.outlines) !== void 0 ? options.outlines : this.outlines;
this.font = (options == null ? void 0 : options.font) !== void 0 ? options.font : this.font;
this.font = (options == null ? void 0 : options.fontName) !== void 0 ? options.fontName : this.font;
if ((options == null ? void 0 : options.scissors) !== void 0) {

@@ -61,10 +61,10 @@ this.scissors = options.scissors;

/**
* Adds the QR Bill to the bottom of the current page if there is enough space,
* otherwise it will create a new page for the QR Bill.
* Attaches the QR-Bill to a PDFKit document instance. It will create a new page with the size of the QR-Slip if not
* enough space is left on the current page.
* @param doc The PDFKit instance
* @param xPosition The horizontal position where the QR Bill will be placed.
* @param yPosition The vertical position where the QR Bill will be placed.
* @param x The horizontal position in points where the QR Bill will be placed.
* @param y The vertical position in points where the QR Bill will be placed.
*/
attachTo(doc, xPosition = 0, yPosition = doc.page.height - mm2pt(105)) {
if (!_SwissQRBill2.isSpaceSufficient(doc, xPosition, yPosition)) {
attachTo(doc, x = 0, y = ((_a) => (_a = doc.page) == null ? void 0 : _a.height)() ? ((_b) => (_b = doc.page) == null ? void 0 : _b.height)() - mm2pt(105) : 0) {
if (!_SwissQRBill2.isSpaceSufficient(doc, x, y)) {
doc.addPage({

@@ -74,7 +74,7 @@ margin: 0,

});
xPosition = 0;
yPosition = 0;
x = 0;
y = 0;
}
this._x = xPosition;
this._y = yPosition;
this._x = x;
this._y = y;
this.render(doc);

@@ -232,6 +232,3 @@ }

const swissQRCode = new SwissQRCode(this.data);
doc.save();
doc.translate(this.x(67), this.y(17));
swissQRCode.attachTo(doc);
doc.restore();
swissQRCode.attachTo(doc, this.x(67), this.y(17));
doc.fontSize(8);

@@ -238,0 +235,0 @@ doc.font(`${this.font}-Bold`);

@@ -10,2 +10,3 @@ /// <reference types="pdfkit" />

* @param size The size of the QR code in mm.
* @throws { ValidationError } Throws an error if the data is invalid.
*/

@@ -16,4 +17,6 @@ constructor(data: Data, size?: number);

* @param doc The PDF document to attach the Swiss QR Code to.
* @param x The horizontal position in points where the Swiss QR Code will be placed.
* @param y The vertical position in points where the Swiss QR Code will be placed.
*/
attachTo(doc: PDFKit.PDFDocument): void;
attachTo(doc: PDFKit.PDFDocument, x?: number, y?: number): void;
}

@@ -0,3 +1,5 @@

import { cleanData } from "../shared/cleaner.js";
import { renderQRCode, renderSwissCross } from "../shared/qr-code.js";
import { mm2pt } from "../shared/utils.js";
import { validateData } from "../shared/validator.js";
class SwissQRCode {

@@ -8,6 +10,8 @@ /**

* @param size The size of the QR code in mm.
* @throws { ValidationError } Throws an error if the data is invalid.
*/
constructor(data, size = 46) {
this.size = mm2pt(size);
this.data = data;
this.data = cleanData(data);
validateData(this.data);
}

@@ -17,5 +21,8 @@ /**

* @param doc The PDF document to attach the Swiss QR Code to.
* @param x The horizontal position in points where the Swiss QR Code will be placed.
* @param y The vertical position in points where the Swiss QR Code will be placed.
*/
attachTo(doc) {
attachTo(doc, x = ((_a) => (_a = doc.x) != null ? _a : 0)(), y = ((_b) => (_b = doc.y) != null ? _b : 0)()) {
doc.save();
doc.translate(x, y);
renderQRCode(this.data, this.size, (xPos, yPos, blockSize) => {

@@ -22,0 +29,0 @@ doc.rect(

@@ -27,6 +27,2 @@ /// <reference types="pdfkit" />

width?: number;
/** Horizontal start position of the table. */
x?: number;
/** Vertical start position of the table. */
y?: number;
}

@@ -91,37 +87,49 @@ export interface PDFRow {

}
/**
* The Table class is used to create tables for PDFKit documents. A table can be attached to any PDFKit document instance
* using the {@link Table.attachTo} method.
* @example
* ```ts
* 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();
* ```
*/
export declare class Table {
private data;
/**
* Inserts a table to the document.
* @param data An Object which contains the table information.
* Creates a new Table instance.
* @param data The rows and columns for the table.
* @returns The Table instance.
* @example
* ```ts
* const table = {
* 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"
* }
* ]
* }
* ]
* };
* ```
*/

@@ -131,9 +139,11 @@ constructor(data: PDFTable);

/**
* Attaches the table to a PDFKit document instance.
* Attaches the table to a PDFKit document instance beginning on the current page. It will create a new page with for
* every row that no longer fits on a page.
* @param doc The PDFKit document instance
* @returns The Table instance.
* @param x The horizontal position in points where the table be placed.
* @param y The vertical position in points where the table will be placed.
* @throws { Error } Throws an error if no table rows are provided.
*/
attachTo(doc: PDFKit.PDFDocument): this;
attachTo(doc: PDFKit.PDFDocument, x?: number, y?: number): void;
private _positionsToObject;
}

@@ -30,34 +30,5 @@ var __defProp = Object.defineProperty;

/**
* Inserts a table to the document.
* @param data An Object which contains the table information.
* Creates a new Table instance.
* @param data The rows and columns for the table.
* @returns The Table instance.
* @example
* ```ts
* const table = {
* 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"
* }
* ]
* }
* ]
* };
* ```
*/

@@ -79,17 +50,21 @@ constructor(data) {

/**
* Attaches the table to a PDFKit document instance.
* Attaches the table to a PDFKit document instance beginning on the current page. It will create a new page with for
* every row that no longer fits on a page.
* @param doc The PDFKit document instance
* @returns The Table instance.
* @param x The horizontal position in points where the table be placed.
* @param y The vertical position in points where the table will be placed.
* @throws { Error } Throws an error if no table rows are provided.
*/
attachTo(doc) {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
attachTo(doc, x = ((_a) => (_a = doc.x) != null ? _a : 0)(), y = ((_b) => (_b = doc.y) != null ? _b : 0)()) {
var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
if (this.data.rows === void 0) {
throw new Error("No table rows provided.");
}
if (!doc.page) {
doc.addPage({ size: "A4" });
}
doc.options.bufferPages = true;
const startX = doc.x;
const tableX = x;
const tableY = y;
const startPage = this.getCurrentPage(doc);
const tableX = this.data.x ? this.data.x : doc.x;
const tableY = this.data.y ? this.data.y : doc.y;
const tableWidth = this.data.width ? this.data.width : doc.page.width - tableX - doc.page.margins.right;

@@ -150,6 +125,6 @@ const tableBackgroundColor = this.data.backgroundColor ? this.data.backgroundColor : void 0;

doc.moveTo(columnX + columnWidth, rowY);
const textOptions = __spreadProps(__spreadValues({}, (_a = column.textOptions) != null ? _a : {}), {
const textOptions = __spreadProps(__spreadValues({}, (_a2 = column.textOptions) != null ? _a2 : {}), {
align: columnAlign,
baseline: "middle",
height: rowHeight !== void 0 ? rowHeight - ((_b = paddings.top) != null ? _b : 0) - ((_c = paddings.bottom) != null ? _c : 0) : void 0,
height: rowHeight !== void 0 ? rowHeight - ((_b2 = paddings.top) != null ? _b2 : 0) - ((_c = paddings.bottom) != null ? _c : 0) : void 0,
lineBreak: true,

@@ -260,4 +235,3 @@ width: columnWidth - ((_d = paddings.left) != null ? _d : 0) - ((_e = paddings.right) != null ? _e : 0)

}
doc.x = startX;
return this;
doc.x = tableX;
}

@@ -264,0 +238,0 @@ _positionsToObject(numberOrPositions) {

@@ -8,3 +8,3 @@ import { cleanData } from "./cleaner.js";

const cleanedData = cleanData(data);
void validateData(cleanedData);
validateData(cleanedData);
const amount = (_a = cleanedData.amount) == null ? void 0 : _a.toFixed(2);

@@ -155,3 +155,3 @@ const reference = getReferenceType(cleanedData.reference);

const swissCrossLength = mm2pt(3.89) * scale;
void renderRectFunction(
renderRectFunction(
size / 2 - swissCrossWhiteBackgroundSize / 2,

@@ -163,3 +163,3 @@ size / 2 - swissCrossWhiteBackgroundSize / 2,

);
void renderRectFunction(
renderRectFunction(
size / 2 - swissCrossBlackBackgroundSize / 2,

@@ -171,3 +171,3 @@ size / 2 - swissCrossBlackBackgroundSize / 2,

);
void renderRectFunction(
renderRectFunction(
size / 2 - swissCrossLength / 2,

@@ -179,3 +179,3 @@ size / 2 - swissCrossThickness / 2,

);
void renderRectFunction(
renderRectFunction(
size / 2 - swissCrossThickness / 2,

@@ -182,0 +182,0 @@ size / 2 - swissCrossLength / 2,

@@ -92,3 +92,3 @@ export type Currency = "CHF" | "EUR";

*/
font?: FontName;
fontName?: FontName;
/**

@@ -95,0 +95,0 @@ * The language with which the bill is rendered.

@@ -5,4 +5,4 @@ import type { Data } from "./types";

* @param data The data to validate.
* @throws {ValidationError} If the data is invalid.
* @throws { ValidationError } If the data is invalid.
*/
export declare function validateData(data: Data): void;
import { SVG } from "svg-engine";
import type { Data, SVGOptions } from '../shared/types';
/**
* The SwissQRBill class creates the Payment Part with the QR Code as an SVG.
* @example
* ```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"
* };
*
* const svg = new SwissQRBill(data);
* writeFileSync("qr-bill.svg", svg.toString());
* ```
*/
export declare class SwissQRBill {
protected instance: SVG;
instance: SVG;
private scissors;

@@ -11,6 +42,5 @@ private outlines;

constructor(data: Data, options?: SVGOptions);
get outerHTML(): string;
/**
* Outputs the SVG as a string.
* @returns The outerHTML of the SVG as a `string`.
* @returns The outerHTML of the SVG.
*/

@@ -20,3 +50,2 @@ toString(): string;

* Returns the SVG element.
* @readonly
* @returns The SVG element.

@@ -23,0 +52,0 @@ */

@@ -19,3 +19,3 @@ import { SVG, calc } from "svg-engine";

this.outlines = (options == null ? void 0 : options.outlines) !== void 0 ? options.outlines : this.outlines;
this.font = (options == null ? void 0 : options.font) !== void 0 ? options.font : this.font;
this.font = (options == null ? void 0 : options.fontName) !== void 0 ? options.fontName : this.font;
this.scissors = (options == null ? void 0 : options.scissors) !== void 0 ? options.scissors : this.scissors;

@@ -27,15 +27,11 @@ this.instance = new SVG();

}
get outerHTML() {
return this.instance.outerHTML;
}
/**
* Outputs the SVG as a string.
* @returns The outerHTML of the SVG as a `string`.
* @returns The outerHTML of the SVG.
*/
toString() {
return this.outerHTML;
return this.instance.outerHTML;
}
/**
* Returns the SVG element.
* @readonly
* @returns The SVG element.

@@ -204,4 +200,4 @@ */

const qrCode = new SwissQRCode(this.data);
qrCode.x("67mm").y("17mm");
this.instance.appendInstance(qrCode);
qrCode.instance.x("67mm").y("17mm");
this.instance.appendInstance(qrCode.instance);
}

@@ -208,0 +204,0 @@ _formatAddress(data) {

import SVG from "svg-engine";
import type { Data } from "swissqrbill:shared:types";
export declare class SwissQRCode extends SVG {
export declare class SwissQRCode {
instance: SVG;
/**

@@ -8,4 +9,15 @@ * Creates a Swiss QR Code.

* @param size The size of the QR code in mm.
* @throws { ValidationError } Throws an error if the data is invalid.
*/
constructor(data: Data, size?: number);
/**
* Outputs the SVG as a string.
* @returns The outerHTML of the SVG element.
*/
toString(): string;
/**
* Returns the SVG element.
* @returns The SVG element.
*/
get element(): SVGElement;
}
import SVG from "svg-engine";
import { renderQRCode, renderSwissCross } from "../shared/qr-code.js";
class SwissQRCode extends SVG {
class SwissQRCode {
/**

@@ -8,9 +8,10 @@ * Creates a Swiss QR Code.

* @param size The size of the QR code in mm.
* @throws { ValidationError } Throws an error if the data is invalid.
*/
constructor(data, size = 46) {
super();
this.width(`${size}mm`);
this.height(`${size}mm`);
this.instance = new SVG();
this.instance.width(`${size}mm`);
this.instance.height(`${size}mm`);
renderQRCode(data, size, (xPos, yPos, blockSize) => {
this.addRect(
this.instance.addRect(
`${xPos}mm`,

@@ -23,3 +24,3 @@ `${yPos}mm`,

renderSwissCross(size, (xPos, yPos, width, height, fillColor) => {
this.addRect(
this.instance.addRect(
`${xPos}mm`,

@@ -32,2 +33,16 @@ `${yPos}mm`,

}
/**
* Outputs the SVG as a string.
* @returns The outerHTML of the SVG element.
*/
toString() {
return this.instance.outerHTML;
}
/**
* Returns the SVG element.
* @returns The SVG element.
*/
get element() {
return this.instance.element;
}
}

@@ -34,0 +49,0 @@ export {

{
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.3",
"type": "module",

@@ -62,2 +62,3 @@ "name": "swissqrbill",

"postrelease:latest": "eslint --fix package.json && markdownlint-cli2-fix 'CHANGELOG.md'",
"prebuild": "npm run typecheck && npm run lint && npm run spellcheck",
"prerelease:alpha": "npm run test -- --run && npm run build",

@@ -76,6 +77,7 @@ "prerelease:beta": "npm run test -- --run && npm run build",

"test:update": "npm run test -- --update --run",
"test:visual": "cross-env VISUAL=true npm run test -- --run"
"test:visual": "cross-env VISUAL=true npm run test -- --run",
"typecheck": "tsc --noEmit"
},
"engines": {
"node": ">=v13.6.0"
"node": ">=18.0.0"
},

@@ -102,3 +104,3 @@ "files": [

"@cspell/dict-fr-fr": "^2.2.2",
"@cspell/dict-it-it": "^3.0.1",
"@cspell/dict-it-it": "^3.1.0",
"@schoero/changelog-config": "^0.0.2",

@@ -111,7 +113,7 @@ "@schoero/cspell-config": "^1.8.2",

"@schoero/vite-config": "^0.0.28",
"@types/node": "^20.8.9",
"@types/pdfkit": "^0.13.1",
"@types/svg-parser": "^2.0.5",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"@types/node": "^20.9.0",
"@types/pdfkit": "^0.13.2",
"@types/svg-parser": "^2.0.6",
"@typescript-eslint/eslint-plugin": "^6.10.0",
"@typescript-eslint/parser": "^6.10.0",
"cross-env": "^7.0.3",

@@ -121,4 +123,4 @@ "glob": "^10.3.10",

"typescript": "^5.2.2",
"unwritten": "^0.1.3",
"vite-plugin-dts": "^3.6.2",
"unwritten": "^0.2.3",
"vite-plugin-dts": "^3.6.3",
"vite-plugin-no-bundle": "^3.0.0",

@@ -125,0 +127,0 @@ "vitest": "^0.34.6"

<div align="center">
<img alt="SwissQRBill" src="https://raw.githubusercontent.com/schoero/SwissQRBill/master/assets/swissqrbill-logo.svg">
<img alt="SwissQRBill" src="https://raw.githubusercontent.com/schoero/SwissQRBill/main/assets/swissqrbill-logo.svg">
</div>

@@ -27,3 +27,3 @@

![QR bill](assets/qr-bill.svg)
![QR bill](assets/qr-bill.png)

@@ -40,5 +40,5 @@ <br/>

* [Quick start](#quick-start)
* [API documentation](https://github.com/schoero/SwissQRBill/blob/master/doc/api.md)
* [API documentation](./docs)
* [PDFKit documentation](http://pdfkit.org/docs/getting_started.html)
* [How to create a complete bill](https://github.com/schoero/SwissQRBill/blob/master/doc/how-to-create-a-complete-bill.md)
* [how to create a complete qr bill](./docs/how-to-create-a-complete-qr-bill.md)
* [QR bill validator](https://swiss-qr-invoice.org/validator/?lang=de)

@@ -55,2 +55,4 @@ * [QR bill specifications](https://www.six-group.com/dam/download/banking-services/standardization/qr-bill/ig-qr-bill-v2.2-en.pdf)

<br/>
## Features

@@ -146,3 +148,3 @@

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

@@ -153,2 +155,2 @@ ![Complete QR bill](assets/complete-qr-bill.png)

[repository docs]: ./docs/
[how to create a complete bill]: ./docs/how-to-create-a-complete-bill.md
[how to create a complete qr bill]: ./docs/how-to-create-a-complete-qr-bill.md

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc