cardargus provides two rendering engines for converting SVG to PNG/PDF:
| Engine | Package | Fonts | Best For |
|---|---|---|---|
| rsvg | rsvg, magick |
Embedded only | Speed, no Chrome dependency |
| Chrome | chromote |
Web fonts + embedded | Perfect font rendering |
The simplest way to save a card:
Uses librsvg/ImageMagick. Fast and reliable, but requires fonts to be embedded in the SVG:
Uses headless Chrome via the chromote package. Provides
perfect font rendering, including Google Fonts loaded
via @import:
# Check if Chrome is available
if (chrome_available()) {
# High-quality PNG with Chrome
svg_to_png_chrome(card, "my_card.png", dpi = 300)
}
# Convert to PDF (vector output)
svg_to_pdf_chrome(card, "my_card.pdf")# Transparent background (preserves rounded corners)
svg_to_png(card, "card_transparent.png", dpi = 300)
# White background
svg_to_png(card, "card_white.png", dpi = 300, background = "white")
# Chrome also supports backgrounds
svg_to_png_chrome(card, "card_white.png", dpi = 300, background = "white")Export to multiple formats at once:
Convert multiple cards efficiently:
# Create multiple cards
cards <- list(
svg_card(title = "Card 1", badges_data = list(), fields = list()),
svg_card(title = "Card 2", badges_data = list(), fields = list()),
svg_card(title = "Card 3", badges_data = list(), fields = list())
)
# Batch convert to PNG
batch_svg_to_png(
svg_list = cards,
output_dir = "output/cards",
prefix = "card",
dpi = 300,
background = "white"
)
# Creates: output/cards/card_001.png, card_002.png, card_003.pngAll knitr functions support an engine parameter:
"auto" (default): Uses Chrome if available, otherwise
rsvg"chrome": Forces Chrome (errors if unavailable)"rsvg": Forces rsvg/magickConvert cards to grobs for use with grid graphics:
| Use Case | DPI | Notes |
|---|---|---|
| Screen/web | 72-96 | Smaller file size |
| Standard print | 150 | Good balance |
| High-quality print | 300 | Publication quality |
| Large format | 300+ | Posters, banners |
Chrome uses 96 DPI as its base resolution. When you set
dpi = 300, cardargus calculates a scale factor:
scale = dpi / 96 = 300 / 96 ≈ 3.125x
This means a 400×300 SVG becomes a 1250×938 PNG at 300 DPI.
# Safe conversion with fallback
convert_card <- function(card, output_path, dpi = 300) {
if (chrome_available()) {
svg_to_png_chrome(card, output_path, dpi = dpi)
} else {
message("Chrome not available, using rsvg")
svg_to_png(card, output_path, dpi = dpi)
}
}
# With explicit error handling
tryCatch({
svg_to_png_chrome(card, "output.png", dpi = 300)
message("Conversion successful!")
}, error = function(e) {
message("Chrome conversion failed: ", e$message)
message("Falling back to rsvg...")
svg_to_png(card, "output.png", dpi = 300)
})This is expected behavior. librsvg cannot:
@import url(...)Solution: Use svg_to_png_chrome() or
ensure fonts are embedded with save_svg().
Chrome has startup overhead. For batch processing, consider: