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.
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.
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.
链接地址: http://www.djcxy.com/p/21896.html上一篇: Facebook Audience Network Proguard设置
下一篇: 用PDFKit导入完整的SVG