jspdf.plugin.svgToPdf.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*globals RGBColor, DOMParser, jsPDF*/
  2. /*jslint eqeq:true, vars:true*/
  3. /*
  4. * svgToPdf.js
  5. *
  6. * Copyright 2012-2014 Florian Hülsmann <fh@cbix.de>
  7. * Copyright 2014 Ben Gribaudo <www.bengribaudo.com>
  8. *
  9. * This script is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This script is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this file. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. (function(jsPDFAPI, undef) {
  24. 'use strict';
  25. var pdfSvgAttr = {
  26. // allowed attributes. all others are removed from the preview.
  27. g: ['stroke', 'fill', 'stroke-width'],
  28. line: ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width'],
  29. rect: ['x', 'y', 'width', 'height', 'stroke', 'fill', 'stroke-width'],
  30. ellipse: ['cx', 'cy', 'rx', 'ry', 'stroke', 'fill', 'stroke-width'],
  31. circle: ['cx', 'cy', 'r', 'stroke', 'fill', 'stroke-width'],
  32. text: ['x', 'y', 'font-size', 'font-family', 'text-anchor', 'font-weight', 'font-style', 'fill']
  33. };
  34. var attributeIsNotEmpty = function (node, attr) {
  35. var attVal = attr ? node.getAttribute(attr) : node;
  36. return attVal !== '' && attVal !== null;
  37. };
  38. var nodeIs = function (node, possible) {
  39. return possible.indexOf(node.tagName.toLowerCase()) > -1;
  40. };
  41. var removeAttributes = function(node, attributes) {
  42. var toRemove = [];
  43. [].forEach.call(node.attributes, function(a) {
  44. if (attributeIsNotEmpty(a) && attributes.indexOf(a.name.toLowerCase()) == -1) {
  45. toRemove.push(a.name);
  46. }
  47. });
  48. toRemove.forEach(function(a) {
  49. node.removeAttribute(a.name);
  50. });
  51. };
  52. var svgElementToPdf = function(element, pdf, options) {
  53. // pdf is a jsPDF object
  54. //console.log("options =", options);
  55. var remove = (options.removeInvalid == undef ? false : options.removeInvalid);
  56. var k = (options.scale == undef ? 1.0 : options.scale);
  57. var colorMode = null;
  58. [].forEach.call(element.children, function(node) {
  59. //console.log("passing: ", node);
  60. var hasFillColor = false;
  61. var hasStrokeColor = false;
  62. var fillRGB;
  63. if(nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle', 'text'])) {
  64. var fillColor = node.getAttribute('fill');
  65. if(attributeIsNotEmpty(fillColor)) {
  66. fillRGB = new RGBColor(fillColor);
  67. if(fillRGB.ok) {
  68. hasFillColor = true;
  69. colorMode = 'F';
  70. } else {
  71. colorMode = null;
  72. }
  73. }
  74. }
  75. if(nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle'])) {
  76. if(hasFillColor) {
  77. pdf.setFillColor(fillRGB.r, fillRGB.g, fillRGB.b);
  78. }
  79. if(attributeIsNotEmpty(node, 'stroke-width')) {
  80. pdf.setLineWidth(k * parseInt(node.getAttribute('stroke-width'), 10));
  81. }
  82. var strokeColor = node.getAttribute('stroke');
  83. if(attributeIsNotEmpty(strokeColor)) {
  84. var strokeRGB = new RGBColor(strokeColor);
  85. if(strokeRGB.ok) {
  86. hasStrokeColor = true;
  87. pdf.setDrawColor(strokeRGB.r, strokeRGB.g, strokeRGB.b);
  88. if(colorMode == 'F') {
  89. colorMode = 'FD';
  90. } else {
  91. colorMode = null;
  92. }
  93. } else {
  94. colorMode = null;
  95. }
  96. }
  97. }
  98. switch(node.tagName.toLowerCase()) {
  99. case 'svg':
  100. case 'a':
  101. case 'g':
  102. svgElementToPdf(node, pdf, options);
  103. removeAttributes(node, pdfSvgAttr.g);
  104. break;
  105. case 'line':
  106. pdf.line(
  107. k*parseInt(node.getAttribute('x1'), 10),
  108. k*parseInt(node.getAttribute('y1'), 10),
  109. k*parseInt(node.getAttribute('x2'), 10),
  110. k*parseInt(node.getAttribute('y2'), 10)
  111. );
  112. removeAttributes(node, pdfSvgAttr.line);
  113. break;
  114. case 'rect':
  115. pdf.rect(
  116. k*parseInt(node.getAttribute('x'), 10),
  117. k*parseInt(node.getAttribute('y'), 10),
  118. k*parseInt(node.getAttribute('width'), 10),
  119. k*parseInt(node.getAttribute('height'), 10),
  120. colorMode
  121. );
  122. removeAttributes(node, pdfSvgAttr.rect);
  123. break;
  124. case 'ellipse':
  125. pdf.ellipse(
  126. k*parseInt(node.getAttribute('cx'), 10),
  127. k*parseInt(node.getAttribute('cy'), 10),
  128. k*parseInt(node.getAttribute('rx'), 10),
  129. k*parseInt(node.getAttribute('ry'), 10),
  130. colorMode
  131. );
  132. removeAttributes(node, pdfSvgAttr.ellipse);
  133. break;
  134. case 'circle':
  135. pdf.circle(
  136. k*parseInt(node.getAttribute('cx'), 10),
  137. k*parseInt(node.getAttribute('cy'), 10),
  138. k*parseInt(node.getAttribute('r'), 10),
  139. colorMode
  140. );
  141. removeAttributes(node, pdfSvgAttr.circle);
  142. break;
  143. case 'text':
  144. if(node.hasAttribute('font-family')) {
  145. switch((node.getAttribute('font-family') || '').toLowerCase()) {
  146. case 'serif': pdf.setFont('times'); break;
  147. case 'monospace': pdf.setFont('courier'); break;
  148. default:
  149. node.setAttribute('font-family', 'sans-serif');
  150. pdf.setFont('helvetica');
  151. }
  152. }
  153. if(hasFillColor) {
  154. pdf.setTextColor(fillRGB.r, fillRGB.g, fillRGB.b);
  155. }
  156. var fontType = "";
  157. if(node.hasAttribute('font-weight')) {
  158. if(node.getAttribute('font-weight') == "bold") {
  159. fontType = "bold";
  160. } else {
  161. node.removeAttribute('font-weight');
  162. }
  163. }
  164. if(node.hasAttribute('font-style')) {
  165. if(node.getAttribute('font-style') == "italic") {
  166. fontType += "italic";
  167. } else {
  168. node.removeAttribute('font-style');
  169. }
  170. }
  171. pdf.setFontType(fontType);
  172. var pdfFontSize = 16;
  173. if(node.hasAttribute('font-size')) {
  174. pdfFontSize = parseInt(node.getAttribute('font-size'), 10);
  175. }
  176. var box = node.getBBox();
  177. //FIXME: use more accurate positioning!!
  178. var x, y, xOffset = 0;
  179. if(node.hasAttribute('text-anchor')) {
  180. switch(node.getAttribute('text-anchor')) {
  181. case 'end': xOffset = box.width; break;
  182. case 'middle': xOffset = box.width / 2; break;
  183. case 'start': break;
  184. case 'default': node.setAttribute('text-anchor', 'start'); break;
  185. }
  186. x = parseInt(node.getAttribute('x'), 10) - xOffset;
  187. y = parseInt(node.getAttribute('y'), 10);
  188. }
  189. //console.log("fontSize:", pdfFontSize, "text:", node.textContent);
  190. pdf.setFontSize(pdfFontSize).text(
  191. k * x,
  192. k * y,
  193. node.textContent
  194. );
  195. removeAttributes(node, pdfSvgAttr.text);
  196. break;
  197. //TODO: image
  198. default:
  199. if (remove) {
  200. console.log("can't translate to pdf:", node);
  201. node.parentNode.removeChild(node);
  202. }
  203. }
  204. });
  205. return pdf;
  206. };
  207. jsPDFAPI.addSVG = function(element, x, y, options) {
  208. options = (options === undef ? {} : options);
  209. options.x_offset = x;
  210. options.y_offset = y;
  211. if (typeof element === 'string') {
  212. element = new DOMParser().parseFromString(element, 'text/xml').documentElement;
  213. }
  214. svgElementToPdf(element, this, options);
  215. return this;
  216. };
  217. }(jsPDF.API));