svg4pdf-lib
v0.2.0
Published
Insert SVG into a PDF document created with @pdfme/pdf-lib. Converts SVG directly to pdf-lib operators.
Readme
svg4pdf-lib
Insert SVG into a PDF document created with @pdfme/pdf-lib (a maintained fork of the original pdf-lib).
svg4pdf-lib walks the SVG tree and emits @pdfme/pdf-lib operators and PDF objects directly. There is no intermediate PDF-instruction layer: paths, paints, transparency groups, shadings, patterns, text and images all become pdf-lib calls in a single pass.
Install
npm install svg4pdf-lib @pdfme/pdf-lib@pdfme/pdf-lib is a peer dependency, so you control its version. @pdf-lib/upng is pulled in for PNG decoding; PDFKit is not a dependency.
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— top-left corner of the drawing area on the PDF page (pt).options— optional:width,height— desired size in pt (defaults to the page size).preserveAspectRatio— same syntax as SVG (xMidYMid meet, etc.).fontCallback(family, bold, italic, options)— returns an embeddedPDFFontfor the requested face. Called for every<text>element; returnundefinedto skip the text.warningCallback(message)— receives non-fatal warnings.imageCallback(link)— transform an<image>href into the source handed to the image loader.cmyk— whentrue, convert named colors and default paints to CMYK. SVGcmyk(c,m,y,k)syntax is always accepted.colorCallback(color, raw)— transform or replace parsed SVG colors before they are applied. Useful for custom spot-color handling.precision— curve subdivision precision for path geometry (default 3).
Coverage
- Shape elements:
rect,circle,ellipse,line,polyline,polygon,path(including elliptical arcs). - Text:
text,tspan,textPath, withtext-anchor,dominant-baseline,baseline-shift, letter and word spacing,textLength, text decoration,xml:space. - Structure:
g,svg,defs,use,symbol,a. - Styling: presentation attributes,
style="",<style>rules with tag/id/class selectors, and property inheritance. - Effects:
marker,clipPath,mask(luminance and alpha), opacity, blend modes, dash patterns,vector-effect. - Paint servers: linear and radial gradients, and patterns as tiling patterns, both including per-stop alpha via soft masks.
- CMYK: named colors,
cmyk(c,m,y,k)syntax, and CMYK gradients whenoptions.cmykis enabled. - Raster images:
<image>referencing base64data:image/pngordata:image/jpegURIs, with PNG alpha as an SMask. - Link annotations:
<a>elements become PDF link annotations.
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. The SVG parsing and layout logic is derived from SVG-to-PDFKit and svg-for-pdfkit; the upstream code remains available under the MIT License reproduced in the LICENSE file.
