Skip to content

Commit 82ef087

Browse files
committed
Add support for SVG documents with 100% width/height
1 parent beb6533 commit 82ef087

157 files changed

Lines changed: 28537 additions & 16 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

karma.conf.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ module.exports = (config) => {
4848
pattern: 'tests/**/reference/*.pdf',
4949
included: false,
5050
served: true
51+
}, {
52+
pattern: 'tests/**/input/*.svg',
53+
included: false,
54+
served: true
5155
}
5256
],
5357

plugins/addsvg.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* ========= DEPRECATED ==========
33
*
4-
* This plugin is deprecated.
4+
* This plugin is deprecated!
5+
*
56
jsPDF SVG plugin
67
Copyright (c) 2012 Willow Systems Corporation, willow-systems.com
78
*/

plugins/svg.js

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

saucelabs.karma.conf.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ module.exports = (config) => {
101101
pattern: 'tests/**/reference/*.pdf',
102102
included: false,
103103
served: true
104+
}, {
105+
pattern: 'tests/**/input/*.svg',
106+
included: false,
107+
served: true
104108
}
105109
],
106110

tests/svg/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Thanks to the canvg project for the test SVGs.

tests/svg/icon.spec.js

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/svg/input/1.svg

Lines changed: 55 additions & 0 deletions
Loading

tests/svg/input/10.svg

Lines changed: 12 additions & 0 deletions
Loading

tests/svg/input/11.svg

Lines changed: 13 additions & 0 deletions
Loading

tests/svg/input/12.svg

Lines changed: 13 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)