pzavolinsky / SVG-to-PDFKit

Insert SVG into a PDF document created with PDFKit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SVG-to-PDFKit

Insert SVG into a PDF document created with PDFKit.

Use:

SVGtoPDF(doc, svg, x, y, options);

    If you prefer, you can add the function to the PDFDocument prototype:

PDFDocument.prototype.addSVG = function(svg, x, y, options) {
  return SVGtoPDF(this, svg, x, y, options), this;
};

    And then simply call:

doc.addSVG(svg, x, y, options);

Parameters:

doc [PDFDocument] = the PDF document created with PDFKit
svg [SVGElement or string] = the SVG object or XML code
x, y [number] = the position where the SVG will be added
options [Object] = >
  - width, height [number] = initial viewport, by default it's the page dimensions
  - useCSS [boolean] = use the CSS styles computed by the browser (for SVGElement only)
  - fontCallback [function] = function called to get the fonts, see source code
  - imageCallback [function] = same as above for the images (for Node.js)

Demo:

    https://alafr.github.io/SVG-to-PDFKit/examples/demo.htm

Supported:

  • shapes: rect, circle, path, ellipse, line, polyline, polygon
  • special elements: use, nested svg
  • text elements: text, tspan, textPath
  • text attributes: x, y, dx, dy, rotate, text-anchor, textLength, word-spacing, letter-spacing, font-size
  • styling: with attributes only
  • colors: fill, stroke & color (rgb, rgba, hex, string), fill-opacity, stroke-opacity & opacity
  • units: all standard units
  • transformations: transform, viewBox & preserveAspectRatio attributes
  • clip paths & masks
  • gradients

Unsupported:

  • links (#18)
  • patterns
  • filters
  • other things I don't even know they exist

Warning:

  • Make sure to use a fully updated PDFKit version: see here how to build it
  • There are bugs, please send issues and/or pull requests.

License:

    MIT

Other useful projects:

  • PDFKit, the JavaScript PDF generation library for Node and the browser.
  • For inserting SVG graphics into a PDFKit document there is also svgkit.
  • For the opposite conversion, from PDF to SVG, you can use Mozilla's PDF.js.

About

Insert SVG into a PDF document created with PDFKit


Languages

Language:JavaScript 100.0%