Skip to content

Commit

Permalink
πŸ“ Provide accessible examples
Browse files Browse the repository at this point in the history
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
ralfstx committed Oct 2, 2023
1 parent 6473eb0 commit da09e7b
Show file tree
Hide file tree
Showing 16 changed files with 1,023 additions and 806 deletions.
6 changes: 6 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package-lock.json
yarn.lock
deno.lock
bun.lockb
fonts/
*.pdf
83 changes: 83 additions & 0 deletions examples/README.md
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
24 changes: 24 additions & 0 deletions examples/download-fonts.sh
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
8 changes: 8 additions & 0 deletions examples/package.json
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"
}
}
Loading

0 comments on commit da09e7b

Please sign in to comment.