forked from haochi/svg_to_pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg_to_pdf.js
41 lines (37 loc) · 1.09 KB
/
svg_to_pdf.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
/**
* @param {SVGElement} svg
* @param {Function} callback
* @param {jsPDF} callback.pdf
* */
function svg_to_pdf(svg, callback) {
svgAsDataUri(svg, {}, function(svg_uri) {
var image = document.createElement('img');
image.src = svg_uri;
image.onload = function() {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var doc = new jsPDF('portrait', 'pt');
var dataUrl;
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0, image.width, image.height);
dataUrl = canvas.toDataURL('image/jpeg');
doc.addImage(dataUrl, 'JPEG', 0, 0, image.width, image.height);
callback(doc);
}
});
}
/**
* @param {string} name Name of the file
* @param {string} dataUriString
*/
function download_pdf(name, dataUriString) {
var link = document.createElement('a');
link.addEventListener('click', function(ev) {
link.href = dataUriString;
link.download = name;
document.body.removeChild(link);
}, false);
document.body.appendChild(link);
link.click();
}