Static Export (SVG, PNG, PDF)

JSXGraph.jl supports exporting boards as static images via save() or the direct export functions save_svg(), save_png(), save_pdf().

Requirements

Static export is provided via a package extension that depends on NodeJS_22_jll. Node.js v22 is automatically downloaded as a Julia artifact — no system installation needed. On first export, required npm packages are installed in a package-local directory (.node_deps/).

Formatnpm packages (auto-installed)
SVGjsdom
PNGjsdom, sharp
PDFjsdom, pdfkit, svg-to-pdfkit
using Pkg
Pkg.add("NodeJS_22_jll")

Usage

Export via save()

Load NodeJS_22_jll, then the save() function dispatches on the file extension:

using JSXGraph
using NodeJS_22_jll  # activates static export extension

b = board("myboard", xlim=(-5, 5), ylim=(-5, 5)) do b
    push!(b, point(1, 2; name="A"))
    push!(b, circle(point(0, 0), 3; strokeColor="blue"))
end

# HTML export (always available)
save("plot.html", b)

# SVG export
save("plot.svg", b)

# PNG export
save("plot.png", b)

# PNG with 2× resolution (Retina/HiDPI)
save("plot_hd.png", b; scale=2)

# PDF export
save("plot.pdf", b)

Direct export functions

save_svg("plot.svg", b)
save_png("plot.png", b)
save_png("plot_hd.png", b; scale=2)
save_pdf("plot.pdf", b)

Supported Formats

ExtensionFormatBackend
.htmlSelf-contained HTML pageBuilt-in
.svgScalable Vector GraphicsNodeJS22jll + jsdom
.pngPortable Network GraphicsNodeJS22jll + jsdom + sharp
.pdfPDF documentNodeJS22jll + jsdom + pdfkit + svg-to-pdfkit

Unsupported extensions (e.g. .gif, .bmp) raise an ErrorException.

Scale Factor (PNG)

The scale keyword controls the output resolution for PNG exports:

save("plot.png", board; scale=1)  # native resolution (e.g. 500×500)
save("plot.png", board; scale=2)  # 2× resolution (e.g. 1000×1000)
save("plot.png", board; scale=3)  # 3× resolution (e.g. 1500×1500)

Higher scale values produce sharper images suitable for print or HiDPI displays.

API Reference

JSXGraph.save_svgFunction
save_svg(filename::String, board::Board)

Export a board as a static SVG image file.

Requires NodeJS_22_jll to be installed. Add it with:

using Pkg; Pkg.add("NodeJS_22_jll")

Then load it before calling save_svg:

using NodeJS_22_jll
save("plot.svg", board)

Arguments

  • filename::String: Output file path (should end in .svg)
  • board::Board: The board to export
source
JSXGraph.save_pngFunction
save_png(filename::String, board::Board; scale::Int=1)

Export a board as a PNG image file.

The scale parameter controls the resolution multiplier (e.g. scale=2 produces a 2× resolution image suitable for Retina/HiDPI displays).

Requires NodeJS_22_jll to be installed. Add it with:

using Pkg; Pkg.add("NodeJS_22_jll")

Then load it before calling save_png:

using NodeJS_22_jll
save("plot.png", board)
save("plot_hd.png", board; scale=2)  # 2× resolution

Arguments

  • filename::String: Output file path (should end in .png)
  • board::Board: The board to export
  • scale::Int=1: Resolution multiplier (1 = native, 2 = 2× Retina)
source
JSXGraph.save_pdfFunction
save_pdf(filename::String, board::Board)

Export a board as a PDF document.

Requires NodeJS_22_jll to be installed. Add it with:

using Pkg; Pkg.add("NodeJS_22_jll")

Then load it before calling save_pdf:

using NodeJS_22_jll
save("plot.pdf", board)

Arguments

  • filename::String: Output file path (should end in .pdf)
  • board::Board: The board to export
source

How It Works

  1. A Node.js script is generated that creates a virtual DOM using jsdom
  2. The JSXGraph library is injected into the virtual DOM
  3. The board is initialized with all elements
  4. The SVG element is extracted from the DOM
  5. For SVG: the raw SVG is written with proper XML headers
  6. For PNG: sharp converts the SVG to a raster image at the specified scale
  7. For PDF: pdfkit + svg-to-pdfkit embed the SVG in a PDF document

Node.js is provided by NodeJS_22_jll (Julia artifact, no system dependency).

Troubleshooting

"SVG/PNG/PDF export requires the NodeJS22jll package": Load it first:

using NodeJS_22_jll

npm package installation fails: Try installing manually:

cd ~/.julia/packages/JSXGraph/<hash>/.node_deps
~/.julia/artifacts/<nodejs_hash>/bin/npm install jsdom sharp pdfkit svg-to-pdfkit