On this page

Developer reference

Every function and option, what it returns, and how to show the pages in a web page. Examples are written to run as shown.

Which one should I use?

Everything runs on your own machine, in your own process. The Node.js package converts on a background thread so the event loop keeps running, and the Python package releases the GIL while it works. No server, account or network access is involved.

You want to…Use
Convert files in a terminal, a script or CIdocsvg
Show uploaded documents in a web or Electron appNode.js preview() + document-svg/preview-ui
Convert from Python: scripts, notebooks, web APIsPython preview() / convert()
Build it into a Rust programconvert_path() / convert_bytes()
Convert inside the browser only (PDF, Word, Excel, PowerPoint)<docsvg-viewer> (WebAssembly)

Command line: docsvg

The format is chosen from the file extension, with a look at the file's first bytes when the extension is missing or generic. The command exits with 0 on success, and with 1 and a message on standard error when conversion fails.

docsvg <INPUT> --output <FOLDER> [options]
docsvg reverse <SVG or FOLDER> --output <FILE>
docsvg transform <SVG or -> --output <SVG or -> [options]

Convert: docsvg <INPUT> --output <FOLDER>

OptionDefaultWhat it does
-o, --output DIRrequiredNew or empty folder that receives the pages and conversion.json.
--jobs N1How many PDF pages to convert at once. Other formats are converted page by page.
--max-input-mib N512 MiBLargest input file accepted.
--max-entry-mib N128 MiBLargest expanded part inside a PDF or a ZIP-based file (Office, EPUB, 3MF, CBZ).
--max-pages N10,000Most pages, slides or sheets written.
--max-xml-events N5,000,000Most XML parser steps per part; stops oversized or hostile XML.
--no-metadatametadata onLeave the provenance metadata out of each SVG.
--precision N5Decimal places kept in coordinates. Smaller numbers give smaller files.
--outline-embedded-pdf-textoffDraw embedded PDF fonts as shapes: looks exactly like the original, but the text can no longer be selected. Check that the font's license allows this.
--embed-drawio-sourceoffKeep the draw.io source inside each SVG so it can be turned back into an editable diagram. Roughly doubles the size.
--stencils PATHnonedraw.io shape library files or folders to draw library shapes with. Without them, such shapes become labelled placeholders.

Write back: docsvg reverse

OptionDefaultWhat it does
-o, --output FILErequiredThe file to create. Its extension picks the format: .pptx, .docx, .xlsx, .pdf, .drawio, .dxf, .gcode, .stl, .html, .webp and more.
--max-input-mib N512 MiBLargest total SVG input.
--max-pages N10,000Most SVG pages read.

Tidy up: docsvg transform

OptionDefaultWhat it does
--minifyoffRemove comments and extra spaces.
--monochrome COLORoffPaint every fill and stroke in one colour, e.g. "#1e293b".
--responsiveoffDrop the fixed size so the SVG scales to its container.
--precision NnoneRound coordinates to N decimal places.
--remove-metadataoffRemove <metadata>, <desc> and data-* attributes.
--clean-pathsoffTidy path data (drop zero-length segments and duplicate closes).
--strip-empty-groupsoffRemove groups that draw nothing.

What a conversion writes

The folder must be new or empty; existing files are never overwritten. conversion.json is the report of the whole job. The same fields come back from convert() and preview() (camelCase in Node.js).

out/
├── page-0001.svg
├── page-0002.svg
└── conversion.json
FieldWhat it does
page_countNumber of pages written.
warningsDocument-wide notes on anything approximated or left out. Empty does not guarantee a pixel-perfect match.
pages[].svgFile name of the page (convert) or the complete SVG markup (preview).
pages[].width_points, height_pointsPage size in points (1 pt = 1/72 inch).
pages[].warningsNotes for that page only.
source_formatThe format that was detected, such as pptx or pdf.
needs_reviewpreview() only: true when the document or any page has a warning.
elapsed_ms, input_bytes, versionHow long it took, how big the input was, and which version made it.

Node.js

Errors reject the promise with an Error whose message starts with the kind of problem: "invalid input", "unsupported input", "safety limit exceeded" or "I/O error".

const { convert, preview, reverse, transform } = require('document-svg')

async function main() {
  const result = await preview('slides.pptx', { maxPages: 50 })
  console.log(result.pageCount, result.needsReview)
  const firstPageSvg = result.pages[0].svg

  const report = await convert('slides.pptx', 'out/slides', { jobs: 2 })
  await reverse('out/slides', 'slides-copy.pptx')
  const small = transform(firstPageSvg, { minify: true })
}

