forked from parallax/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvg.js
More file actions
150 lines (140 loc) · 3.89 KB
/
Copy pathsvg.js
File metadata and controls
150 lines (140 loc) · 3.89 KB
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
/** @preserve
jsPDF svg plugin
Copyright (c) 2016 James Hall
MIT license.
*/
;(function (jsPDFAPI) {
'use strict'
// IE does not support outerHTML on SVGElement
if (typeof SVGElement === 'object' && !SVGElement.prototype.outerHTML) {
Object.defineProperty(SVGElement.prototype, 'outerHTML', {
get: function () {
var $node
var $temp
$temp = document.createElement('div')
$node = this.cloneNode(true)
$temp.appendChild($node)
return $temp.innerHTML
},
enumerable: false,
configurable: true
})
}
/**
* @todo Remove duplication
*/
function loadBinaryResource (url) {
const req = new XMLHttpRequest()
req.open('GET', url, false)
// XHR binary charset opt by Marcus Granado 2006 [http://mgran.blogspot.com]
req.overrideMimeType('text\/plain; charset=x-user-defined')
req.send(null)
if (req.status !== 200) {
throw new Error('Unable to load file ' + url)
}
return req.responseText
}
/**
* This is a cheap hack that inserts the SVG into a browser node to get it
* to clean it up for us.
* @param {string} svg The SVG code
* @return {object} A DOM object
*/
var domClean = function (svg) {
var div = document.createElement('div');
div.innerHTML = svg
return div.querySelector('svg')
}
var getSvgFromInput = function (svg) {
/**
* If it's a string, then treat it as either raw SVG content
*/
var svgContent
if (typeof svg === 'string') {
// If it's SVG
if (svg.indexOf('<svg') !== -1) {
svgContent = domClean(svg)
} else {
// Otherwise it's a URL
svgContent = domClean(loadBinaryResource(svg))
}
}
if (typeof svg === 'object') {
svgContent = svg
}
// Everything should be an object now
svgContent = fixDimensions(svgContent)
return svgContent.outerHTML
}
/**
* Fix dimensions, if there's any 100%s in there that won't render
* @todo Change that to the page size
*
* @param {object} dom DOM node of an SVG
* @return {object} Fixed DOM node
*/
var fixDimensions = function (dom) {
if (dom.getAttributeNode('width').nodeValue == '100%') {
var width = document.createAttribute('width')
width.value = '10'
dom.setAttributeNode(width)
}
if (dom.getAttributeNode('height').nodeValue == '100%') {
var height = document.createAttribute('height')
height.value = '10'
dom.setAttributeNode(height)
}
return dom
}
/**
* Render an SVG to the document.
*
* @param {Mixed} svg Pass either a DOM object, a string containing the
* SVG content, or a path to the SVG you want to add.
* @param {Object} options Options are passed in as an object:
* * x - The x position
* * y - The y position
* @return {jsPDF}
* @function
* @name svg
* @example
* var doc = new jsPDF()
* doc.svg(document.querySelector('.svg'))
*
* // or to draw at a particular point on the page
* doc.svg(document.querySelector('.svg'), { x: 10, y: 10 })
*
* // You can also draw SVG content directly
* doc.svg(`<svg width="120" height="120" viewBox="0 0 120 120"
* xmlns="http://www.w3.org/2000/svg">
* <rect x="0" y="0" width="100" height="100"/>
* </svg>`, {
* x: 20,
* y: 20
* })
*/
jsPDFAPI.svg = function (svg, options) {
var svgContent = getSvgFromInput(svg)
if (typeof options === 'undefined') {
options = {}
}
if (options.x === undefined) {
options.x = 0
}
if (options.y === undefined) {
options.y = 0
}
var c = this.canvas
var ctx = c.getContext('2d')
ctx.ignoreClearRect = true
canvg(c, svgContent, {
ignoreMouse: true,
ignoreAnimation: true,
ignoreDimensions: true,
useCORS: true,
offsetX: options.x,
offsetY: options.y
})
return this
}
})(jsPDF.API)