Added weasyprint docker PDF command example

This commit is contained in:
Dan Brown 2025-01-12 12:18:56 +00:00
commit 89b82a5634
Signed by: danb
GPG key ID: 46D9F943C24A2EF9

View file

@ -70,6 +70,64 @@ This example uses [weasyprint](https://doc.courtbouillon.org/weasyprint/stable/)
EXPORT_PDF_COMMAND="weasyprint {input_html_path} {output_pdf_path}"
```
#### Example: Weasyprint via Docker
This example also uses [weasyprint](https://doc.courtbouillon.org/weasyprint/stable/) to generate PDF exports,
but via a docker container to help isolate the PDF generation process. This also allows the PDF generation
to be performed remotely on other systems if needed since communication with BookStack is performed over HTTP
using curl. This uses the [4teamwork/weasyprint](https://github.com/4teamwork/weasyprint-docker) and [caddy](https://caddyserver.com/)
docker containers.
**Considerations:**
- Curl must be installed on the host system.
- This container setup provides extra levels of isolation, but this does not assure fully secure sandboxing.
- If using on a different host, you may need to consider limiting access or adding auth to avoid exposing to the general web.
<details>
<summary>Docker Compose Setup</summary>
Save the below as `docker-compose.yml` then run `docker compose up -d` within the same directory
to start up the containers.
This sets up the `4teamwork/weasyprint` container on its own isolated network, which is then exposed/proxied
via the caddy proxy container on port 3000 to the host.
```yml
services:
weasyprint:
image: 4teamwork/weasyprint:latest
networks:
- weasy-net
restart: always
proxy:
image: caddy:2.9-alpine
command: caddy reverse-proxy --from http://127.0.0.1:3000 --to http://weasyprint:8080
ports:
- 127.0.0.1:3000:3000
networks:
- weasy-net
- proxy
restart: always
networks:
weasy-net:
driver: ipvlan
internal: true
proxy:
driver: bridge
internal: false
```
</details>
```bash
# Export to PDF by calling the Weasyprint docker service using curl.
# This will likely need altering if running on non-unix systems,
# or if running on an external host, or when using a different port.
EXPORT_PDF_COMMAND='EXPORT_HTML={input_html_path}; curl -F "html=@$EXPORT_HTML" http://127.0.0.1:3000 -o {output_pdf_path}'
```
---
### Using wkhtmltopdf