forked from cbix/svgToPdf.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsvgToPdf.js
224 lines (219 loc) · 8.65 KB
/
svgToPdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* svgToPdf.js
*
* Copyright 2012-2014 Florian Hülsmann <[email protected]>
* Copyright 2014 Ben Gribaudo <[email protected]>
*
* 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.
*
* 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.
*
* 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/>.
*
*/
var pdfSvgAttr = {
// allowed attributes. all others are removed from the preview.
g: ['stroke', 'fill', 'stroke-width'],
line: ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width'],
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'],
text: ['x', 'y', 'font-size', 'font-family', 'text-anchor', 'font-weight', 'font-style', 'fill']
};
var svgElementToPdf = function(element, pdf, options) {
// pdf is a jsPDF object
//console.log("options =", options);
var remove = (typeof(options.removeInvalid) == 'undefined' ? false : options.removeInvalid);
var k = (typeof(options.scale) == 'undefined' ? 1.0 : options.scale);
var colorMode = null;
$(element).children().each(function(i, node) {
//console.log("passing: ", node);
var n = $(node);
var hasFillColor = false;
var hasStrokeColor = false;
if(n.is('g,line,rect,ellipse,circle,text')) {
var fillColor = n.attr('fill');
if(typeof(fillColor) != 'undefined') {
var fillRGB = new RGBColor(fillColor);
if(fillRGB.ok) {
hasFillColor = true;
colorMode = 'F';
} else {
colorMode = null;
}
}
}
if(n.is('g,line,rect,ellipse,circle')) {
if(hasFillColor) {
pdf.setFillColor(fillRGB.r, fillRGB.g, fillRGB.b);
}
if(typeof(n.attr('stroke-width')) != 'undefined') {
pdf.setLineWidth(k * parseInt(n.attr('stroke-width')));
}
var strokeColor = n.attr('stroke');
if(typeof(strokeColor) != 'undefined') {
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;
}
} else {
colorMode = null;
}
}
}
switch(n.get(0).tagName.toLowerCase()) {
case 'svg':
case 'a':
case 'g':
svgElementToPdf(node, pdf, options);
$.each(node.attributes, function(i, a) {
if(typeof(a) != 'undefined' && pdfSvgAttr.g.indexOf(a.name.toLowerCase()) == -1) {
node.removeAttribute(a.name);
}
});
break;
case 'line':
pdf.line(
k*parseInt(n.attr('x1')),
k*parseInt(n.attr('y1')),
k*parseInt(n.attr('x2')),
k*parseInt(n.attr('y2'))
);
$.each(node.attributes, function(i, a) {
if(typeof(a) != 'undefined' && pdfSvgAttr.line.indexOf(a.name.toLowerCase()) == -1) {
node.removeAttribute(a.name);
}
});
break;
case 'rect':
pdf.rect(
k*parseInt(n.attr('x')),
k*parseInt(n.attr('y')),
k*parseInt(n.attr('width')),
k*parseInt(n.attr('height')),
colorMode
);
$.each(node.attributes, function(i, a) {
if(typeof(a) != 'undefined' && pdfSvgAttr.rect.indexOf(a.name.toLowerCase()) == -1) {
node.removeAttribute(a.name);
}
});
break;
case 'ellipse':
pdf.ellipse(
k*parseInt(n.attr('cx')),
k*parseInt(n.attr('cy')),
k*parseInt(n.attr('rx')),
k*parseInt(n.attr('ry')),
colorMode
);
$.each(node.attributes, function(i, a) {
if(typeof(a) != 'undefined' && pdfSvgAttr.ellipse.indexOf(a.name.toLowerCase()) == -1) {
node.removeAttribute(a.name);
}
});
break;
case 'circle':
pdf.circle(
k*parseInt(n.attr('cx')),
k*parseInt(n.attr('cy')),
k*parseInt(n.attr('r')),
colorMode
);
$.each(node.attributes, function(i, a) {
if(typeof(a) != 'undefined' && pdfSvgAttr.circle.indexOf(a.name.toLowerCase()) == -1) {
node.removeAttribute(a.name);
}
});
break;
case 'text':
if(node.hasAttribute('font-family')) {
switch(n.attr('font-family').toLowerCase()) {
case 'serif': pdf.setFont('times'); break;
case 'monospace': pdf.setFont('courier'); break;
default:
n.attr('font-family', 'sans-serif');
pdf.setFont('helvetica');
}
}
if(hasFillColor) {
pdf.setTextColor(fillRGB.r, fillRGB.g, fillRGB.b);
}
var fontType = "";
if(node.hasAttribute('font-weight')) {
if(n.attr('font-weight') == "bold") {
fontType = "bold";
} else {
node.removeAttribute('font-weight');
}
}
if(node.hasAttribute('font-style')) {
if(n.attr('font-style') == "italic") {
fontType += "italic";
} else {
node.removeAttribute('font-style');
}
}
pdf.setFontType(fontType);
var pdfFontSize = 16;
if(node.hasAttribute('font-size')) {
pdfFontSize = parseInt(n.attr('font-size'));
}
var box = node.getBBox();
//FIXME: use more accurate positioning!!
var x, y, xOffset = 0;
if(node.hasAttribute('text-anchor')) {
switch(n.attr('text-anchor')) {
case 'end': xOffset = box.width; break;
case 'middle': xOffset = box.width / 2; break;
case 'start': break;
case 'default': n.attr('text-anchor', 'start');
}
x = parseInt(n.attr('x')) - xOffset;
y = parseInt(n.attr('y'));
}
//console.log("fontSize:", pdfFontSize, "text:", n.text());
pdf.setFontSize(pdfFontSize).text(
k * x,
k * y,
n.text()
);
$.each(node.attributes, function(i, a) {
if(typeof(a) != 'undefined' && pdfSvgAttr.text.indexOf(a.name.toLowerCase()) == -1) {
node.removeAttribute(a.name);
}
});
break;
//TODO: image
default:
if(remove) {
console.log("can't translate to pdf:", node);
n.remove();
}
}
});
return pdf;
};
(function(jsPDFAPI) {
'use strict';
jsPDFAPI.addSVG = function(element, x, y, options) {
'use strict'
options = (typeof(options) == 'undefined' ? {} : options);
options.x_offset = x;
options.y_offset = y;
svgElementToPdf(element, this, options);
return this;
};
})(jsPDF.API);