adachsoft / svg-to-stl
Convert filled SVG artwork into watertight 3D relief meshes and STL files.
Requires
- php: ^8.3
- adachsoft/collection: ^3
Requires (Dev)
- adachsoft/php-code-style: ^0.8.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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:
- Open the artwork in Inkscape.
- Select text and choose Path → Object to Path.
- Select stroked objects and choose Path → Stroke to Path.
- 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]
--presetstarts from the named preset; other supplied options override its width, relief height, base thickness, base shape, rim width, or keyring-hole diameter.--widthsets the target artwork width in millimetres.--heightsets the default relief height in millimetres.--basesets the base thickness in millimetres. Without a preset, a positive base thickness uses the rectangular base by default.--base-shapeselects a circular, rectangular, rounded-rectangle, or absent base.--rimsets the rim width in millimetres. Its height is 1 mm unless the selected preset supplies a different rim height;--rim 0disables it.--holesets the keyring-hole diameter in millimetres; zero disables the hole.--color-heightmay 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'.--asciiwrites ASCII STL instead of binary STL.
Relief options
| Option | Default | Description |
|---|---|---|
targetWidthMm | 50 | Artwork width in millimetres. |
reliefHeightMm | 2 | Default artwork extrusion height. |
baseThicknessMm | 0 | Base thickness; zero means no base. |
baseShape | NONE without a base; otherwise RECTANGLE | NONE, RECTANGLE, ROUNDED_RECTANGLE, or CIRCLE. |
basePaddingMm | 2 | Padding between artwork bounds and the base outline. |
baseCornerRadiusMm | 2 | Rounded-rectangle corner radius. |
rimWidthMm | 0 | Raised rim width; zero disables the rim. |
rimHeightMm | 0 | Raised rim height; must be positive when rim width is positive. |
keyringHoleDiameterMm | 0 | Through-hole diameter; zero disables the hole. |
flattenTolerance | 0.05 | Maximum curve deviation in output millimetres. |
heightByColour | [] | Per-fill relief heights keyed by normalized #rrggbb colour. |
mirror | false | Whether 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.