svg4pdf-lib
v0.1.1
Published
Insert SVG into a PDF document created with @pdfme/pdf-lib. Built on top of svg-for-pdfkit.
Readme
svg4pdf-lib
Insert SVG into a PDF document created with @pdfme/pdf-lib (a maintained fork of the original pdf-lib).
This package is an adapter for svg-for-pdfkit, a community-maintained fork of the excellent SVG-to-PDFKit by alafr. svg-for-pdfkit handles SVG parsing, property inheritance, transforms, markers, gradients, patterns, masks, and text; svg4pdf-lib translates its PDFKit-style drawing calls into @pdfme/pdf-lib vector operators.
The adapter configures svg-for-pdfkit with flipYAxis: true and negateTextMatrix: true so that the emitted PDF operators match @pdfme/pdf-lib's coordinate system and text-matrix convention.
Install
npm install svg4pdf-lib @pdfme/pdf-lib@pdfme/pdf-lib is a peer dependency. svg-for-pdfkit is included as a runtime dependency and does not pull in PDFKit.
Usage
import {PDFDocument} from "@pdfme/pdf-lib"
import {drawSvg} from "svg4pdf-lib"
const pdfDoc = await PDFDocument.create()
const page = pdfDoc.addPage([612, 792])
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<circle cx="50" cy="50" r="40" fill="red" stroke="black" stroke-width="2"/>
</svg>`
drawSvg(page, svg, 100, 100, {
width: 200,
height: 200,
fontCallback: (family, bold, italic) => {
// Return an already-embedded PDFFont for the requested family/style.
return myEmbeddedFonts[family]
}
})
const bytes = await pdfDoc.save()API
drawSvg(page, svg, x, y, options?)
page— a@pdfme/pdf-libPDFPage.svg— SVG markup as a string.x,y— position on the PDF page (pt), origin bottom-left.options— optional:width,height— desired size in pt. Aspect ratio is preserved by default.preserveAspectRatio— same syntax as SVG (xMidYMid meet, etc.).fontCallback(family, bold, italic, options)— returns aPDFFontfor the requested face. Called for every<text>element.warningCallback(message)— receives non-fatal warnings.cmyk— whentrue, convert named colors and default paints to CMYK (0-1). Also enables SVGcmyk(c,m,y,k)color syntax.colorCallback(color, raw)— transform or replace parsed SVG colors before they are applied. Useful for custom spot-color handling.
Coverage
Inherited from svg-for-pdfkit:
- Shape elements:
rect,circle,ellipse,line,polyline,polygon,path. - Text:
text,tspan,textPath(withtext-anchor,dominant-baseline, etc.). - Structure:
g,svg,defs,use,symbol,a. - Effects:
marker,clipPath,mask, transforms, opacity, fill/stroke styling, dash patterns. - Paint servers: linear/radial gradients and patterns (tiling patterns via Form XObjects), including per-stop alpha via soft masks.
- CMYK colors: named colors,
cmyk(c,m,y,k)CSS syntax, and CMYK gradients whenoptions.cmykis enabled. - Raster images:
<image>elements referencing base64data:image/pngordata:image/jpegURIs.
Limitations:
- Remote image URLs are not fetched; provide base64 data URIs or pre-resolve images through
options.imageCallback. - Spot colors (Separation colorspaces) are not rendered automatically. Use
options.colorCallbackto map spot-color names to concrete RGB/CMYK values.
License
LGPL-3.0-or-later. See LICENSE. This package is derived from SVG-to-PDFKit and svg-for-pdfkit; the upstream code remains available under the MIT License reproduced in the LICENSE file.
