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 d07b30a commit 832bc09
Show file tree
Hide file tree
Showing 23 changed files with 872 additions and 483 deletions.
23 changes: 23 additions & 0 deletions docs/importing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

# Importing the library

Depending how you intend to use the library, there are different ways to import it. While it is straight forward to import SwissQRBill, it may be a bit more complicated to import PDFKit, which is used to create the PDF itself.

## Table of contents

### Node.js

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

### Browser

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

### Node JS

#### Node ESM import

Importing the library in Node.js is straight forward. You can use the following import statement:

```ts
``;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions examples/browser-bundling-with-webpack/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prettier": false
}
7 changes: 7 additions & 0 deletions examples/browser-bundling-with-webpack/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="module" src="./lib/pdf.js"></script>
</head>
<body></body>
</html>
29 changes: 29 additions & 0 deletions examples/browser-bundling-with-webpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "browser-bundling-with-webpack",
"author": "Roger Schönbächler",
"scripts": {
"build": "webpack",
"start": "webpack serve --mode development"
},
"dependencies": {
"blob-stream": "^0.1.3",
"pdfkit": "^0.13.0",
"swissqrbill": "alpha"
},
"devDependencies": {
"assert": "^2.0.0",
"brfs": "^2.0.2",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"eslint": "^8.38.0",
"process": "^0.11.10",
"readable-stream": "^3.6.2",
"transform-loader": "^0.2.4",
"ts-loader": "^9.4.2",
"typescript": "^5.0.4",
"util": "^0.12.5",
"webpack": "^5.78.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.13.2"
}
}
37 changes: 37 additions & 0 deletions examples/browser-bundling-with-webpack/src/pdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import fs from "node:fs";

import BlobStream from "blob-stream";
import PDFDocument from "pdfkit";
import Helvetica from "pdfkit/js/data/Helvetica.afm";
import HelveticaBold from "pdfkit/js/data/Helvetica-Bold.afm";
import { SwissQRBill } from "swissqrbill/pdf";


fs.writeFileSync("data/Helvetica.afm", Helvetica);
fs.writeFileSync("data/Helvetica-Bold.afm", HelveticaBold);

const data = {
creditor: {
account: "CH58 0079 1123 0008 8901 2",
address: "Creditor Address",
city: "Creditor City",
country: "CH",
name: "Creditor FirstName LastName",
zip: 1234
},
currency: "CHF"
};

const stream = new BlobStream();
const pdf = new PDFDocument();
const qrBill = new SwissQRBill(data);

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

stream.on("finish", () => {
window.location.href = stream.toBlobURL("application/pdf");
console.log("PDF has been successfully created.");
});

pdf.end();
77 changes: 77 additions & 0 deletions examples/browser-bundling-with-webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const webpack = require("webpack");


module.exports = {
devServer: {
devMiddleware: {
publicPath: "/lib/"
},
port: 80,
static: {
directory: "./"
}
},
devtool: "inline-source-map",
entry: {
pdf: "./src/pdf.js"
},
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",
options: {
brfs: {}
},
test: /fontkit[/\\]index.js$/
},
{
enforce: "post",
loader: "transform-loader",
options: {
brfs: {}
},
test: /linebreak[/\\]src[/\\]linebreaker.js/
}
]
},
output: {
filename: "[name].js",
library: "SwissQRBill",
libraryTarget: "umd",
path: `${__dirname}/lib/`
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
process: "process/browser"
}),
new webpack.NormalModuleReplacementPlugin(new RegExp(/\.js$/), resource => {
if(resource.context.includes("node_modules") !== true){
resource.request = resource.request.replace(".js", "");
}
})
],
resolve: {
alias: {
"fs": "pdfkit/js/virtual-fs.js",
"iconv-lite": false
},
extensions: [".tsx", ".ts", ".js"],
fallback: {
assert: require.resolve("assert/"),
buffer: require.resolve("buffer"),
crypto: false,
stream: require.resolve("readable-stream"),
util: require.resolve("util"),
zlib: require.resolve("browserify-zlib")
}
},
target: "web"
};
3 changes: 3 additions & 0 deletions examples/node-esm-javascript/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prettier": false
}
17 changes: 17 additions & 0 deletions examples/node-esm-javascript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "module",
"name": "node-esm-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": "alpha"
},
"devDependencies": {
"typescript": "^5.2.2"
}
}
11 changes: 11 additions & 0 deletions examples/node-esm-javascript/src/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const data = {
creditor: {
account: "CH58 0079 1123 0008 8901 2",
address: "Creditor Address",
city: "Creditor City",
country: "CH",
name: "Creditor FirstName LastName",
zip: 1234
},
currency: "CHF"
};
14 changes: 14 additions & 0 deletions examples/node-esm-javascript/src/pdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SwissQRBill } from "swissqrbill/pdf"
import PDFDocument from "pdfkit";
import { createWriteStream } from "node:fs";
import { data } from "./data.js";

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

pdf.pipe(stream);

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

6 changes: 6 additions & 0 deletions examples/node-esm-javascript/src/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { SwissQRBill } from "swissqrbill/svg"
import { writeFileSync } from "node:fs";
import { data } from "./data.js";

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

0 comments on commit 832bc09

Please sign in to comment.