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.
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.
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 path
s) 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With