Rsvg-convert Provider
rsvg-convert is a lightweight command-line tool from the librsvg project. It is widely available on Linux distributions as a standard package and supports PNG, PDF, PS, EPS, and SVG output.
Supported Formats
| Format | Supported |
|---|---|
| PNG | Yes |
| Yes | |
| PS | Yes |
| EPS | Yes |
| SVG | Yes |
Basic Usage
use Laratusk\Larasvg\Facades\SvgConverter;
SvgConverter::using('rsvg-convert')
->open(resource_path('svg/logo.svg'))
->setFormat('png')
->setDimensions(512, 512)
->toFile(storage_path('app/logo.png'));Zoom
Scale the output with a zoom factor instead of explicit dimensions:
// 2× zoom
$converter->setZoom(2.0);
// Independent horizontal and vertical zoom
$converter->setXZoom(2.0)->setYZoom(1.5);Aspect Ratio
Preserve the aspect ratio when both width and height are set:
$converter->setWidth(800)->setHeight(600)->keepAspectRatio();Background Color
rsvg-convert accepts HEX, RGB, and RGBA colors directly:
// Solid background
$converter->setBackground('#ffffff');
$converter->setBackground('rgb(255, 255, 255)');
// Transparent background via RGBA
$converter->setBackground('rgba(255, 255, 255, 0.5)');Background Opacity
rsvg-convert has no separate --background-opacity flag. Calling setBackgroundOpacity() combines the opacity with the color automatically, converting it to an rgba() value:
$converter->setBackground('#ffffff')->setBackgroundOpacity(0.5);
// Sends: --background-color='rgba(255,255,255,0.5)'Page Dimensions (PDF / PS)
Set physical page size for vector output:
$converter
->setFormat('pdf')
->setPageWidth('210mm') // A4 width
->setPageHeight('297mm') // A4 height
->keepAspectRatio()
->toFile(storage_path('app/output.pdf'));CSS length units are accepted: px, pt, in, cm, mm.
Page Margins
Offset the rendered content within the page:
$converter->setTopMargin('10mm')->setLeftMargin('15mm');Stylesheet
Apply an external CSS stylesheet to the SVG before rendering:
$converter->setStylesheet(resource_path('css/svg-theme.css'));Unlimited Mode
Disable the SVG parser guard limits for large or complex SVGs:
$converter->unlimited();Image Data (PDF / PS)
Control whether embedded images in the SVG are kept compressed or expanded:
// Keep compressed (default for PDF/PS)
$converter->keepImageData(true);
// Embed as uncompressed RGB
$converter->keepImageData(false);Base URI
Set the base URI for resolving relative resource references inside the SVG:
$converter->setBaseUri('file://'.resource_path('svg/'));Full Example
SvgConverter::using('rsvg-convert')
->open(resource_path('svg/report.svg'))
->setFormat('pdf')
->setPageWidth('210mm')
->setPageHeight('297mm')
->keepAspectRatio()
->setBackground('#ffffff')
->setStylesheet(resource_path('css/print.css'))
->setBaseUri('file://'.resource_path('svg/'))
->toFile(storage_path('app/report.pdf'));