Skip to content

Commit

Permalink
docs: add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
schoero committed Sep 30, 2023
1 parent 392a656 commit d9334b4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
33 changes: 29 additions & 4 deletions docs/importing.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,42 @@ Depending how you intend to use the library, there are different ways to import

### Node.js

- [bundling with webpack](https://stackblitz.com/github/schoero/swissqrbill/tree/master/examples/)
- [Node ESM import](node-esm-import) [StackBlitz][node esm javascript]

### Browser

- [bundling with webpack](https://stackblitz.com/github/schoero/swissqrbill/tree/master/examples/)
- [bundling with webpack][bundling with webpack]

### Node JS

#### Node ESM import

Importing the library in Node.js is straight forward. You can use the following import statement:
Check out a working example on [StackBlitz][node esm javascript] using Chrome.
Importing the library in Node.js using ES modules is straight forward. You can use the following import statement:

```ts
``;
// PDFKit
import PDFDocument from "pdfkit";
// PDF
import { SwissQRBill } from "swissqrbill/pdf";
// SVG
import { SwissQRBill } from "swissqrbill/svg";
```

#### Node CJS import

Check out a working example on [StackBlitz][node cjs javascript] using Chrome.
SwissQRBill provides a CommonJS module for legacy Node.js applications. You can require the library as follows:

```ts
// PDFKit
const PDFDocument = require("pdfkit");
// PDF
const { SwissQRBill } = require("swissqrbill/pdf");
// SVG
const { SwissQRBill } = require("swissqrbill/svg");
```

[node esm javascript]: https://stackblitz.com/github/schoero/swissqrbill/tree/feat/stackblitz-examples/examples/node-esm-javascript
[node cjs javascript]: https://stackblitz.com/github/schoero/swissqrbill/tree/feat/stackblitz-examples/examples/node-cjs-javascript
[bundling with webpack]: https://stackblitz.com/github/schoero/swissqrbill/tree/feat/stackblitz-examples/examples/browser-bundling-with-webpack
2 changes: 0 additions & 2 deletions examples/browser-bundling-with-webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ module.exports = {
},
module: {
rules: [
// bundle and load afm files verbatim
{
test: /\.afm$/,
type: "asset/source"
},
// convert to base64 and include inline file system binary files used by fontkit and linebreak
{
enforce: "post",
loader: "transform-loader",
Expand Down
18 changes: 18 additions & 0 deletions examples/node-cjs-javascript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "commonjs",
"name": "node-cjs-javascript",
"author": "Roger Schönbächler",
"scripts": {
"generate": "npm run pdf && npm run svg",
"pdf": "node pdf.js",
"svg": "node svg.js"
},
"dependencies": {
"pdfkit": "^0.13.0",
"swissqrbill": "^4.0.0-alpha.3"
},
"devDependencies": {
"tsx": "^3.12.10",
"typescript": "^5.2.2"
}
}
11 changes: 11 additions & 0 deletions examples/node-cjs-javascript/src/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
creditor: {
account: "CH58 0079 1123 0008 8901 2",
address: "Creditor Address",
city: "Creditor City",
country: "CH",
name: "Creditor FirstName LastName",
zip: 1234
},
currency: "CHF"
};
15 changes: 15 additions & 0 deletions examples/node-cjs-javascript/src/pdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { createWriteStream } = require("fs");
const PDFDocument = require("pdfkit");
const { SwissQRBill } = require("swissqrbill/pdf");

const data = require("./data");


const stream = createWriteStream("./swissqrbill.pdf");
const pdf = new PDFDocument({ size: "A4" });
const qrBill = new SwissQRBill(data);

pdf.pipe(stream);

qrBill.attachTo(pdf);
pdf.end();
8 changes: 8 additions & 0 deletions examples/node-cjs-javascript/src/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { writeFileSync } = require("fs");
const { SwissQRBill } = require("swissqrbill/svg");

const data = require("./data.js");


const qrBill = new SwissQRBill(data);
writeFileSync("./swissqrbill.svg", qrBill.toString());

0 comments on commit d9334b4

Please sign in to comment.