RLesur / weasydoc

Convert R Markdown to PDF Using Weasyprint (or Prince XML)
GNU General Public License v3.0
45 stars 3 forks source link

Font-awesome icons #11

Open BenjaminLouis opened 5 years ago

BenjaminLouis commented 5 years ago

Do you think it could be possible to have access to font-awesome icons ?

By trying this 1) https://github.com/rstudio/rmarkdown/issues/813 or this https://github.com/rstudio/fontawesome, I was able to add icons in the html document but they are absent in the pdf document with the weasyprint engine.

With Prince, only the 2) option works!

Maybe it's an issue for HTML to PDF converters rather than an issue for your package...

RLesur commented 5 years ago

Hi Benjamin,

I tried different things, it's tricky. I hope I will be clear.

1) Using rmarkdown::html_dependency_font_awesome() You need the development version of rmarkdown because since this PR https://github.com/rstudio/rmarkdown/pull/1388 fa are now rendered using web fonts. I tried this example https://github.com/rstudio/rmarkdown/issues/813#issuecomment-260742550, and I succeeded with both WeasyPrint and Prince on linux. On Windows, I think Prince will be OK (not tested). WeasyPrint added very recently the @font-face support on Windows (version 43, see https://github.com/Kozea/WeasyPrint/pull/592), not tested.

2) The fontawesome package 2.1. Inline SVG The fontawesome::fa() function uses inline svg. Prince supports inline svg on both linux and windows. WeasyPrint does not support inline svg, see https://github.com/Kozea/WeasyPrint/issues/75. However, for WeasyPrint there is a hacky way that should work on both linux and windows (works on linux, not tested on windows). Here is an example with the r-project fa icon. Put the following code in a chunk:

fa_r <- fontawesome::fa("r-project")
fa_r_raw <- charToRaw(fa_r)
fa_r_enc <- paste0("data:image/svg+xml;charset=utf-8;base64,", 
                   base64enc::base64encode(fa_r_raw))

Then, in your Rmd file, you can write:

R project icon : <img alt="R project" src="`r fa_r_enc`">

2.2. fa as png The fontawesome::fa_png() function writes png fa icon to a file. So, you can use something like this in a chunk:

tmpfile <- tempfile(fileext = ".png")
fontawesome::fa_png("r-project", tmpfile)
knitr::include_graphics(tmpfile)

It should work for both WeasyPrint and Prince on linux and windows.

BenjaminLouis commented 5 years ago

Hi Romain,

Thanks for the detailed solutions:

On linux with weasyprint, I didn't succeed with 2. and 3. The former inserted a placeholder for the image in the pdf doc but the image wasn't displayed. The latter wasn't able to find the image in my system, even after specifying explicitly a path (i.e. without tempfile).

The solution 1 works with me. I didn't try on windows but I will soon.

You'll find some reprex here : https://github.com/BenjaminLouis/reprex-weasydoc/tree/master/issue%2311