How to automate svg base64 encoding for json apis and build scripts
- Step 1Node.js one-liner for local files — const base64 = Buffer.from(fs.readFileSync('icon.svg')).toString('base64'). For batch: const icons = Object.fromEntries(fs.readdirSync('./icons').map(f => [f, Buffer.from(fs.readFileSync('./icons/'+f)).toString('base64')]))
- Step 2Use the JAD batch API — POST multiple SVG files to /api/svg/to-code with format: base64. The response is a JSON object mapping filename to Base64-encoded string.
- Step 3Generate a typed icon manifest — From the batch API response, generate: export const Icons = { searchIcon: 'data:image/svg+xml;base64,...' } as const. This gives TypeScript consumers typed icon names.
Frequently asked questions
What is the fastest way to Base64 encode SVGs in Node.js?+
Buffer.from(svgString, 'utf8').toString('base64') is the native Node.js approach. For files: Buffer.from(fs.readFileSync('icon.svg')).toString('base64'). No dependencies required.
Can I pre-encode SVGs and bundle them with my app?+
Yes. Generate a JSON file with all Base64-encoded SVGs at build time and import it in your app. This avoids runtime encoding costs. The JSON file will be ~33% larger than the raw SVGs.
How do I use batch-encoded SVGs in a PDF library?+
Libraries like pdfmake and jsPDF accept SVG images as Base64 strings. Pass the Base64 output directly: doc.addImage(base64Str, 'SVG', x, y, width, height).
Does the batch API support including the data URI prefix?+
Yes. Pass include_prefix: true to get data:image/svg+xml;base64,[encoded] strings, or false (default) for raw Base64 only.
Privacy first
Every JAD SVG tool runs entirely in your browser using the DOM API and Canvas. Your SVG files never leave your device — verified by zero outbound network requests during processing.