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 https://github.com/coletiv/puppeteer-pdf
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).

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?

Popular in Libraries Top

tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
pkrawat1
Presenting Aviacommerce, open source e-commerce platform in Elixir Aviacommerce is an open source e-commerce platform in Elixir. We at...
New
mcrumm
If you would like to migrate away from node/npm/webpack while still using sass, the dart_sass package provides a installer and runner for...
New
New
mbuhot
EctoJob A transactional job queue built with Ecto, PostgreSQL and GenStage Available on Hex.pm: ecto_job | Hex Docs: API Reference — ec...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Sub Categories:

We're in Beta

About us Mission Statement