dompdf/php-svg-lib
PHP library for parsing and rendering SVG documents. Provides an object model for SVG elements, support for styles, paths, and basic shapes, and can render to common backends (e.g., PDF via dompdf). Useful for embedding SVG graphics in PDFs.
Begin by installing the package via Composer: composer require dompdf/php-svg-lib. Although it's primarily used as a dependency of dompdf, you can use it directly to parse and process SVGs in isolation. Your first use case is likely converting an SVG file or string into a DOM-like structure for inspection or manipulation:
use DOMPDF\PhpSvgLib\Svg;
$svgString = '<svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="red"/></svg>';
$svg = new Svg();
$document = $svg->loadString($svgString);
// Access the parsed root element and its children
Check the src/ directory for the main entry points: Svg, Svg\Loader\Loader, and Svg\Document. Start with Svg::loadString() or Svg::loadFile() to parse SVG content.
In PDF Generation (with dompdf): When rendering HTML containing <img> tags or inline SVGs to PDF, php-svg-lib is automatically invoked behind the scenes. Ensure your SVGs are well-formed and avoid unsupported features (e.g., <script> or animation), or they may render incorrectly or be ignored.
Direct SVG Preprocessing: Use Svg\Loader\Loader to parse SVGs before feeding them to image generators or rasterizers. Example preprocessing: extract metadata, validate viewBox, or normalize stroke/fill styles.
$loader = new \DOMPDF\PhpSvgLib\Loader\Loader();
$svgDoc = $loader->loadFile('logo.svg');
$root = $svgDoc->getElementByTagName('svg');
$root->setAttribute('viewBox', '0 0 24 24');
file_put_contents('normalized.svg', $svgDoc->saveXml());
Custom Rendering Pipelines: Integrate Svg\Renderer\Renderer to render SVGs to custom backends (e.g., GD, Imagick, or canvas drawing commands), especially useful in headless environments where imagettftext() or imageloadfont() are insufficient.
Dynamic SVG Manipulation: Inject/modify SVG content programmatically before rendering—e.g., replace placeholder colors based on user preferences, or append overlays.
Limited Feature Support: This library supports only a subset of SVG spec. Critical gaps: gradients (except solid fill), filters, masks, clip paths, <use> references, and most text rendering (only basic <text> with x/y/dy). Avoid relying on advanced CSS or inline styles.
Color Parsing Quirks: Colors must be standard CSS values (#hex, rgb(), rgba(), or named colors like red). CSS variables (var(--primary)) and currentColor are not resolved automatically.
Transform Order Matters: Composite transforms (transform="scale(2) rotate(45)") are applied right-to-left. Verify transformations by printing the transform matrix on elements if rendering appears skewed.
Namespace Handling: SVGs should not include the xmlns:xlink namespace unless strictly required; it may cause parsing issues in older versions.
Debugging Failed Renders: Enable libxml_use_internal_errors(true) before DOM operations to silence XML warnings, and inspect $svg->getWarnings() if using the high-level loader. Check that your input SVG has a valid viewBox—a missing one often causes rendering at 0px dimensions.
Extension Points: Extend Svg\Loader\Loader or Svg\Renderer\Renderer subclasses to handle custom elements (e.g., my-custom-tag) by overriding handleElement() methods. This is critical for rendering SVGs with brand-specific icons or internal metadata tags.
How can I help you explore Laravel packages today?