-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The example folder contained only a single example named `sample.js`. This file was used to check certain conditions during development, but was not comprehensible to users. To make the examples more accessible, this commit replaces this file with multiple individual examples for different topics. A README file is added to the examples folder to explain how to run the examples. A script is provided to download the required fonts. This is clearer than extracting TTF fonts from WOFF files in npm packages. It is also closer to an actual usage of the library. The `npm start` magic is removed. All runtime environments support a `--watch` flag that can be used to re-create the PDF files when the source files change.
- Loading branch information
Showing
16 changed files
with
1,023 additions
and
806 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,6 @@ | ||
package-lock.json | ||
yarn.lock | ||
deno.lock | ||
bun.lockb | ||
fonts/ | ||
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,83 @@ | ||
# Running the examples | ||
|
||
## Prerequisites | ||
|
||
- A recent version of [Node], [Bun], or [Deno]. | ||
|
||
## Download fonts | ||
|
||
Fonts are not included in the repository, but can be downloaded using | ||
the `download-fonts.sh` script. This places the downloaded fonts in the | ||
`fonts` directory. | ||
|
||
```sh | ||
$ ./download-fonts.sh | ||
``` | ||
|
||
## Install dependencies | ||
|
||
In the examples directory, use your favorite installer to install the | ||
`pdfmkr` dependency. | ||
|
||
```sh | ||
$ cd examples | ||
$ npm install | ||
# OR | ||
$ yarn install | ||
# OR | ||
$ bun install | ||
``` | ||
|
||
If you're using [Deno], you can skip this step. | ||
|
||
## Run the examples | ||
|
||
### Node | ||
|
||
Node.js does not yet expose the [WebCrypto API] in the global scope. | ||
Since PDF Maker uses this API, the `--experimental-global-webcrypto` | ||
flag must be used: | ||
|
||
```sh | ||
$ node --experimental-global-webcrypto src/hello-world.js | ||
``` | ||
|
||
Alternatively, you can expose the API in the global scope yourself: | ||
|
||
```js | ||
import * as crypto from 'crypto'; | ||
global.crypto ??= crypto; | ||
``` | ||
Also note that Node.js still relies on the `module: true` flag in the | ||
`package.json` file to enable ESM mode. | ||
### Bun | ||
[Bun] works out of the box: | ||
```sh | ||
$ bun run src/hello-world.js | ||
``` | ||
### Deno | ||
[Deno] works out of the box: | ||
```sh | ||
$ deno run --allow-read --allow-write src/hello-world.js | ||
``` | ||
### Pro-tip: watch mode | ||
While experimenting with the examples, you can use the `--watch` flag to | ||
automatically re-create a PDF when the file changes, e.g.: | ||
```sh | ||
$ bun run --watch src/hello-world.js | ||
``` | ||
[Node]: https://nodejs.org/en/ | ||
[Bun]: https://bun.sh/ | ||
[Deno]: https://deno.land/ | ||
[WebCrypto API]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API |
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,24 @@ | ||
#!/bin/sh | ||
# Downloads DejaVu Sans Condensed fonts to fonts/ folder | ||
# Usage: ./download-fonts.sh | ||
|
||
cd "$(dirname "$0")" | ||
|
||
# Create a fonts folder | ||
mkdir -p fonts | ||
cd fonts/ | ||
|
||
# Download the zip file | ||
wget https://github.com/dejavu-fonts/dejavu-fonts/releases/download/version_2_37/dejavu-fonts-ttf-2.37.zip | ||
|
||
# Extract the zip file | ||
unzip dejavu-fonts-ttf-2.37.zip | ||
|
||
# Move the ttf files to the fonts folder | ||
mv dejavu-fonts-ttf-2.37/ttf/DejaVuSansCondensed.ttf ./ | ||
mv dejavu-fonts-ttf-2.37/ttf/DejaVuSansCondensed-Bold.ttf ./ | ||
mv dejavu-fonts-ttf-2.37/ttf/DejaVuSansCondensed-Oblique.ttf ./ | ||
mv dejavu-fonts-ttf-2.37/ttf/DejaVuSansCondensed-BoldOblique.ttf ./ | ||
|
||
# Remove the zip file and the extracted folder | ||
rm -rf dejavu-fonts-ttf-2.37.zip dejavu-fonts-ttf-2.37/ |
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,8 @@ | ||
{ | ||
"name": "example", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"pdfmkr": "^0.5.3" | ||
} | ||
} |
Oops, something went wrong.