Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font-awesome icons #11

Open
BenjaminLouis opened this issue Sep 19, 2018 · 2 comments
Open

Font-awesome icons #11

BenjaminLouis opened this issue Sep 19, 2018 · 2 comments

Comments

@BenjaminLouis
Copy link

BenjaminLouis commented Sep 19, 2018

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

By trying this 1) rstudio/rmarkdown#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
Copy link
Owner

RLesur commented Sep 19, 2018

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 FontAwesome 5.1.0 update and fix to flexdashboard rstudio/rmarkdown#1388 fa are now rendered using web fonts.
    I tried this example Feature Request: Font-Awesome Icons in Rmarkdown Docs rstudio/rmarkdown#813 (comment), 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 support @font-face on Windows Kozea/WeasyPrint#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 Add support for inline SVG Kozea/WeasyPrint#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
Copy link
Author

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

BenjaminLouis added a commit to BenjaminLouis/manageR that referenced this issue Sep 25, 2018
Add icon with solution 1 from RLesur/weasydoc#11 :

- work on linux with weasyprint (prince not tested)
- work on windows with Prince but not with weasyprint
BenjaminLouis added a commit to BenjaminLouis/manageR that referenced this issue Sep 25, 2018
add icons with solution 2 from RLesur/weasydoc#11 :

- works on windows with weasyprint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants