]> BookStack Code Mirror - website/blob - content/docs/admin/pdf-rendering.md
Updated v25.02 post time
[website] / content / docs / admin / pdf-rendering.md
1 +++
2 title = "PDF Rendering"
3 description = "Options to configure PDF rendering within BookStack"
4 date = "2017-01-22"
5 type = "admin-doc"
6 +++
7
8 By default BookStack uses [dompdf](https://github.com/dompdf/dompdf) to export pages as PDF documents. The benefit of using dompdf is that it doesn't require any additional installation or setup but the rendering capabilities are somewhat limited.
9
10 As an alternative you can instead define a command to make use of external PDF rendering options that may provide more capabilities.
11
12 {{<toc>}}
13
14 ---
15
16 ### Export Page Size
17
18 By default PDF exports are generated at an A4 size. If you'd prefer exports to be generated at "US Letter" standard sizes
19 you can specify this within your `.env` like so:
20
21 ```bash
22 # US Letter
23 EXPORT_PAGE_SIZE=letter
24 ```
25
26 ---
27
28 ### PDF Export Command
29
30 Instead of using the default PHP-based default PDF rendering system, you can define a command for BookStack to use when generating
31 a PDF. This enables flexibility in what program is used to create PDF exports.
32 The command is set via a `EXPORT_PDF_COMMAND` option in your `.env` file, which can use the following placeholders:
33
34 - `{input_html_path}` - Path to a file where BookStack will provide HTML to convert.
35 - `{output_pdf_path}` - Path to a file where the command should output its PDF result.
36
37 Here's an example value for this option using these placeholders:
38
39 ```bash
40 EXPORT_PDF_COMMAND="/scripts/convert.sh {input_html_path} {output_pdf_path}"
41 ```
42
43 Below you can find some examples using this for specific PDF generation options.
44
45 **Timeout**
46
47 By default BookStack will wait for 15 seconds before reaching a timeout, at which point it'd consider the export process as failed, but this can be configured like so:
48
49 ```bash
50 # Allow 30 seconds for the export process instead of the default 15
51 EXPORT_PDF_COMMAND_TIMEOUT=30
52 ```
53
54 **Considerations**
55
56 - Security is a significant concern for this option and process. Input HTML will include user-editable data, and is not assured to be trustworthy & safe. Ideally, any networking or filesystem access would be disabled/prevented during conversion.
57 - BookStack will attempt to embed required images into the HTML data as base64 data URIs to avoid external fetching.
58 - BookStack will embed CSS styling into the HTML data.
59 - Use of this option requires running a process from PHP, which can be considered risky and may potentially be blocked by configuration, environment and/or systems like SELinux by default.
60 - BookStack sets a default timeout of 15 seconds for this command to return, unless altered as shown above.
61 - BookStack will use the return status code of the command as an indicator of failure/success, while also checking that the output PDF was written to.
62
63 #### Example: Weasyprint Command Option
64
65 **Warning:** This is option is not considered secure due to potential filesystem/network access.
66
67 This example uses [weasyprint](https://doc.courtbouillon.org/weasyprint/stable/) to generate PDF exports.
68
69 ```bash
70 EXPORT_PDF_COMMAND="weasyprint {input_html_path} {output_pdf_path}"
71 ```
72
73 #### Example: Weasyprint via Docker
74
75 This example also uses [weasyprint](https://doc.courtbouillon.org/weasyprint/stable/) to generate PDF exports,
76 but via a docker container to help isolate the PDF generation process. This also allows the PDF generation
77 to be performed remotely on other systems if needed since communication with BookStack is performed over HTTP
78 using curl. This uses the [4teamwork/weasyprint](https://github.com/4teamwork/weasyprint-docker) and [caddy](https://caddyserver.com/)
79 docker containers.
80
81 **Considerations:**
82
83 - Curl must be installed on the host system.
84 - This container setup provides extra levels of isolation, but this does not assure fully secure sandboxing.
85 - If using on a different host, you may need to consider limiting access or adding auth to avoid exposing to the general web.
86
87 <details>
88 <summary>Docker Compose Setup</summary>
89
90 Save the below as `docker-compose.yml` then run `docker compose up -d` within the same directory
91 to start up the containers. 
92
93 This sets up the `4teamwork/weasyprint` container on its own isolated network, which is then exposed/proxied
94 via the caddy proxy container on port 3000 to the host.
95
96 ```yml
97 services:
98   weasyprint:
99     image: 4teamwork/weasyprint:latest
100     networks:
101       - weasy-net
102     restart: always
103   proxy:
104     image: caddy:2.9-alpine
105     command: caddy reverse-proxy --from http://127.0.0.1:3000 --to http://weasyprint:8080 
106     ports: 
107       - 127.0.0.1:3000:3000
108     networks: 
109       - weasy-net
110       - proxy
111     restart: always
112
113 networks:
114   weasy-net:
115     driver: ipvlan
116     internal: true
117   proxy:
118     driver: bridge
119     internal: false
120 ```
121
122 </details>
123
124 ```bash
125 # Export to PDF by calling the Weasyprint docker service using curl.
126 # This will likely need altering if running on non-unix systems,
127 # or if running on an external host, or when using a different port.
128 EXPORT_PDF_COMMAND='EXPORT_HTML={input_html_path}; curl -F "html=@$EXPORT_HTML" http://127.0.0.1:3000 -o {output_pdf_path}'
129 ```
130
131 ---
132
133 ### Using wkhtmltopdf
134
135 **Note:** As of BookStack v24.05 this option is considered deprecated, due to the diminishing support of wkhtmltopdf.
136 You can instead use the more flexible [PDF Export Command](#pdf-export-command) option detailed above.
137
138 Pre-compiled binaries for wkhtmltopdf can be found on the downloads page of [their website](http://wkhtmltopdf.org/downloads.html).
139 BookStack will check for a file named `wkhtmltopdf` at the base folder of a BookStack install. If found it will use that to render PDF exports. 
140 If that does not exist it will check for a `WKHTMLTOPDF` variable in the `.env` file. 
141 You can use the below variable in your `.env` file to set an alternate location to wkhtmltopdf:
142
143 ```bash
144 WKHTMLTOPDF=/home/user/bins/wkhtmltopdf
145 ```
146
147 If neither of those exist, or if the below mentioned security option is not enabled, the default dompdf renderer will be used instead.
148
149 **Note:** As of BookStack v21.08 the `ALLOW_UNTRUSTED_SERVER_FETCHING` must also be set to `true` for wkhtmltopdf to be enabled, without this dompdf will be used instead. 
150 This change was made for security since, in many cases, wkhtmltopdf will perform fetches to external URLs which may be defined by users.
151 You should only enable the below option in environments where users & visitors are trusted.
152
153 ```bash
154 ALLOW_UNTRUSTED_SERVER_FETCHING=true
155 ```
156
157 [See our security page for more detail regarding this option](/docs/admin/security/#server-side-requests).