gymmed/svg-parser
最新稳定版本:v0.1
Composer 安装命令:
composer require gymmed/svg-parser
包简介
Includes css document styles to svg without editing original svg document.
README 文档
README
Svg Parser PHP
Applies CSS document styles to an SVG without modifying the original SVG file. It also parses CSS variables and inserts their values directly into CSS selectors styles, as it is intendent for broader use than just for browser rendering.
Installation
You can install the package via composer:
composer require gymmed/svg-parser-php
Use Case
Dompdf or laravel wrapper laravel-dompdf don't support direct SVG import or requires the inclusion of external SVG files. This library provides a way to attach CSS documents to SVG files without needing to directly modify the SVG.
If you stumble upon the same problem, I highly recommend using imagick or rasterize-svg to convert the SVG to PNG and include it in the PDF. This way, you reduce the chances of the styles being rendered incorrectly by Dompdf and won't need two different CSS documents or two SVG files.
Usage
use GymMed\SvgParser; //provide full path $svgPath = realpath(__DIR__ . "./assets/example.svg"); $cssPath = realpath(__DIR__ . "./assets/css/index.css"); $svgWithCSS = SvgParser::formatSvgFromPath($svgPath, [$cssPath]);
or use SVG content:
use GymMed\SvgParser; //provide full path $svgPath = realpath(__DIR__ . "./assets/example.svg"); $cssPath = realpath(__DIR__ . "./assets/css/index.css"); $svg = file_get_contents($svgPath); $svgWithCSS = SvgParser::formatSvg($svg, [$cssPath]);
If you want to write simple CSS and apply it inside SVG:
use GymMed\SvgParser; //provide full path $svgPath = realpath(__DIR__ . "./assets/example.svg"); $svg = file_get_contents($svgPath); $svgWithCSS = SvgParser::getParsedCSSToSvg($svg, ".circle { background-color: red; border-radius: 50% }");
If you want to provide only full path instead of SVG itself:
use GymMed\SvgParser; //provide full path $svgPath = realpath(__DIR__ . "./assets/example.svg"); $svgWithCSS = SvgParser::getParsedCSSToSvgFromPath($svgPath, ".circle { background-color: red; border-radius: 50% }");
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-15