Skip to content

dailydrip/puppeteer-pdf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PuppeteerPdf

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.

Puppeteer PDF vs wkhtmltopdf

Disadvantage

  • Bigger PDF file size.
  • NodeJS 8+ needed

Advantages

  • Display independent render (for better testing how template will be).

Installation

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

Use

Initial

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

Using header and footer templates

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.

Configure execution path

config :puppeteer_pdf, exec_path: "/usr/local/bin/puppeteer-pdf"

Continuous Integration

If you use CI

before_script:
- nvm install 8
- npm i puppeteer-pdf -g

About

PDF generation wrapper for Elixir using Puppeteer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Elixir 100.0%