How to create printable web page by rendering text on svg image with Phoenix?

I do not know if my question makes sense, but I will try to develop it.

Let’s say I have a classic Phoenix web application and want to add some cards printing feature. This is will be done by retrieving some data from the database and rendering them precisely on an existing svg image. Then user can print the resulted page.

This is really my first time for such a feature, not only with Elixir but all languages included. So I have really no idea where to start from.

Please, which tools, libraries, resources should I use for this?

Tutorials or open source projects also will be greatly welcome.

Thanks

I have done this previously with PDF generator to build printable receipt, with background image… but it was in Ruby, with prawn.

I guess I would use pdf_generator w/ Elixir, but I have not done this yet.

For simple card printing, You could use a special stylesheet for printing purpose and include it like this.

<link rel="stylesheet" type="text/css" href="print.css" media="print">

This allows to define what, and what not to print.

2 Likes

Thanks I will try the pdf_generator and make feedback later.

Probably the easiest way but I think the pdf solution is more robust.

Note that prawn is very lightweight as it directly writes raw PDFs (you can think of PDF as a textual format). On the other hand pdf_generator uses wkhtmltopdf which might be quite heavy. There are some libraries similar to prawn for Elixir/Erlang (one even started by Joe Armstrong), but none of them have support for proper custom fonts (it’s a hard task) so that was a showstopper for me to do PDF generation in Elixir.

2 Likes

Thanks for the warning.

Anyway, due to the short time I have to implement this feature, I just reviewed my decision and I will go with simple HTML content that will be print by the user.

I found print.js, a javascript library to help printing from the web. It seems great for the job. Kind like @kokolegorille 's suggestion based on a printing stylesheet, I can specify the part of the page to be printed and pass as well the url of the css file to apply.

As a side note this is maybe one more reason for me to learn Rails as a spare wheel. ^^

Imho the best way currently to create a pdf from html in elixir is rambo+weasyprint. I had many problems with wkhtmltopdf and more complex options. Weasyprint on the other hand does what it suggests and handles css quite well.

4 Likes

Oh, I found a post with another solution.

Why not run Ruby code in Elixir? Why not run prawn in Elixir?

This post seems to be a nice alternative and describe how to use prawn with Elixir :slight_smile:

UPDATE:

I changed dependencies to be the most recent…

      {:export, "~> 0.1.1"},
      {:erlport, "~> 0.10.1"},

and now I can generate a pdf in Elixir, using Ruby Prawn library.

2 Likes

As a summary for people who come across this topic: