This is a wrapper to NodeJS module puppeteer_pdf. After some attempts to use wkhtmltopdf using pdf_generator, I've decided to use other software to generate PDF and create a wrapper for it.
- Bigger PDF file size.
- NodeJS 8+ needed
- Display independent render (for better testing how template will be).
If available in Hex, the package can be installed
by adding puppeteer_pdf
to your list of dependencies in mix.exs
:
def deps do
[
{:puppeteer_pdf, "~> 0.1.1"}
]
end
These are the options available right now:
options = [
margin_left: 40,
margin_right: 40,
margin_top: 40,
margin_bottom: 150,
format: "A4",
print_background: true,
header_template: header_html, # Support both file and html
footer_template: footer_html,
display_header_footer: true,
debug: true
]
And to generate the PDF you can use the following code using Phoenix Template:
# Get template rendered previously
html = Phoenix.View.render_to_string(
MyApp.View,
"pdf/invoice.html",
assigns
)
# Get full path to generated pdf file
pdf_path = Path.absname("invoice.pdf")
case PuppeteerPdf.generate_with_html(html, pdf_path, options) do
{:ok, _} -> ...
{:error, message} -> ...
end
Or just with HTML file:
html_path = Path.absname("random.html")
case PuppeteerPdf.generate_with_html(html_path, pdf_path, options) do
{:ok, _} -> ...
{:error, message} -> ...
end
You can defined an HTML header and footer, using the header_template
and footer_template
options.
To use a file, use the following format: file:///home/user/file.html
.
Don't forget to also include display_header_footer
to true
.
config :puppeteer_pdf, exec_path: "/usr/local/bin/puppeteer-pdf"
If you use CI
before_script:
- nvm install 8
- npm i puppeteer-pdf -g