main().catch(console.error)
FunctionReturnsWhat it does
preview(file, options?)Promise<PreviewReport>Every page as SVG text, in memory. Nothing is left on disk.
convert(file, folder, options?)Promise<ConversionReport>Write pages and conversion.json to a folder.
reverse(svgOrFolder, file, options?)Promise<ReverseReport>Package SVG pages as another file type. Options: maxInputBytes, maxPages.
transform(svg, options?)stringTidy an SVG string; runs synchronously.

Options for preview() and convert()

OptionDefaultWhat it does
jobs1How many PDF pages to convert at once. Other formats are converted page by page.
maxInputBytes512 MiBLargest input file accepted.
maxZipEntryBytes128 MiBLargest expanded part inside a PDF or a ZIP-based file (Office, EPUB, 3MF, CBZ).
maxPages10,000Most pages, slides or sheets written.
maxXmlEvents5,000,000Most XML parser steps per part; stops oversized or hostile XML.
includeMetadata: falsemetadata onLeave the provenance metadata out of each SVG.
precision5Decimal places kept in coordinates. Smaller numbers give smaller files.
outlineEmbeddedPdfTextoffDraw embedded PDF fonts as shapes: looks exactly like the original, but the text can no longer be selected. Check that the font's license allows this.
embedDrawioSourceoffKeep the draw.io source inside each SVG so it can be turned back into an editable diagram. Roughly doubles the size.
stencilPathsnonedraw.io shape library files or folders to draw library shapes with. Without them, such shapes become labelled placeholders.
maxSvgBytes64 MiBpreview() only: largest SVG for one page.
maxTotalSvgBytes256 MiBpreview() only: largest total for all pages.

Options for transform()

OptionDefaultWhat it does
minifyoffRemove comments and extra spaces.
monochromeoffPaint every fill and stroke in one colour, e.g. "#1e293b".
responsiveoffDrop the fixed size so the SVG scales to its container.
precisionnoneRound coordinates to N decimal places.
removeMetadataoffRemove <metadata>, <desc> and data-* attributes.
cleanPathsoffTidy path data (drop zero-length segments and duplicate closes).
stripEmptyGroupsoffRemove groups that draw nothing.

document-svg/preview-ui (runs in the browser; no native code)

FunctionReturnsWhat it does
createSvgPreviewUrl(svg)stringA Blob URL for an <img>. Checks the SVG first.
revokeSvgPreviewUrl(url)voidRelease a Blob URL when the image goes away.
createSvgPreviewDataUrl(svg)stringA data: URL, for passing to another window or process.
copySvgToClipboard(svg)Promise<'image/svg+xml' | 'text/plain'>Copy the image, or its source where images can't be copied. Call from a click.
copySvgSourceToClipboard(svg)Promise<void>Always copy the SVG source as text.

Python

Install document-svg, import document_svg. Results are plain dictionaries with the same fields as conversion.json.

from document_svg import convert, preview, reverse, transform

result = preview("report.docx", max_pages=50)
print(result["page_count"], result["needs_review"])
first_page_svg = result["pages"][0]["svg"]

report = convert("report.docx", "out/report", jobs=2)
reverse("out/report", "report-copy.docx")
small = transform(first_page_svg, minify=True)
FunctionReturnsWhat it does
preview(path, **options)dictEvery page as SVG text, in memory. Accepts max_svg_bytes and max_total_svg_bytes as well.
convert(path, folder, **options)dictWrite pages and conversion.json to a folder.
reverse(svg_or_folder, path, *, max_input_bytes=None, max_pages=None)dictPackage SVG pages as another file type.
transform(svg, **options)str | bytesTidy an SVG. Returns str for str input and bytes for bytes input.

Options for preview() and convert() (keyword-only)

OptionDefaultWhat it does
jobs1How many PDF pages to convert at once. Other formats are converted page by page.
max_input_bytes512 MiBLargest input file accepted.
max_zip_entry_bytes128 MiBLargest expanded part inside a PDF or a ZIP-based file (Office, EPUB, 3MF, CBZ).
max_pages10,000Most pages, slides or sheets written.
max_xml_events5,000,000Most XML parser steps per part; stops oversized or hostile XML.
include_metadata=Falsemetadata onLeave the provenance metadata out of each SVG.
precision5Decimal places kept in coordinates. Smaller numbers give smaller files.
outline_embedded_pdf_textoffDraw embedded PDF fonts as shapes: looks exactly like the original, but the text can no longer be selected. Check that the font's license allows this.
embed_drawio_sourceoffKeep the draw.io source inside each SVG so it can be turned back into an editable diagram. Roughly doubles the size.
stencil_pathsnonedraw.io shape library files or folders to draw library shapes with. Without them, such shapes become labelled placeholders.

Exceptions

ErrorWhen
ValueErrorThe input is invalid or unsupported, or a safety limit was reached.
OSErrorA file could not be read or written, e.g. the output folder is not empty.
RuntimeErrorAnything else, such as a damaged PDF, ZIP or XML structure.

Rust

Only the crate root (the functions above, their option and report types, Error) and the ir and svg modules are public API; internal readers can change between releases.

use document_svg::{convert_path, ConvertOptions};

fn main() -> Result<(), document_svg::Error> {
    let options = ConvertOptions { jobs: 2, ..ConvertOptions::default() };
    let report = convert_path("report.pdf", "out/report", &options)?;
    println!("{} pages, {} warnings", report.page_count, report.warnings.len());
    Ok(())
}
FunctionReturnsWhat it does
convert_path(input, folder, &ConvertOptions)Result<ConversionReport>Convert a file and write pages and conversion.json.
convert_bytes(name, &bytes, &ConvertOptions, ByteConvertLimits, on_page)Result<ByteConversionReport>Convert from memory without temporary files; on_page receives each SvgPage as soon as it is ready. Also works on wasm32.
svg_to_document(input, output, &ReverseOptions)Result<ReverseReport>Package SVG pages as another file type (the CLI's reverse).
transform_svg(&bytes, &TransformOptions)Result<Vec<u8>>Tidy an SVG (the CLI's transform).

ConvertOptions fields

OptionDefaultWhat it does
jobs1How many PDF pages to convert at once. Other formats are converted page by page.
max_input_bytes512 MiBLargest input file accepted.
max_zip_entry_bytes128 MiBLargest expanded part inside a PDF or a ZIP-based file (Office, EPUB, 3MF, CBZ).
max_pages10,000Most pages, slides or sheets written.
max_xml_events5,000,000Most XML parser steps per part; stops oversized or hostile XML.
include_metadata: falsemetadata onLeave the provenance metadata out of each SVG.
precision5Decimal places kept in coordinates. Smaller numbers give smaller files.
outline_embedded_pdf_textoffDraw embedded PDF fonts as shapes: looks exactly like the original, but the text can no longer be selected. Check that the font's license allows this.
embed_drawio_sourceoffKeep the draw.io source inside each SVG so it can be turned back into an editable diagram. Roughly doubles the size.
stencil_pathsnonedraw.io shape library files or folders to draw library shapes with. Without them, such shapes become labelled placeholders.

Error variants

ErrorWhen
InvalidInputThe input or an argument is invalid, e.g. the output folder is not empty.
UnsupportedThe format can't be recognised, or a feature isn't supported.
LimitExceededA safety limit was reached.
Io, Pdf, Xml, Zip, JsonReading or parsing failed at that layer.

Showing pages in a web page

Yes — the pages are ordinary SVG images, so any browser shows them. There are two ways to get them onto the screen.

1. Convert on your server or in Electron's main process with Node.js, send the SVG to the page, and show it with preview-ui (see the Node.js section and the preview guide). Works for every supported format.

2. Convert inside the browser with the WebAssembly build and its ready-made <docsvg-viewer> element. Nothing is uploaded and no server is needed. It covers PDF, Word, Excel and PowerPoint, and offers page thumbnails, zoom, fit to width or page, rotation, full screen, text search and download of the current page.

<script type="module" src="/bindings/wasm/web/docsvg-viewer.js"></script>
<docsvg-viewer thumbnails></docsvg-viewer>

<script type="module">
  const viewer = document.querySelector('docsvg-viewer')
  viewer.options = { maxPages: 200 }          // optional limits
  fileInput.addEventListener('change', () => viewer.openFile(fileInput.files[0]))
</script>

<docsvg-viewer> at a glance

FunctionWhat it does
thumbnailsAttribute: show a column of page thumbnails.
openFile(file)Method: convert and show a File (from an input or drag and drop, which the element also handles itself).
optionsProperty: limits such as maxInputBytes (default 64 MiB), maxPages (default 1,000) and precision.
KeyboardArrow keys and Page Up/Down move between pages, + and − zoom, f fits the width, r rotates.
  • The WebAssembly build is not published as a package. Build it from bindings/wasm (Rust and wasm-bindgen are needed), then serve bindings/wasm/web from your site.
  • Serve it over HTTP; opening the files with file:// does not work. A Content-Security-Policy needs 'wasm-unsafe-eval' for scripts and 'self' for workers.
  • Pages are shown through Blob URLs in <img> elements; the viewer never inserts document SVG into your page.

Limits at a glance

Every limit protects the machine from oversized or broken files. Lower them for your use case if you like; don't raise them just to push a difficult file through.

LimitCLI, Node.js, Python, RustBrowser (WebAssembly)
Input file512 MiB64 MiB (at most 256 MiB)
One expanded part (PDF stream, ZIP entry)128 MiB32 MiB
Pages10,0001,000
XML parser steps per part5,000,0002,000,000
SVG for one page (in-memory preview)64 MiB16 MiB
SVG for all pages (in-memory preview)256 MiB128 MiB
Coordinate decimal places54