Search by

adachsoft / svg-to-stl

Arkadiusz Adach

Convert filled SVG artwork into watertight 3D relief meshes and STL files.

Package info

gitlab.com/a.adach/svg-to-stl

Issues

pkg:composer/adachsoft/svg-to-stl

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.0 2026-11-29 15:39 UTC

This package is auto-updated.

Last update: 2026-11-29 14:39:15 UTC


README

adachsoft/svg-to-stl converts filled SVG artwork into watertight, millimetre-scale relief models. It supports separate relief heights per fill colour, optional base shapes, raised rims, keyring holes, and binary or ASCII STL output. The library is implemented in PHP and does not require external graphics or mesh executables.

Requirements and installation

  • PHP 8.3 or newer
  • Composer

Install the package in a PHP project:

composer require adachsoft/svg-to-stl

Supported SVG input

The parser accepts filled SVG path, rect, circle, ellipse, polygon, and polyline geometry, including supported transforms and fill rules. It does not render strokes or text. Convert both to paths before exporting the SVG:

  1. Open the artwork in Inkscape.
  2. Select text and choose Path → Object to Path.
  3. Select stroked objects and choose Path → Stroke to Path.
  4. Save the document as plain SVG, with the intended fill colours and no unconverted text or strokes.

Only filled shapes are included in the model. Unsupported SVG features are not rendered as a browser would render them.

Usage

Use the PHP API or the command-line tool to convert filled SVG artwork into STL.

PHP quick start

<?php

declare(strict_types=1);

use AdachSoft\SvgToStl\Mesh\MeshValidator;
use AdachSoft\SvgToStl\Model\ReliefOptions;
use AdachSoft\SvgToStl\ReliefBuilder;
use AdachSoft\SvgToStl\Stl\StlFormatEnum;
use AdachSoft\SvgToStl\Stl\StlWriter;

$svg = file_get_contents('logo.svg');
if ($svg === false) {
    throw new RuntimeException('Unable to read logo.svg.');
}

$options = ReliefOptions::coin(40.0);
$mesh = (new ReliefBuilder())->build($svg, $options);
if (!MeshValidator::isWatertight($mesh)) {
    throw new RuntimeException('The generated mesh is not watertight.');
}

(new StlWriter())->write($mesh, 'logo.stl', StlFormatEnum::BINARY);

targetWidthMm controls the artwork width. The builder centers the artwork and scales it uniformly; base padding is added outside its bounds. Use StlFormatEnum::ASCII for a text STL.

CLI quick start

After installing the package, run the Composer binary or invoke the script with PHP:

svg-to-stl logo.svg logo.stl --preset coin --width 40
php bin/svg-to-stl logo.svg logo-ascii.stl --preset relief --height 2 --ascii

The command reports the triangle count, watertight status, and output size. It returns a non-zero exit code and a readable error message for invalid arguments, unreadable input, conversion failures, non-watertight output, or write failures.

CLI options

svg-to-stl input.svg output.stl [--preset coin|keychain|relief]
  [--width mm] [--height mm] [--base mm]
  [--base-shape circle|rect|rounded|none] [--rim mm] [--hole mm]
  [--color-height '#ffffff=3' ...] [--ascii]
  • --preset starts from the named preset; other supplied options override its width, relief height, base thickness, base shape, rim width, or keyring-hole diameter.
  • --width sets the target artwork width in millimetres.
  • --height sets the default relief height in millimetres.
  • --base sets the base thickness in millimetres. Without a preset, a positive base thickness uses the rectangular base by default.
  • --base-shape selects a circular, rectangular, rounded-rectangle, or absent base.
  • --rim sets the rim width in millimetres. Its height is 1 mm unless the selected preset supplies a different rim height; --rim 0 disables it.
  • --hole sets the keyring-hole diameter in millimetres; zero disables the hole.
  • --color-height may be repeated to set a positive relief height for a six-digit fill colour, for example --color-height '#ffffff=3' --color-height '#000000=1.2'.
  • --ascii writes ASCII STL instead of binary STL.

Relief options

OptionDefaultDescription
targetWidthMm50Artwork width in millimetres.
reliefHeightMm2Default artwork extrusion height.
baseThicknessMm0Base thickness; zero means no base.
baseShapeNONE without a base; otherwise RECTANGLENONE, RECTANGLE, ROUNDED_RECTANGLE, or CIRCLE.
basePaddingMm2Padding between artwork bounds and the base outline.
baseCornerRadiusMm2Rounded-rectangle corner radius.
rimWidthMm0Raised rim width; zero disables the rim.
rimHeightMm0Raised rim height; must be positive when rim width is positive.
keyringHoleDiameterMm0Through-hole diameter; zero disables the hole.
flattenTolerance0.05Maximum curve deviation in output millimetres.
heightByColour[]Per-fill relief heights keyed by normalized #rrggbb colour.
mirrorfalseWhether to mirror the centered artwork horizontally.

A rectangular base extends each artwork bound by basePaddingMm. A circular base encloses the artwork bounding-box diagonal and adds the same padding. Rounded rectangles and circles are flattened using flattenTolerance. A keyring hole is placed near the top edge, must fit inside the base, and is rejected if it intersects the artwork bounds.

Presets

ReliefOptions::coin(40.0);     // circular base, 2 mm thick; 1 mm relief; 1.5 mm × 1 mm rim
ReliefOptions::keychain(40.0); // rounded base, 3 mm thick; 1.5 mm relief; 5 mm keyring hole
ReliefOptions::relief(100.0);  // no base; 3 mm relief

Preset width arguments set the artwork width. The circular coin base grows to enclose the artwork bounds plus padding. The keychain preset uses additional base padding so its 5 mm hole stays clear of the artwork bounds.

Multi-colour prints

SVG shapes are extruded as separate solids. By default, shapes use reliefHeightMm; heightByColour overrides that height for a fill colour. The CLI accepts repeated --color-height '#rrggbb=mm' arguments. For a single-nozzle printer, assign heights that align with layer boundaries, then add a pause or colour change at the corresponding layer in the slicer.

Print tips

  • Choose a layer height that divides the relief heights cleanly when using a colour change at a specific Z height.
  • In PrusaSlicer or Bambu Studio, use Add pause/colour change at layer at the intended transition.
  • Confirm the model scale and the target layer before printing; STL files do not carry colour metadata.
  • The generated mesh is intended to be watertight and can be checked with MeshValidator::isWatertight() before writing it.

Limitations and licensing

Overlapping SVG shapes are emitted as separate closed solids. Slicers are expected to union those solids during slicing; the library does not perform boolean operations or resolve overlapping geometry itself. The parser also does not convert text or strokes and does not support arbitrary SVG filters, masks, or browser rendering behavior.

The earcut triangulation port included in this package is distributed under the ISC license. The rest of the package is licensed under MIT.