speeddragon

speeddragon

Puppeteer-PDF - Converting HTML to PDF

Hi, a couple of months ago I’ve tested some libraries to convert HTML into PDF, but in the end I’ve decided to create a wrapper for puppeteer-pdf that is a NodeJS binary. Puppeteer is the name of the software that allow to control a Chrome windowless instance, and with puppeteer-pdf it allow to render into a PDF an HTML file.

GitHub project in GitHub - coletiv/puppeteer-pdf: PDF generation wrapper for Elixir using Puppeteer · GitHub
Medium article where I compare to WkHtmlToPdf (the most popular library to generate PDF in Elixir), https://medium.com/coletiv-stories/puppeteer-vs-wkhtmltopdf-and-why-i-created-a-new-module-9466eb1db7d1

Any feedback is welcome.

Most Liked

aseigo

aseigo

Neat :slight_smile:

Some comments:

As noted in the Library guidelines using Application.get_env should be avoided in libraries as it is both cumbersome and easily results in unresolvable conflicts in needs. Consider moving it to the options parameters that is already there.

I suppose this next thing is an application issue, but constructing command line params as it currently is without checking for special/control characters in them is a security hole waiting to open.

It also looks like a small landmine that Briefly cleans up on process exit, but generate/3 does create a process; so calling this repeatedly from a long-lived process will (probably unexpectedly to the user) create a bunch of tempfiles that never get deleted and slowly fill up an ets table in the background. Might be nice to spawn a process for the work in generate.

The function names .. they don’t feel immediately clear as to which does which just from the name and there are no docs (also: write docs and typespecs :slight_smile: .. perhaps PuppeteerPDF.Generate.from_string/3 and PuppeteerPDF.Generate.from_file/3 would be more explicit and clear? And yes, that implied making a PuppeteerPDF.Generate module, but to my eyes it reads a lot clearer as to what is going on…

As for features, would be awesome if you could give it a URL and fetch the HTML locally .. but that’s a significant feature add… that said, one can imagine the name of that function easily with the Generate.from_* pattern …

Hope you don’t mind so many comments / critiques eek it is great to see more modules and libraries in the community and this is a great start to something a lot of people need…

michalmuskala

michalmuskala

It’s actually fine with System.cmd because it does not call the shell. It’s not possible to do classical shell-injection with things like "foo; $(rm -rf /)" - the pupeteer executable will receive it as a whole string. There might be some issues in what it does, in turn, with them, so it’s better to escape anyway, but the direct problem is not actually there.

speeddragon

speeddragon

Hi @ryanzidago, currently this isn’t supported. This library use https://www.npmjs.com/package/puppeteer-pdf to generate the PDF file, and I’ve looked also into pdf_generator library, it doesn’t supported either.

To support what you want, I would go with a custom javascript code using puppeteer example. You can add cookies there, can trigger the render of PDF there. Should be simple.

If you want to contribute, you can probably use the github source and create a PR for both javascript and elixir module, but I think that the project isn’t active (the nodejs one).

Last Post!

auraham

auraham

Is it safe to call the wrapper concurrently? Something like this:

list_of_paths_to_html_files
|> Enum.map(fn path ->
    Task.async(fn ->
       {:ok, html} = File.read(path)
       PuppeteerPdf.Generate.from_string(html, pdf_path, options) 
    end)
end)
|> Enum.map(fn t -> Task.await(t) end)

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
385 14863 120
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
general-CbIC
Hi :waving_hand:t2: Poolex is an Elixir library for managing pools of workers. In short, this is a poolboy written in Elixir. When I s...
New
kip
Following on from my CLDR lbraries I started work on Unicode transforms. But like everything related to CLDR there is a lot of yak-shavin...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement