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

as_raw_html() removes custom font from google_font() #1736

Open
ozanozbeker opened this issue Jun 27, 2024 · 0 comments
Open

as_raw_html() removes custom font from google_font() #1736

ozanozbeker opened this issue Jun 27, 2024 · 0 comments
Assignees

Comments

@ozanozbeker
Copy link

Description

I use as_raw_html() to attach my {gt} tables to {blastula} emails, but I noticed the custom fonts from google_font() gets dropped in the process.

Reproducible example

weekly_email = compose_email(
  header = add_image("resources/combined_logo.png")
  body = blocks(
    
    block_title("weekly Report"),
    
    block_text(html(str_c(
      "<center> Sent on ", add_readable_time(),
      "<br>This email report is also attached as PDF & HTML files!</center>"
    ))),

    block_articles(
      article(content = as_raw_html(gt_marketplace))
    ),

    block_articles(
      article(content = as_raw_html(price_summary)),
      article(content = add_ggplot(plt_price_history, width = 10, height = 4))
    ),
  )
)

The {gt} tables will default to the client default font. I did come up with a work-around for 1 custom font, but it would need to be tweaked for multiple fonts. Here is the custom function:

as_raw_html_with_google_font.R = function(gt, font_name = "Roboto", font_family_css = "Roboto") {
  # Transfrom {gt} to HTML
  html_gt = as_raw_html(gt)

  # Embed Google Font link in the HTML header
  google_font_link = paste0(
    "https://fonts.googleapis.com/css2?family=",
    font_family_css,
    "&display=swap")

  # Define CSS to apply the Google Font to the {gt} table
  css_content = sprintf("
    <style>
      .gt_table {
        font-family: '%s', sans-serif;
      }
    </style>
  ", font_name)

  # Combine the font link and CSS with the HTML
  htmltools::tagList(
    tags$head(
      tags$link(href = google_font_link, rel = "stylesheet"),
      HTML(css_content)
    ),
    HTML(html_gt)
  )
}  
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