Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Svg Lib Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • 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.

Gotchas and Tips

  • 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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport