@@ -22,11 +22,84 @@ MIT license.
2222 } )
2323 }
2424
25+ /**
26+ * @todo Remove duplication
27+ */
28+ function loadBinaryResource ( url ) {
29+ const req = new XMLHttpRequest ( )
30+ req . open ( 'GET' , url , false )
31+ // XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
32+ req . overrideMimeType ( 'text\/plain; charset=x-user-defined' )
33+ req . send ( null )
34+ if ( req . status !== 200 ) {
35+ throw new Error ( 'Unable to load file ' + url )
36+ }
37+ return req . responseText
38+ }
39+
40+ /**
41+ * This is a cheap hack that inserts the SVG into a browser node to get it
42+ * to clean it up for us.
43+ * @param {string } svg The SVG code
44+ * @return {object } A DOM object
45+ */
46+ var domClean = function ( svg ) {
47+ var div = document . createElement ( 'div' ) ;
48+ div . innerHTML = svg
49+ return div . querySelector ( 'svg' )
50+ }
51+
52+ var getSvgFromInput = function ( svg ) {
53+ /**
54+ * If it's a string, then treat it as either raw SVG content
55+ */
56+ var svgContent
57+ if ( typeof svg === 'string' ) {
58+ // If it's SVG
59+ if ( svg . indexOf ( '<svg' ) !== - 1 ) {
60+ svgContent = domClean ( svg )
61+ } else {
62+ // Otherwise it's a URL
63+ svgContent = domClean ( loadBinaryResource ( svg ) )
64+ }
65+ }
66+
67+ if ( typeof svg === 'object' ) {
68+ svgContent = svg
69+ }
70+
71+ // Everything should be an object now
72+ svgContent = fixDimensions ( svgContent )
73+
74+ return svgContent . outerHTML
75+ }
76+
77+ /**
78+ * Fix dimensions, if there's any 100%s in there that won't render
79+ * @todo Change that to the page size
80+ *
81+ * @param {object } dom DOM node of an SVG
82+ * @return {object } Fixed DOM node
83+ */
84+ var fixDimensions = function ( dom ) {
85+ if ( dom . getAttributeNode ( 'width' ) . nodeValue == '100%' ) {
86+ var width = document . createAttribute ( 'width' )
87+ width . value = '100'
88+ dom . setAttributeNode ( width )
89+ }
90+ if ( dom . getAttributeNode ( 'height' ) . nodeValue == '100%' ) {
91+ var width = document . createAttribute ( 'height' )
92+ width . value = '100'
93+ dom . setAttributeNode ( width )
94+ }
95+ return dom
96+ }
97+
2598 /**
2699 * Render an SVG to the document.
27100 *
28- * @param {Mixed } svg Pass either a DOM object, or a string containing
29- * the SVG content itself .
101+ * @param {Mixed } svg Pass either a DOM object, a string containing the
102+ * SVG content, or a path to the SVG you want to add .
30103 * @param {Object } options Options are passed in as an object:
31104 * * x - The x position
32105 * * y - The y position
@@ -50,18 +123,8 @@ MIT license.
50123 * })
51124 */
52125 jsPDFAPI . svg = function ( svg , options ) {
53- /**
54- * If it's a string, then treat it as raw SVG content
55- */
56- var svgContent
57- if ( typeof svg === 'string' ) {
58- var div = document . createElement ( 'div' ) ;
59- div . innerHTML = svg
60- svgContent = div . querySelector ( 'svg' ) . outerHTML
61- }
62- if ( typeof svg === 'object' ) {
63- svgContent = svg . outerHTML
64- }
126+ var svgContent = getSvgFromInput ( svg )
127+
65128 if ( typeof options === 'undefined' ) {
66129 options = { }
67130 }
0 commit comments