3
3
A wrapper for both wkhtmltopdf and chrome-headless plus PDFTK (adds in
4
4
encryption) for use in Elixir projects.
5
5
6
- ``` Elixir
7
- {:ok , pdf} = PdfGenerator .generate_binary (" <html><body><h1>Yay!</h1></body></html>" )
8
- ```
6
+ # Latest release v0.6.0 on 2019-12-17
9
7
10
- # Latest release v0.5.5 – v0.5.8 on 2019-12-17
11
-
12
- - 0.5.8
8
+ - 0.6.0
9
+ - introducting ` make ` as build tool (optional) for chromium binaries
10
+ (puppeteer)
13
11
- ** BUGFIX:** documentation: option ` pagesize ` requires string argument
14
12
(for example ` "letter" ` or ` "A4" ` )
15
13
- updated some npm dependencies for chromium
16
- - 0.5.7
17
- - ** BUGFIX:** fix chrome-option parameter handling
18
- - 0.5.6
19
- - ** BUGFIX:** fix A4 and A5 paper sizes in inches for ** chrome-headless** :
20
- it's not 8.5 x 11.0 (US letter) but 8.26772 x 11.695 (DIN A4), the former
21
- being chrome-headless defaults. This is important if you want to create
22
- proper A4 pages
23
- - Users printing ** US letter** sized PDFs, please use ` page_size: "letter" `
24
- - 0.5.5
25
- - improved documentation on ` prefer_system_executable: true ` for chrome.
26
- Thanks to [ Martin Richer] ( https://github.com/richeterre ) for rasining this
27
- and a [ PR] ( https://github.com/gutschilla/elixir-pdf-generator/pull/55 )
28
- - improved documentation on ` no_sandbox: true ` for chrome in dockerized
29
- environment (running as root)
30
- - clarify that wkhtmltopdf installation snippet is for Ubuntu 18.04.
31
- - log call options as debug info to Logger
32
- - add "knows issues" section to README
33
14
34
15
For a proper changelog, see [ CHANGES] ( CHANGES.md )
35
16
36
- # System prerequisites
37
-
38
- It's either
39
-
40
- * wkhtmltopdf or
41
-
42
- * nodejs and possibly chrome/chromium
43
-
44
- ## chrome-headless
45
-
46
- This will allow you to make more use of Javascript and advanced CSS as it's just
47
- your Chrome/Chromium browser rendering your web page as HTML and printing it as
48
- PDF. Rendering _ tend_ to be a bit faster than with wkhtmltopdf. The price tag is
49
- that PDFs printed with chrome/chromium are usually considerably bigger than
50
- those generated with wkhtmltopdf.
51
-
52
- 1 . Run ` npm -g install chrome-headless-render-pdf puppeteer ` .
53
-
54
- This requires [ nodejs] ( https://nodejs.org ) , of course. This will install a
55
- recent chromium and chromedriver to run Chrome in headless mode and use this
56
- browser and its API to print PDFs globally on your machine.
57
-
58
- If you prefer a project-local install, just use ` npm install ` This will
59
- install dependencies under ` ./node_modules ` . Be aware that those won't be
60
- packaged in your distribution (I will add support for this later).
61
-
62
- On some machines, this doesn't install Chromium and fails. Here's how to get
63
- this running on Ubuntu 18:
64
-
65
- ```
66
- DEBIAN_FRONTEND=noninteractive PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=TRUE \
67
- apt-get install -y chromium-chromedriver \
68
- && npm -g install chrome-headless-render-pdf puppeteer
69
- ```
70
-
71
- ## wkhtmltopdf
72
-
73
- 2 . Download wkhtmltopdf and place it in your $PATH. Current binaries can be
74
- found here: http://wkhtmltopdf.org/downloads.html
75
-
76
- For the impatient (Ubuntu 18.04 Bionic Beaver):
77
-
78
- ```
79
- apt-get -y install xfonts-base xfonts-75dpi \
80
- && wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb \
81
- && dpkg -i wkhtmltox_0.12.5-1.bionic_amd64.deb
82
- ```
83
-
84
- For other distributions, refer to http://wkhtmltopdf.org/downloads.html – For
85
- example, replace ` bionic ` with ` xenial ` if you're on Ubuntu 16.04.
86
-
87
- ## optional dependencies
88
-
89
- 3 . _ optional:_ Install ` xvfb ` (shouldn't be required with the binary mentioned above):
90
-
91
- To use other wkhtmltopdf executables comiled with an unpatched Qt on systems
92
- without an X window server installed, please install ` xvfb-run ` from your
93
- repository (on Debian/Ubuntu: ` sudo apt-get install xvfb ` ).
94
-
95
- I haven't heard any feedback of people using this feature since a while since
96
- the wkhtmltopdf projects ships ready-made binaries. I will deprecate this
97
- starting in ` 0.6.0 ` since, well, YAGNI.
98
-
99
- 4 . _ optional:_ Install ` pdftk ` via your package manager or homebrew. The project
100
- page also contains a Windows installer. On Debian/Ubuntu just type:
101
- ` apt-get -y install pdftk `
102
-
103
17
# Usage
104
18
105
19
_ Hint:_ In IEX, ` h PdfGenerator.generate ` is your friend.
@@ -110,18 +24,44 @@ Add this to your dependencies in your mix.exs:
110
24
def application do
111
25
[applications: [
112
26
:logger ,
113
- :pdf_generator # <-- add this
27
+ :pdf_generator # <-- add this for Elixir <= 1.4
114
28
]]
115
29
end
116
30
117
31
defp deps do
118
32
[
119
33
# ... whatever else
120
- { :pdf_generator , " >=0.5.5 " }, # <-- and this
34
+ { :pdf_generator , " >=0.6.0 " }, # <-- and this
121
35
]
122
36
end
123
37
```
124
38
39
+ If you want to use a locally-installed chromium in ** RELEASES** (think `mix
40
+ release`), alter your mixfile to let make take care of comilation and
41
+ dependency-fetching:
42
+
43
+ ``` Elixir
44
+ defp deps do
45
+ [
46
+ # ... whatever else
47
+ { :pdf_generator , " >=0.6.0" , compile: " make chrome" }
48
+ ]
49
+ end
50
+ ```
51
+
52
+ This will embed a ** 300 MB** (yes, that large) Chromium binary into your priv folder
53
+ which will survive packaging as Erlang release. This _ can_ be handy as this will
54
+ run on slim Alpine docker images with just NodeJS installed.
55
+
56
+ The recommended way still is to install Chromium/Puppeteer globally and set the
57
+ ` prefer_system_executable: true ` option when generating PDFs.
58
+
59
+ In development: While this usually works, it unfortunately leads to
60
+ pdf_generator to be compiled all the time again and again due to my bad Makefile
61
+ skills. Help is very much appreciated.
62
+
63
+ ## Try it out
64
+
125
65
Then pass some html to PdfGenerator.generate
126
66
127
67
``` Elixir
@@ -167,6 +107,82 @@ filename = PdfGenerator.generate! "<html>..."
167
107
pdf_binary = PdfGenerator .generate_binary! " <html>..."
168
108
```
169
109
110
+ # System prerequisites
111
+
112
+ It's either
113
+
114
+ * wkhtmltopdf or
115
+
116
+ * nodejs (for chromium/puppeteer)
117
+
118
+ ## chrome-headless
119
+
120
+ This will allow you to make more use of Javascript and advanced CSS as it's just
121
+ your Chrome/Chromium browser rendering your web page as HTML and printing it as
122
+ PDF. Rendering _ tend_ to be a bit faster than with wkhtmltopdf. The price tag is
123
+ that PDFs printed with chrome/chromium are usually considerably bigger than
124
+ those generated with wkhtmltopdf.
125
+
126
+ ### global install (great for Docker images)
127
+
128
+ Run ` npm -g install chrome-headless-render-pdf puppeteer ` .
129
+
130
+ This requires [ nodejs] ( https://nodejs.org ) , of course. This will install a
131
+ recent chromium and chromedriver to run Chrome in headless mode and use this
132
+ browser and its API to print PDFs globally on your machine.
133
+
134
+ If you prefer a project-local install, just use ` npm install ` This will install
135
+ dependencies under ` ./node_modules ` . Be aware that those won't be packaged in
136
+ your distribution (I will add support for this later).
137
+
138
+ On some machines, this doesn't install Chromium and fails. Here's how to get
139
+ this running on Ubuntu 18:
140
+
141
+ ``` bash
142
+ DEBIAN_FRONTEND=noninteractive PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=TRUE \
143
+ apt-get install -y chromium-chromedriver \
144
+ && npm -g install chrome-headless-render-pdf puppeteer
145
+ ```
146
+
147
+ ### local install
148
+
149
+ Run ` make priv/node_modules ` . This requires both ` nodejs ` (insallation see
150
+ above) and ` make ` .
151
+
152
+ Or, run ` cd priv && npm install `
153
+
154
+ ## wkhtmltopdf
155
+
156
+ 2 . Download wkhtmltopdf and place it in your $PATH. Current binaries can be
157
+ found here: http://wkhtmltopdf.org/downloads.html
158
+
159
+ For the impatient (Ubuntu 18.04 Bionic Beaver):
160
+
161
+ ```
162
+ apt-get -y install xfonts-base xfonts-75dpi \
163
+ && wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb \
164
+ && dpkg -i wkhtmltox_0.12.5-1.bionic_amd64.deb
165
+ ```
166
+
167
+ For other distributions, refer to http://wkhtmltopdf.org/downloads.html – For
168
+ example, replace ` bionic ` with ` xenial ` if you're on Ubuntu 16.04.
169
+
170
+ ## optional dependencies
171
+
172
+ 3 . _ optional:_ Install ` xvfb ` (shouldn't be required with the binary mentioned above):
173
+
174
+ To use other wkhtmltopdf executables comiled with an unpatched Qt on systems
175
+ without an X window server installed, please install ` xvfb-run ` from your
176
+ repository (on Debian/Ubuntu: ` sudo apt-get install xvfb ` ).
177
+
178
+ I haven't heard any feedback of people using this feature since a while since
179
+ the wkhtmltopdf projects ships ready-made binaries. I will deprecate this
180
+ starting in ` 0.6.0 ` since, well, YAGNI.
181
+
182
+ 4 . _ optional:_ Install ` pdftk ` via your package manager or homebrew. The project
183
+ page also contains a Windows installer. On Debian/Ubuntu just type:
184
+ ` apt-get -y install pdftk `
185
+
170
186
# Options and Configuration
171
187
172
188
This module will automatically try to finde both ` wkhtmltopdf ` and ` pdftk ` in
0 commit comments