2014-06-01 21:29:54 +00:00
|
|
|
/*globals RGBColor, DOMParser, jsPDF*/
|
|
|
|
/*jslint eqeq:true, vars:true*/
|
2014-04-08 05:13:54 +00:00
|
|
|
/*
|
|
|
|
* svgToPdf.js
|
2014-06-01 21:29:54 +00:00
|
|
|
*
|
|
|
|
* Copyright 2012-2014 Florian Hülsmann <fh@cbix.de>
|
|
|
|
* Copyright 2014 Ben Gribaudo <www.bengribaudo.com>
|
|
|
|
*
|
2014-04-08 05:13:54 +00:00
|
|
|
* This script is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-06-01 21:29:54 +00:00
|
|
|
*
|
2014-04-08 05:13:54 +00:00
|
|
|
* This script is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
2014-06-01 21:29:54 +00:00
|
|
|
*
|
2014-04-08 05:13:54 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this file. If not, see <http://www.gnu.org/licenses/>.
|
2014-06-01 21:29:54 +00:00
|
|
|
*
|
2014-04-08 05:13:54 +00:00
|
|
|
*/
|
|
|
|
|
2014-06-01 21:29:54 +00:00
|
|
|
(function(jsPDFAPI, undef) {
|
|
|
|
'use strict';
|
2014-04-08 05:13:54 +00:00
|
|
|
|
|
|
|
var pdfSvgAttr = {
|
|
|
|
// allowed attributes. all others are removed from the preview.
|
|
|
|
g: ['stroke', 'fill', 'stroke-width'],
|
2014-06-01 21:29:54 +00:00
|
|
|
line: ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width'],
|
2014-04-08 05:13:54 +00:00
|
|
|
rect: ['x', 'y', 'width', 'height', 'stroke', 'fill', 'stroke-width'],
|
|
|
|
ellipse: ['cx', 'cy', 'rx', 'ry', 'stroke', 'fill', 'stroke-width'],
|
|
|
|
circle: ['cx', 'cy', 'r', 'stroke', 'fill', 'stroke-width'],
|
2014-06-01 21:29:54 +00:00
|
|
|
text: ['x', 'y', 'font-size', 'font-family', 'text-anchor', 'font-weight', 'font-style', 'fill']
|
2014-04-08 05:13:54 +00:00
|
|
|
};
|
|
|
|
|
2014-06-01 21:29:54 +00:00
|
|
|
var attributeIsNotEmpty = function (node, attr) {
|
|
|
|
var attVal = attr ? node.getAttribute(attr) : node;
|
|
|
|
return attVal !== '' && attVal !== null;
|
|
|
|
};
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2014-06-01 21:29:54 +00:00
|
|
|
var nodeIs = function (node, possible) {
|
|
|
|
return possible.indexOf(node.tagName.toLowerCase()) > -1;
|
|
|
|
};
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2014-06-01 21:29:54 +00:00
|
|
|
var removeAttributes = function(node, attributes) {
|
|
|
|
var toRemove = [];
|
|
|
|
[].forEach.call(node.attributes, function(a) {
|
|
|
|
if (attributeIsNotEmpty(a) && attributes.indexOf(a.name.toLowerCase()) == -1) {
|
|
|
|
toRemove.push(a.name);
|
2014-04-08 05:13:54 +00:00
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
toRemove.forEach(function(a) {
|
|
|
|
node.removeAttribute(a.name);
|
|
|
|
});
|
|
|
|
};
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2014-06-01 21:29:54 +00:00
|
|
|
var svgElementToPdf = function(element, pdf, options) {
|
|
|
|
// pdf is a jsPDF object
|
|
|
|
//console.log("options =", options);
|
|
|
|
var remove = (options.removeInvalid == undef ? false : options.removeInvalid);
|
|
|
|
var k = (options.scale == undef ? 1.0 : options.scale);
|
|
|
|
var colorMode = null;
|
|
|
|
[].forEach.call(element.children, function(node) {
|
|
|
|
//console.log("passing: ", node);
|
2014-04-08 05:13:54 +00:00
|
|
|
var hasFillColor = false;
|
|
|
|
var hasStrokeColor = false;
|
2014-06-01 21:29:54 +00:00
|
|
|
var fillRGB;
|
|
|
|
if(nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle', 'text'])) {
|
|
|
|
var fillColor = node.getAttribute('fill');
|
|
|
|
if(attributeIsNotEmpty(fillColor)) {
|
|
|
|
fillRGB = new RGBColor(fillColor);
|
|
|
|
if(fillRGB.ok) {
|
2014-04-08 05:13:54 +00:00
|
|
|
hasFillColor = true;
|
|
|
|
colorMode = 'F';
|
|
|
|
} else {
|
|
|
|
colorMode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
if(nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle'])) {
|
|
|
|
if(hasFillColor) {
|
|
|
|
pdf.setFillColor(fillRGB.r, fillRGB.g, fillRGB.b);
|
|
|
|
}
|
|
|
|
if(attributeIsNotEmpty(node, 'stroke-width')) {
|
|
|
|
pdf.setLineWidth(k * parseInt(node.getAttribute('stroke-width'), 10));
|
|
|
|
}
|
|
|
|
var strokeColor = node.getAttribute('stroke');
|
|
|
|
if(attributeIsNotEmpty(strokeColor)) {
|
|
|
|
var strokeRGB = new RGBColor(strokeColor);
|
|
|
|
if(strokeRGB.ok) {
|
|
|
|
hasStrokeColor = true;
|
|
|
|
pdf.setDrawColor(strokeRGB.r, strokeRGB.g, strokeRGB.b);
|
|
|
|
if(colorMode == 'F') {
|
|
|
|
colorMode = 'FD';
|
|
|
|
} else {
|
|
|
|
colorMode = null;
|
|
|
|
}
|
2014-04-08 05:13:54 +00:00
|
|
|
} else {
|
2014-06-01 21:29:54 +00:00
|
|
|
colorMode = null;
|
2014-04-08 05:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
switch(node.tagName.toLowerCase()) {
|
|
|
|
case 'svg':
|
|
|
|
case 'a':
|
|
|
|
case 'g':
|
|
|
|
svgElementToPdf(node, pdf, options);
|
|
|
|
removeAttributes(node, pdfSvgAttr.g);
|
|
|
|
break;
|
2014-04-08 05:13:54 +00:00
|
|
|
case 'line':
|
|
|
|
pdf.line(
|
2014-06-01 21:29:54 +00:00
|
|
|
k*parseInt(node.getAttribute('x1'), 10),
|
|
|
|
k*parseInt(node.getAttribute('y1'), 10),
|
|
|
|
k*parseInt(node.getAttribute('x2'), 10),
|
|
|
|
k*parseInt(node.getAttribute('y2'), 10)
|
|
|
|
);
|
|
|
|
removeAttributes(node, pdfSvgAttr.line);
|
2014-04-08 05:13:54 +00:00
|
|
|
break;
|
|
|
|
case 'rect':
|
|
|
|
pdf.rect(
|
2014-06-01 21:29:54 +00:00
|
|
|
k*parseInt(node.getAttribute('x'), 10),
|
|
|
|
k*parseInt(node.getAttribute('y'), 10),
|
|
|
|
k*parseInt(node.getAttribute('width'), 10),
|
|
|
|
k*parseInt(node.getAttribute('height'), 10),
|
|
|
|
colorMode
|
|
|
|
);
|
|
|
|
removeAttributes(node, pdfSvgAttr.rect);
|
2014-04-08 05:13:54 +00:00
|
|
|
break;
|
|
|
|
case 'ellipse':
|
|
|
|
pdf.ellipse(
|
2014-06-01 21:29:54 +00:00
|
|
|
k*parseInt(node.getAttribute('cx'), 10),
|
|
|
|
k*parseInt(node.getAttribute('cy'), 10),
|
|
|
|
k*parseInt(node.getAttribute('rx'), 10),
|
|
|
|
k*parseInt(node.getAttribute('ry'), 10),
|
|
|
|
colorMode
|
|
|
|
);
|
|
|
|
removeAttributes(node, pdfSvgAttr.ellipse);
|
2014-04-08 05:13:54 +00:00
|
|
|
break;
|
|
|
|
case 'circle':
|
|
|
|
pdf.circle(
|
2014-06-01 21:29:54 +00:00
|
|
|
k*parseInt(node.getAttribute('cx'), 10),
|
|
|
|
k*parseInt(node.getAttribute('cy'), 10),
|
|
|
|
k*parseInt(node.getAttribute('r'), 10),
|
|
|
|
colorMode
|
|
|
|
);
|
|
|
|
removeAttributes(node, pdfSvgAttr.circle);
|
2014-04-08 05:13:54 +00:00
|
|
|
break;
|
|
|
|
case 'text':
|
2014-06-01 21:29:54 +00:00
|
|
|
if(node.hasAttribute('font-family')) {
|
|
|
|
switch((node.getAttribute('font-family') || '').toLowerCase()) {
|
|
|
|
case 'serif': pdf.setFont('times'); break;
|
|
|
|
case 'monospace': pdf.setFont('courier'); break;
|
2014-04-08 05:13:54 +00:00
|
|
|
default:
|
2014-06-01 21:29:54 +00:00
|
|
|
node.setAttribute('font-family', 'sans-serif');
|
2014-04-08 05:13:54 +00:00
|
|
|
pdf.setFont('helvetica');
|
|
|
|
}
|
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
if(hasFillColor) {
|
|
|
|
pdf.setTextColor(fillRGB.r, fillRGB.g, fillRGB.b);
|
2014-04-08 05:13:54 +00:00
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
var fontType = "";
|
|
|
|
if(node.hasAttribute('font-weight')) {
|
|
|
|
if(node.getAttribute('font-weight') == "bold") {
|
2014-04-08 05:13:54 +00:00
|
|
|
fontType = "bold";
|
|
|
|
} else {
|
|
|
|
node.removeAttribute('font-weight');
|
|
|
|
}
|
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
if(node.hasAttribute('font-style')) {
|
|
|
|
if(node.getAttribute('font-style') == "italic") {
|
2014-04-08 05:13:54 +00:00
|
|
|
fontType += "italic";
|
|
|
|
} else {
|
|
|
|
node.removeAttribute('font-style');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pdf.setFontType(fontType);
|
2014-06-01 21:29:54 +00:00
|
|
|
var pdfFontSize = 16;
|
|
|
|
if(node.hasAttribute('font-size')) {
|
|
|
|
pdfFontSize = parseInt(node.getAttribute('font-size'), 10);
|
2014-04-08 05:13:54 +00:00
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
var box = node.getBBox();
|
2014-04-08 05:13:54 +00:00
|
|
|
//FIXME: use more accurate positioning!!
|
2014-06-01 21:29:54 +00:00
|
|
|
var x, y, xOffset = 0;
|
|
|
|
if(node.hasAttribute('text-anchor')) {
|
|
|
|
switch(node.getAttribute('text-anchor')) {
|
|
|
|
case 'end': xOffset = box.width; break;
|
|
|
|
case 'middle': xOffset = box.width / 2; break;
|
|
|
|
case 'start': break;
|
|
|
|
case 'default': node.setAttribute('text-anchor', 'start'); break;
|
2014-04-08 05:13:54 +00:00
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
x = parseInt(node.getAttribute('x'), 10) - xOffset;
|
|
|
|
y = parseInt(node.getAttribute('y'), 10);
|
2014-04-08 05:13:54 +00:00
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
//console.log("fontSize:", pdfFontSize, "text:", node.textContent);
|
2014-04-08 05:13:54 +00:00
|
|
|
pdf.setFontSize(pdfFontSize).text(
|
2014-06-01 21:29:54 +00:00
|
|
|
k * x,
|
|
|
|
k * y,
|
|
|
|
node.textContent
|
|
|
|
);
|
|
|
|
removeAttributes(node, pdfSvgAttr.text);
|
2014-04-08 05:13:54 +00:00
|
|
|
break;
|
2014-06-01 21:29:54 +00:00
|
|
|
//TODO: image
|
2014-04-08 05:13:54 +00:00
|
|
|
default:
|
|
|
|
if (remove) {
|
2014-06-01 21:29:54 +00:00
|
|
|
console.log("can't translate to pdf:", node);
|
|
|
|
node.parentNode.removeChild(node);
|
2014-04-08 05:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return pdf;
|
2014-06-01 21:29:54 +00:00
|
|
|
};
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2014-06-01 21:29:54 +00:00
|
|
|
jsPDFAPI.addSVG = function(element, x, y, options) {
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2014-06-01 21:29:54 +00:00
|
|
|
options = (options === undef ? {} : options);
|
|
|
|
options.x_offset = x;
|
|
|
|
options.y_offset = y;
|
2014-04-08 05:13:54 +00:00
|
|
|
|
2014-06-01 21:29:54 +00:00
|
|
|
if (typeof element === 'string') {
|
|
|
|
element = new DOMParser().parseFromString(element, 'text/xml').documentElement;
|
2014-04-08 05:13:54 +00:00
|
|
|
}
|
2014-06-01 21:29:54 +00:00
|
|
|
svgElementToPdf(element, this, options);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
}(jsPDF.API));
|