svg-to-pdfkit

WebJar for svg-to-pdfkit

License

License

MIT
Categories

Categories

PDF Data
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

svg-to-pdfkit
Last Version

Last Version

0.1.8
Release Date

Release Date

Type

Type

jar
Description

Description

svg-to-pdfkit
WebJar for svg-to-pdfkit
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/alafr/SVG-to-PDFKit

Download svg-to-pdfkit

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/svg-to-pdfkit/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>svg-to-pdfkit</artifactId>
    <version>0.1.8</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/svg-to-pdfkit/
implementation 'org.webjars.npm:svg-to-pdfkit:0.1.8'
// https://jarcasting.com/artifacts/org.webjars.npm/svg-to-pdfkit/
implementation ("org.webjars.npm:svg-to-pdfkit:0.1.8")
'org.webjars.npm:svg-to-pdfkit:jar:0.1.8'
<dependency org="org.webjars.npm" name="svg-to-pdfkit" rev="0.1.8">
  <artifact name="svg-to-pdfkit" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='svg-to-pdfkit', version='0.1.8')
)
libraryDependencies += "org.webjars.npm" % "svg-to-pdfkit" % "0.1.8"
[org.webjars.npm/svg-to-pdfkit "0.1.8"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : pdfkit jar [0.8.1,)

Project Modules

There are no modules declared in this project.

SVG-to-PDFKit

Insert SVG into a PDF document created with PDFKit.

Install

npm install svg-to-pdfkit --save

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
  - preserveAspectRatio [string] = override alignment of the SVG content inside its viewport
  - 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)
  - documentCallback [function] = same as above for the external SVG documents
  - colorCallback [function] = function called to get color, making mapping to CMYK possible
  - warningCallback [function] = function called when there is a warning
  - assumePt [boolean] = assume that units are PDF points instead of SVG pixels
  - precision [number] = precision factor for approximative calculations (default = 3)

Fonts

In the browser, it's easier to register fonts (see here how) before calling SVGtoPDF. SVGtoPDF doesn't wait for font loading with asynchronous XMLHttpRequest.

Make sure to name the fonts with the exact pattern 'MyFont', 'MyFont-Bold', 'MyFont-Italic', 'MyFont-BoldItalic' (case sensitive), if the font is named font-family="MyFont" in the svg. Missing Bold, Italic, BoldItalic fonts are simulated with stroke and skew angle.

If your fonts don't follow this pattern, or you want to register fonts at the moment they are encountered in the svg, you can use a custom fontCallback function.

Demos

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

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

NodeJS example

    https://runkit.com/alafr/5a1377ff160182001232a91d

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
  • images
  • fonts
  • gradients
  • patterns
  • links

Unsupported

  • filters
  • text attributes: font-variant, writing-mode, unicode-bidi
  • vector-effect (#113)
  • foreignObject (#37)
  • other things I don't even know they exist

Warning

  • Use an updated PDFKit version (≥0.8.1): see here how to build it, or use the prebuilt file in the examples folder.
  • 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.

Versions

Version
0.1.8
0.1.7