Import full SVG using PDFKit

Does anyone know if it is possible to import full SVG into a PDFKit document? From the documents, I see that it has full support for SVG, there are methods for drawing paths, etc., but I do not see a method for importing a full SVG document.

+7
javascript svg pdfkit
source share
2 answers

Unfortunately, with V0.7 (November 2014), PDFKit does not support drawing SVG content to PDF. Hovever, most of the building blocks are (functions covering most of the attributes of SVG presentations + SVG path parser), so the implementation of this question will be related to moving the SVG document tree from the root, tracking the conversion state and attribute (in case of inherited values) and drawing graphic SVG primitives (including path s) that you encounter. Of course, some things, such as symbol designations and filters, will be more difficult to implement, but for basic geometry and styling, it should not take more than a few hours to implement these functions provided by PDFKit.

+2
source share

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

If it is not convenient to implement your own interface, this one is quite easy to use (sample code from 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 follows:

doc [PDFDocument] = PDF document created using PDFKit

svg [SVGElement or string] = SVG object or XML code

x, y [number] = position to add SVG to

Check out the demo here .

+1
source share

All Articles