Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing full SVG with PDFKit

Does anyone know if it's possible to import a full SVG into a PDFKit document? I can see from the docs that it has full SVG support, and there are methods for drawing paths etc, but I cannot see a method for importing a full SVG document.

like image 392
jcvandan Avatar asked Jul 27 '14 15:07

jcvandan


2 Answers

While searching for an answer to this question, I found a small open-source library that does the trick: SVG-to-PDFKit.

If implementing your own interface is not convenient, this one is pretty easy to use (code sample from the Readme):

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

The parameters are as such:

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

Check out the demo here.

like image 150
valentin Avatar answered Nov 12 '22 22:11

valentin


Unfortunately, as of V0.7 (Nov 2014), PDFKit does not support drawing SVG content to the PDF. Hovever, most of the building blocks are there (features covering most SVG presentations attributes + an SVG path parser), so implementing this would be a matter of traversing your SVG document tree from the root, keeping track of the transformation and attribute state (in case of inherited values) and drawing the SVG graphics primitives (including paths) you encounter. Of course some things like symbol definitions and filters would be more complicated to implement, but for basic geometry & styling this should not take longer than a few hours to implement given the features provided by PDFKit.

like image 28
Florian Ledermann Avatar answered Nov 12 '22 22:11

Florian Ledermann