| Title: | Generate SVG Information Cards with Embedded Fonts and Badges |
|---|---|
| Description: | Create self-contained SVG information cards with embedded 'Google Fonts', shields-style badges, and custom logos. Cards are fully portable SVG files ideal for dashboards, reports, and web applications. Includes functions to export cards to PNG format and display them in 'R Markdown' and 'Quarto' documents. |
| Authors: | Andre Leite [aut, cre], Hugo Vasconcelos [aut], Diogo Bezerra [aut] |
| Maintainer: | Andre Leite <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.2 |
| Built: | 2026-05-29 09:35:50 UTC |
| Source: | https://github.com/strategicprojects/cardargus |
Converts multiple SVGs to base64-encoded PNG strings using a single Chrome session. Much faster than calling svg_to_png_chrome() repeatedly.
batch_svg_to_base64_chrome( svg_list, dpi = 300, background = "transparent", load_wait = 0.5, restart_every = 50, retry_attempts = 3, progress = TRUE )batch_svg_to_base64_chrome( svg_list, dpi = 300, background = "transparent", load_wait = 0.5, restart_every = 50, retry_attempts = 3, progress = TRUE )
svg_list |
List of SVG strings or file paths. |
dpi |
Resolution (default 300). |
background |
Background color (default "transparent"). |
load_wait |
Seconds to wait for each page to load (default 0.5). Increase if conversions are failing. |
restart_every |
Restart Chrome session every N conversions (default 50). Helps prevent memory issues and stale connections. |
retry_attempts |
Number of retry attempts on failure (default 3). |
progress |
Show progress bar (default TRUE). |
Character vector of base64-encoded PNGs (data URI format). Returns NA for failed conversions.
Convert a list of SVG strings to PNG files in a given directory. Fonts are embedded automatically for consistent rendering.
batch_svg_to_png( svg_list, output_dir = ".", prefix = "card", dpi = 300, background = "transparent" )batch_svg_to_png( svg_list, output_dir = ".", prefix = "card", dpi = 300, background = "transparent" )
svg_list |
List of SVG strings. |
output_dir |
Output directory. |
prefix |
File name prefix. |
dpi |
Resolution. |
background |
Background color for PNG output. |
Character vector of output paths.
Converts multiple SVGs to PNG files using a single Chrome session. Much faster than calling svg_to_png_chrome() repeatedly.
batch_svg_to_png_chrome( svg_list, output_paths = NULL, dpi = 300, background = "transparent", load_wait = 0.5, restart_every = 50, retry_attempts = 3, progress = TRUE )batch_svg_to_png_chrome( svg_list, output_paths = NULL, dpi = 300, background = "transparent", load_wait = 0.5, restart_every = 50, retry_attempts = 3, progress = TRUE )
svg_list |
List of SVG strings or file paths. |
output_paths |
Character vector of output paths. If NULL, temp files are created. |
dpi |
Resolution (default 300). |
background |
Background color (default "transparent"). |
load_wait |
Seconds to wait for each page to load (default 0.5). Increase if conversions are failing. |
restart_every |
Restart Chrome session every N conversions (default 50). |
retry_attempts |
Number of retry attempts on failure (default 3). |
progress |
Show progress bar (default TRUE). |
Character vector of output file paths. Returns NA for failed conversions.
Wrap an SVG card as a raster grob so it can be used with grid graphics.
card_to_grob(svg_string, dpi = 150, engine = c("auto", "chrome", "rsvg"))card_to_grob(svg_string, dpi = 150, engine = c("auto", "chrome", "rsvg"))
svg_string |
SVG string from |
dpi |
Rasterization DPI (default 150). |
engine |
Rendering engine: |
A grid::rasterGrob object.
Checks whether the chromote package can find and use a Chrome or Chromium installation for headless rendering.
chrome_available(verbose = FALSE)chrome_available(verbose = FALSE)
verbose |
Print status messages (default FALSE). |
TRUE if Chrome is available, FALSE otherwise.
if (chrome_available()) { cat("Using Chrome") } else { cat("Using Magick") }if (chrome_available()) { cat("Using Chrome") } else { cat("Using Magick") }
Compress number to abbreviated format
compress_number(x, digits = 1)compress_number(x, digits = 1)
x |
Numeric value to compress |
digits |
Number of decimal places |
Character string with abbreviated number
compress_number(1234567) compress_number(36400000)compress_number(1234567) compress_number(36400000)
Generate a badge similar to shields.io style with label and value. Both sides have properly rounded corners.
create_badge( label, value, color, font = "Jost", style = "margin:2px;", fontsize = 11, horiz_padding = 5, extra_right_pad = 2, class = "", shadow_offset = 2, corner_radius = 3, height = NULL, as_string = TRUE )create_badge( label, value, color, font = "Jost", style = "margin:2px;", fontsize = 11, horiz_padding = 5, extra_right_pad = 2, class = "", shadow_offset = 2, corner_radius = 3, height = NULL, as_string = TRUE )
label |
Label text (left side) |
value |
Value text (right side) |
color |
Background color for value area |
font |
Font family name |
style |
CSS style string |
fontsize |
Font size in pixels |
horiz_padding |
Horizontal padding |
extra_right_pad |
Extra padding on right side |
class |
CSS class |
shadow_offset |
Shadow offset in pixels |
corner_radius |
Corner radius for rounded rectangle |
height |
Minimum height (optional) |
as_string |
Return as character string |
SVG string
create_badge("UH", "192", "white") create_badge("Recurso Federal", "36,4 milhões", "#4CAF50")create_badge("UH", "192", "white") create_badge("Recurso Federal", "36,4 milhões", "#4CAF50")
Generate multiple badges arranged horizontally with the same height.
create_badge_row( badges_data, default_color = "white", spacing = 4, font = "Jost", fontsize = 10, uniform_height = TRUE )create_badge_row( badges_data, default_color = "white", spacing = 4, font = "Jost", fontsize = 10, uniform_height = TRUE )
badges_data |
A list of lists, each containing label, value, and optionally color |
default_color |
Default color for badges |
spacing |
Spacing between badges |
font |
Font family |
fontsize |
Font size |
uniform_height |
Force all badges to have the same height (default TRUE) |
SVG string containing all badges
badges <- list( list(label = "UH", value = "192"), list(label = "Recurso Federal", value = "36,4 milhões"), list(label = "Contrapartida", value = "0,0") ) create_badge_row(badges, default_color = "white")badges <- list( list(label = "UH", value = "192"), list(label = "Recurso Federal", value = "36,4 milhões"), list(label = "Contrapartida", value = "0,0") ) create_badge_row(badges, default_color = "white")
Takes a list of SVG logos and arranges them horizontally for the bottom-left.
create_bottom_logo_row( logos, target_height = 30, spacing = 10, x_offset = 20, card_height = 400, y_offset = 20 )create_bottom_logo_row( logos, target_height = 30, spacing = 10, x_offset = 20, card_height = 400, y_offset = 20 )
logos |
List of SVG strings or file paths |
target_height |
Height for all logos (default 30) |
spacing |
Horizontal spacing between logos (default 10) |
x_offset |
Left margin from card edge |
card_height |
Total card height for y positioning |
y_offset |
Bottom margin |
A list with svg_content and total_width
Takes a list of SVG logos and arranges them horizontally with proper spacing. Returns the SVG elements positioned for the top-right corner.
create_logo_row( logos, target_height = 40, spacing = 10, card_width = 500, x_offset = 20, y_offset = 20 )create_logo_row( logos, target_height = 40, spacing = 10, card_width = 500, x_offset = 20, y_offset = 20 )
logos |
List of SVG strings or file paths |
target_height |
Height for all logos (default 40) |
spacing |
Horizontal spacing between logos (default 10) |
card_width |
Total card width to calculate positioning |
x_offset |
Right margin from card edge |
y_offset |
Top margin |
A list with svg_content and total_width
Checks if Chrome is available and optionally downloads a standalone Chrome for Testing if not found. This ensures Chrome-based rendering works without requiring a system-wide Chrome installation.
ensure_chrome(download = FALSE, verbose = TRUE)ensure_chrome(download = FALSE, verbose = TRUE)
download |
If TRUE and Chrome is not found, attempt to download Chrome for Testing (default FALSE). |
verbose |
Print status messages (default TRUE). |
When download = TRUE, this function will download "Chrome for Testing",
a standalone Chrome distribution designed for automation. The download
is approximately 150MB and is cached in the user's data directory.
Alternatively, you can:
Install Chrome/Chromium system-wide
Set the CHROMOTE_CHROME environment variable to point to an existing installation
TRUE if Chrome is available (or was successfully downloaded), FALSE otherwise.
# Check and report status ensure_chrome() # Download Chrome if not available ## Not run: ensure_chrome(download = TRUE) ## End(Not run)# Check and report status ensure_chrome() # Download Chrome if not available ## Not run: ensure_chrome(download = TRUE) ## End(Not run)
Attempts to find a Chrome or Chromium executable on the system. Checks common installation paths and environment variables.
find_chrome_path()find_chrome_path()
Path to Chrome executable, or NULL if not found.
path <- find_chrome_path() if (!is.null(path)) { message("Chrome found at: ", path) }path <- find_chrome_path() if (!is.null(path)) { message("Chrome found at: ", path) }
Check if a font is available for embedding
font_available(family)font_available(family)
family |
Font family name |
TRUE if font is cached (any supported format), FALSE otherwise
font_available("Jost") font_available("Montserrat")font_available("Jost") font_available("Montserrat")
Returns the directory where cardargus caches downloaded font files. Fonts in this directory are automatically embedded in SVG/PNG exports.
font_cache_dir(persistent = TRUE)font_cache_dir(persistent = TRUE)
persistent |
Logical. If TRUE (default), uses persistent cache via
|
A character path to the cache directory.
Get Google Font CSS for embedding in SVG
get_font_css(font_name = "Jost", weights = c(400, 500, 600, 700))get_font_css(font_name = "Jost", weights = c(400, 500, 600, 700))
font_name |
Name of the Google Font |
weights |
Vector of font weights to include |
Character string with CSS @font-face rules
get_font_css("Jost")get_font_css("Jost")
Returns the full path to a SVG file bundled with the package.
get_svg_path(filename, height = NULL, width = NULL)get_svg_path(filename, height = NULL, width = NULL)
filename |
Name of the SVG file (e.g., "morar_bem.svg") |
height |
Optional target height (px). If provided, returns the SVG content resized for embedding instead of the file path. |
width |
Optional target width (px). Only used when returning resized SVG content. |
If height and width are both NULL, returns the full file path.
Otherwise returns the resized SVG content (character string).
Generate a building/apartment icon SVG.
icon_building( width = 50, height = 56, stroke_color = "white", stroke_width = 2, fill = "none" )icon_building( width = 50, height = 56, stroke_color = "white", stroke_width = 2, fill = "none" )
width |
Width of the icon |
height |
Height of the icon |
stroke_color |
Stroke color |
stroke_width |
Stroke width |
fill |
Fill color |
SVG string
icon_building(50, 56)icon_building(50, 56)
Generate a construction/crane icon SVG.
icon_construction( width = 50, height = 56, stroke_color = "white", stroke_width = 2, fill = "none" )icon_construction( width = 50, height = 56, stroke_color = "white", stroke_width = 2, fill = "none" )
width |
Width of the icon |
height |
Height of the icon |
stroke_color |
Stroke color |
stroke_width |
Stroke width |
fill |
Fill color |
SVG string
icon_construction(50, 56)icon_construction(50, 56)
Generate a house/home icon SVG. You can also use any SVG file path instead of built-in icons.
icon_house( width = 50, height = 56, stroke_color = "white", stroke_width = 35, fill = "none" )icon_house( width = 50, height = 56, stroke_color = "white", stroke_width = 35, fill = "none" )
width |
Width of the icon |
height |
Height of the icon |
stroke_color |
Stroke color |
stroke_width |
Stroke width |
fill |
Fill color (default none) |
SVG string
icon_house(50, 56) # You can also use a custom SVG file: # svg_card(..., with_icon = "/path/to/my_icon.svg")icon_house(50, 56) # You can also use a custom SVG file: # svg_card(..., with_icon = "/path/to/my_icon.svg")
Generate a map pin/location icon SVG.
icon_map_pin( width = 50, height = 56, stroke_color = "white", stroke_width = 2, fill = "none" )icon_map_pin( width = 50, height = 56, stroke_color = "white", stroke_width = 2, fill = "none" )
width |
Width of the icon |
height |
Height of the icon |
stroke_color |
Stroke color |
stroke_width |
Stroke width |
fill |
Fill color |
SVG string
icon_map_pin(50, 56)icon_map_pin(50, 56)
Generate a dollar/money icon SVG.
icon_money( width = 50, height = 56, stroke_color = "white", stroke_width = 2, fill = "none" )icon_money( width = 50, height = 56, stroke_color = "white", stroke_width = 2, fill = "none" )
width |
Width of the icon |
height |
Height of the icon |
stroke_color |
Stroke color |
stroke_width |
Stroke width |
fill |
Fill color |
SVG string
icon_money(50, 56)icon_money(50, 56)
Embeds the SVG as an <img> using a data:image/svg+xml;base64,... URI.
This is more robust than inline <svg> for Pandoc/pkgdown (avoids "unclosed div"
warnings). For non-HTML outputs, it falls back to include_card_png().
include_card( svg_string, width = "100%", alt = "Card generated by cardargus", dpi = 300, engine = c("auto", "chrome", "rsvg") )include_card( svg_string, width = "100%", alt = "Card generated by cardargus", dpi = 300, engine = c("auto", "chrome", "rsvg") )
svg_string |
SVG string from |
width |
Display width (CSS units), e.g. |
alt |
Alternative text for accessibility. |
dpi |
Fallback DPI used when output is not HTML (default 300). |
engine |
Rendering engine for non-HTML output: |
Knitr output for the current format.
Converts an SVG card to PNG and displays it in R Markdown or Quarto documents. Recommended for better compatibility across HTML/PDF/Word.
include_card_png( svg_string, dpi = 300, width = "100%", alt = "Card generated by cardargus", background = "transparent", engine = c("auto", "chrome", "rsvg") )include_card_png( svg_string, dpi = 300, width = "100%", alt = "Card generated by cardargus", background = "transparent", engine = c("auto", "chrome", "rsvg") )
svg_string |
SVG string from |
dpi |
Rasterization DPI (default 300). |
width |
Display width (CSS units) for HTML outputs. |
alt |
Alternative text for accessibility. |
background |
Background color passed to |
engine |
Rendering engine: |
Knitr output for the current format.
Downloads and caches the specified fonts (or default fonts) so they are available for PNG conversion without internet access.
install_fonts( fonts = c("Jost", "Montserrat", "Roboto", "Open Sans"), verbose = TRUE )install_fonts( fonts = c("Jost", "Montserrat", "Roboto", "Open Sans"), verbose = TRUE )
fonts |
Character vector of Google Font names to download. Default is c("Jost", "Montserrat", "Roboto", "Open Sans"). |
verbose |
Print status messages |
Named logical vector indicating success for each font
install_fonts() install_fonts(c("Jost", "Roboto"))install_fonts() install_fonts(c("Jost", "Roboto"))
Check if a color is light
is_light_color(color)is_light_color(color)
color |
A hex color string or color name |
Logical indicating if the color is light
is_light_color("#FFFFFF") is_light_color("#000000")is_light_color("#FFFFFF") is_light_color("#000000")
List available bundled SVG files
list_bundled_svgs()list_bundled_svgs()
Character vector of available SVG filenames
List registered/cached fonts
list_fonts()list_fonts()
Character vector of font family names that are cached
list_fonts()list_fonts()
Reads an SVG file and processes it to be embedded inside another SVG. Removes XML declarations, adjusts dimensions, and prepares for embedding.
load_svg_for_embed(svg_path, target_height = 40, target_width = NULL)load_svg_for_embed(svg_path, target_height = 40, target_width = NULL)
svg_path |
Path to the SVG file |
target_height |
Desired height in pixels |
target_width |
Optional desired width (calculated from aspect ratio if NULL) |
This function is useful when you want to embed custom logos or icons in cards.
You can pass any SVG file path to the logos, bottom_logos, or with_icon
parameters of svg_card().
A list with svg_content, width, and height
## Not run: # Need a external svg file # Load a custom logo logo <- load_svg_for_embed("/path/to/logo.svg", target_height = 40) # Or just pass the path directly to svg_card(): svg_card( title = "My Card", logos = c("/path/to/logo1.svg", "/path/to/logo2.svg"), ... ) ## End(Not run)## Not run: # Need a external svg file # Load a custom logo logo <- load_svg_for_embed("/path/to/logo.svg", target_height = 40) # Or just pass the path directly to svg_card(): svg_card( title = "My Card", logos = c("/path/to/logo1.svg", "/path/to/logo2.svg"), ... ) ## End(Not run)
Registers a custom knitr engine named "cardargus" so that SVG cards can be
rendered directly from chunks.
register_cardargus_knitr()register_cardargus_knitr()
Invisible NULL.
# In your setup chunk: register_cardargus_knitr() # Then use cardargus as chunk engine: # ```{cardargus} # svg_card(title = "My Card", ...) # ```# In your setup chunk: register_cardargus_knitr() # Then use cardargus as chunk engine: # ```{cardargus} # svg_card(title = "My Card", ...) # ```
Copies a local font file (TTF or WOFF2) to the cardargus cache directory so it can be embedded in SVG exports.
register_font(font_path, family = NULL)register_font(font_path, family = NULL)
font_path |
Path to a local .ttf or .woff2 font file |
family |
Font family name to register (e.g., "Jost"). If NULL, the filename without extension is used. |
Path to the cached font file (invisible)
Registers a Google Font using sysfonts.
register_google_font(font_family)register_google_font(font_family)
font_family |
Font family name (e.g., "Jost") |
Invisible NULL
Saves a card to a file and returns the path for use in knitr chunks.
save_card_for_knitr( svg_string, filename = "card", format = c("svg", "png"), dpi = 300, dir = NULL, engine = c("auto", "chrome", "rsvg") )save_card_for_knitr( svg_string, filename = "card", format = c("svg", "png"), dpi = 300, dir = NULL, engine = c("auto", "chrome", "rsvg") )
svg_string |
SVG string from |
filename |
Output filename (without extension). |
format |
Output format: |
dpi |
Rasterization DPI for PNG (default 300). |
dir |
Output directory (defaults to knitr figure directory or tempdir()). |
engine |
Rendering engine for PNG: |
Path to the saved file.
Saves an SVG string to disk. Before saving, the function:
sanitizes the SVG to remove problematic Inkscape/Sodipodi metadata that can break strict XML parsers, and
detects and embeds fonts (WOFF2 via @font-face) for deterministic rendering.
This function expects the font helpers to be available in the package:
detect_svg_fonts(), ensure_cardargus_fonts(), and embed_svg_fonts().
save_svg(svg_content, output_path)save_svg(svg_content, output_path)
svg_content |
SVG string (or object coercible to character). |
output_path |
Output file path. |
Path to the saved SVG file.
svg <- svg_card("FAR", list(), list()) save_svg(svg, tempfile(fileext = ".svg"))svg <- svg_card("FAR", list(), list()) save_svg(svg, tempfile(fileext = ".svg"))
Registers fonts (via sysfonts) and optionally enables showtext auto mode.
setup_fonts(fonts = c("Jost", "Montserrat"), auto = TRUE)setup_fonts(fonts = c("Jost", "Montserrat"), auto = TRUE)
fonts |
Character vector of Google Font names to register. |
auto |
Enable showtext auto mode. |
Invisible NULL
Generate a complete information card as an SVG with embedded styles, fonts, badges, logos, and field labels/values.
svg_card( title = "FAR", badges_data = list(), fields = list(), bg_color = "#fab255", width = 500, padding = 20, corner_radius = 8, font = "Jost", title_fontsize = 16, title_color = "white", label_fontsize = 11, value_fontsize = 11, label_color = "white", value_bg_color = "#f8f8ff", value_text_color = "#212529", show_house_icon = TRUE, logos = list(), logos_height = 40, bottom_logos = list(), bottom_logos_height = 30, footer = NULL, gap_to_footer = 6, footer_row_padding_bottom = 6, footer_fontsize = 8, footer_color = "white", uniform_row_height = TRUE, show_viewer = interactive() )svg_card( title = "FAR", badges_data = list(), fields = list(), bg_color = "#fab255", width = 500, padding = 20, corner_radius = 8, font = "Jost", title_fontsize = 16, title_color = "white", label_fontsize = 11, value_fontsize = 11, label_color = "white", value_bg_color = "#f8f8ff", value_text_color = "#212529", show_house_icon = TRUE, logos = list(), logos_height = 40, bottom_logos = list(), bottom_logos_height = 30, footer = NULL, gap_to_footer = 6, footer_row_padding_bottom = 6, footer_fontsize = 8, footer_color = "white", uniform_row_height = TRUE, show_viewer = interactive() )
title |
Card title (e.g., "FAR", "FNHIS") |
badges_data |
List of badge data (label, value, color) |
fields |
List of field rows, each row is a list of fields with label, value, and optionally with_icon. The with_icon parameter can be:
|
bg_color |
Background color of the card. Can be a solid color (e.g., "#fab255") or a CSS gradient (e.g., "linear-gradient(to right, #1a5a3a, #2e7d32)" or "linear-gradient(135deg, #667eea, #764ba2)"). |
width |
Card width in pixels |
padding |
Padding inside the card |
corner_radius |
Corner radius for rounded corners |
font |
Font family |
title_fontsize |
Title font size |
title_color |
Color for the card title (default "white") |
label_fontsize |
Label font size |
value_fontsize |
Value font size |
label_color |
Color for field labels (default "white") |
value_bg_color |
Background color for value boxes |
value_text_color |
Text color for values |
show_house_icon |
Show house icon next to empreendimento |
logos |
Character vector of logo file paths or SVG strings for top right.
Use |
logos_height |
Height for top-right logos (default 40) |
bottom_logos |
Character vector of logo file paths or SVG strings for bottom left. |
bottom_logos_height |
Height for bottom-left logos (default 30) |
footer |
Footer text (e.g., update timestamp) |
gap_to_footer |
Distance (px) between the last info block and the footer row. |
footer_row_padding_bottom |
Bottom padding (px) under the footer row (text + logos). |
footer_fontsize |
Footer font size |
footer_color |
Color for footer text (default "white") |
uniform_row_height |
If TRUE, keep the height inside a row. |
show_viewer |
If TRUE (and interactive), preview the SVG in the Viewer. |
SVG string
# With default house icon fields <- list( list( list(label = "Empreendimento", value = "CAIARA II", with_icon = TRUE) ) ) # With custom icon custom_icon <- '<svg width="50" height="50"><circle cx="25" cy="25" r="20" fill="white"/></svg>' fields <- list( list( list(label = "Projeto", value = "Meu Projeto", with_icon = custom_icon) ) ) badges <- list( list(label = "UH", value = "192"), list(label = "Recurso Federal", value = "36,4 milhões") ) # With file paths for logos svg_card("FAR", badges, fields, bg_color = "#fab255", logos = c("path/to/logo1.svg", "path/to/logo2.svg"), bottom_logos = c("path/to/gov_logo.svg")) # With gradient background svg_card("MCMV", badges, fields, bg_color = "linear-gradient(to right, #1a5a3a, #2e7d32)") # Diagonal gradient svg_card("Programa", badges, fields, bg_color = "linear-gradient(135deg, #667eea, #764ba2)")# With default house icon fields <- list( list( list(label = "Empreendimento", value = "CAIARA II", with_icon = TRUE) ) ) # With custom icon custom_icon <- '<svg width="50" height="50"><circle cx="25" cy="25" r="20" fill="white"/></svg>' fields <- list( list( list(label = "Projeto", value = "Meu Projeto", with_icon = custom_icon) ) ) badges <- list( list(label = "UH", value = "192"), list(label = "Recurso Federal", value = "36,4 milhões") ) # With file paths for logos svg_card("FAR", badges, fields, bg_color = "#fab255", logos = c("path/to/logo1.svg", "path/to/logo2.svg"), bottom_logos = c("path/to/gov_logo.svg")) # With gradient background svg_card("MCMV", badges, fields, bg_color = "linear-gradient(to right, #1a5a3a, #2e7d32)") # Diagonal gradient svg_card("Programa", badges, fields, bg_color = "linear-gradient(135deg, #667eea, #764ba2)")
Convert an SVG string or file to multiple formats. Supported formats are:
"svg" - saves the SVG
"png" - rasterizes to PNG via svg_to_png
"pdf" - converts to PDF (prefers rsvg)
svg_to_formats( svg_input, output_base, formats = c("svg", "png"), dpi = 300, background = "transparent" )svg_to_formats( svg_input, output_base, formats = c("svg", "png"), dpi = 300, background = "transparent" )
svg_input |
SVG string or file path. |
output_base |
Base name for output files (without extension). |
formats |
Vector of formats to generate ("png", "svg", "pdf"). |
dpi |
Resolution for raster formats. |
background |
Background color for PNG output. |
Named list with paths to generated files.
Renders an SVG to PDF using headless Chrome via the chromote package. This method produces vector PDFs with perfect font rendering.
svg_to_pdf_chrome( svg_input, output_path, background = "transparent", print_background = TRUE, load_wait = 0.5 )svg_to_pdf_chrome( svg_input, output_path, background = "transparent", print_background = TRUE, load_wait = 0.5 )
svg_input |
SVG string or path to an SVG file. |
output_path |
Output path for the PDF file. |
background |
Background color for the HTML page (default "transparent"). |
print_background |
Whether to include CSS backgrounds in PDF (default TRUE). |
load_wait |
Seconds to wait for page to load (default 0.5). |
Path to the generated PDF file.
## Not run: svg <- svg_card("FAR", list(), list()) if (chrome_available()) { pdf_path <- svg_to_pdf_chrome(svg, tempfile(fileext = ".pdf")) } ## End(Not run)## Not run: svg <- svg_card("FAR", list(), list()) if (chrome_available()) { pdf_path <- svg_to_pdf_chrome(svg, tempfile(fileext = ".pdf")) } ## End(Not run)
Convert an SVG string or SVG file path to a high-quality PNG image. The function sanitizes the SVG and embeds required WOFF2 fonts (downloaded on demand into a user cache) to ensure consistent font rendering.
Important note about DPI: rsvg rasterizes primarily based on pixel
dimensions. To make DPI matter, this function scales output pixel size by
(dpi / 96) when width / height are not explicitly provided.
svg_to_png( svg_input, output_path = NULL, width = NULL, height = NULL, dpi = 300, background = "transparent" )svg_to_png( svg_input, output_path = NULL, width = NULL, height = NULL, dpi = 300, background = "transparent" )
svg_input |
SVG string or path to an SVG file. |
output_path |
Output path for the PNG file (optional; a temp file is used if NULL). |
width |
Output width in pixels (NULL to infer from SVG and scale by DPI). |
height |
Output height in pixels (NULL to infer from SVG and scale by DPI). |
dpi |
Resolution in dots per inch (default 300 for high quality). |
background |
Background color. Use "transparent" or "none" for transparency (default), or specify a color like "white", "#FFFFFF", etc. |
Path to the generated PNG file.
svg <- svg_card("FAR", list(), list()) file_name <- tempfile(fileext = ".png") png_path <- svg_to_png(svg, file_name, dpi = 300) png_path <- svg_to_png(svg, file_name, dpi = 300, background = "white")svg <- svg_card("FAR", list(), list()) file_name <- tempfile(fileext = ".png") png_path <- svg_to_png(svg, file_name, dpi = 300) png_path <- svg_to_png(svg, file_name, dpi = 300, background = "white")
Renders an SVG to PNG using headless Chrome via the chromote package. This method provides superior font rendering compared to librsvg/ImageMagick, as Chrome properly handles @font-face rules, web fonts, and CSS features.
svg_to_png_chrome( svg_input, output_path = NULL, dpi = 300, background = "transparent", load_wait = 0.5 )svg_to_png_chrome( svg_input, output_path = NULL, dpi = 300, background = "transparent", load_wait = 0.5 )
svg_input |
SVG string or path to an SVG file. |
output_path |
Output path for the PNG file. If NULL, a temp file is used. |
dpi |
Resolution in dots per inch (default 300). Chrome uses 96 DPI as base, so dpi = 300 results in approximately 3.125x scaling. |
background |
Background color for the HTML page (default "transparent"). Use "white", "#FFFFFF", etc. for a solid background. |
load_wait |
Seconds to wait for page to load (default 0.5). Increase if fonts are not rendering correctly. |
Path to the generated PNG file.
svg <- svg_card("FAR", list(), list()) file_name <- tempfile(fileext = ".png") ## Not run: if (chrome_available()) { png_path <- svg_to_png_chrome(svg, file_name, dpi = 300) } ## End(Not run)svg <- svg_card("FAR", list(), list()) file_name <- tempfile(fileext = ".png") ## Not run: if (chrome_available()) { png_path <- svg_to_png_chrome(svg, file_name, dpi = 300) } ## End(Not run)
Creates a PNG with extra margin around the card. Fonts are embedded before rasterization for consistent appearance.
svg_to_png_with_margin( svg_input, output_path = NULL, margin = 20, margin_color = "transparent", dpi = 300, background = "transparent" )svg_to_png_with_margin( svg_input, output_path = NULL, margin = 20, margin_color = "transparent", dpi = 300, background = "transparent" )
svg_input |
SVG string or path to SVG file. |
output_path |
Output path for PNG file (optional; a temp file is used if NULL). |
margin |
Margin in pixels to add around the card. |
margin_color |
Color of the margin area (default transparent). |
dpi |
Resolution in dots per inch. |
background |
Background color for the card rasterization (default transparent). |
Path to the generated PNG file.
Returns the path to the inst/svgs directory where SVG files are stored.
svgs_dir()svgs_dir()
Character string with the path to SVGs directory