-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
872 additions
and
483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"prettier": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"prettier": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
Oops, something went wrong.