-
Notifications
You must be signed in to change notification settings - Fork 1
/
export-to-pdf.html
157 lines (147 loc) · 5.83 KB
/
export-to-pdf.html
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
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Export to PDF</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
</head>
<body>
<div class="container-fluid text-center" style="margin-top: 50px;">
<div class="row">
<div id="content" class="col-md-10 col-md-offset-1">
<table class="table table-bordered" id="myTable">
<tr>
<td><b>Sr. No.</b></td>
<td><b>Name</b></td>
<td><b>Email</b> </td>
<td><b>Mobile</b></td>
<td><b>State</b></td>
<td><b>Country</b></td>
</tr>
<tr>
<td>1</td>
<td>Test</td>
<td>test@test.test</td>
<td>0123456789</td>
<td>Stest</td>
<td>Ctest</td>
</tr>
<tr>
<td>2</td>
<td>Test2</td>
<td>test2@test.test</td>
<td>0123456789</td>
<td>Stest</td>
<td>Ctest</td>
</tr>
<tr>
<td>3</td>
<td>Test3</td>
<td>test3@test.test</td>
<td>0123456789</td>
<td>Stest</td>
<td>Ctest</td>
</tr>
<tr>
<td>4</td>
<td>Test4</td>
<td>test4@test.test</td>
<td>0123456789</td>
<td>Stest</td>
<td>Ctest</td>
</tr>
<tr>
<td>5</td>
<td>Test5</td>
<td>test5@test.test</td>
<td>0123456789</td>
<td>Stest</td>
<td>Ctest</td>
</tr>
</table>
</div>
<div class="col-md-4 col-md-offset-4">
<button id="cmd" class="form-control btn btn-primary">Generate PDF</button>
</div>
</div>
</div>
<script>
(function() {
var
form = $('#content'),
cache_width = form.width();
// a4 = [595.28, 841.89]; // for a4 size paper width and height
$('#cmd').on('click', function() {
$('body').scrollTop(0);
createPDF();
});
//create pdf
function createPDF() {
getCanvas().then(function(canvas) {
var
img = canvas.toDataURL("image/png"),
doc = new jsPDF('l', 'px', 'a4');
var width = doc.internal.pageSize.width;
// var height = doc.internal.pageSize.height;
doc.addImage(img, 'JPEG', 0, 0, width, 0);
doc.save('html-to-pdf.pdf');
// form.width(cache_width);
});
}
// create canvas object
function getCanvas() {
// form.width((a4[0] * 1.33333) - 80).css('max-width', 'none');
return html2canvas(form, {
imageTimeout: 2000,
removeContainer: true
});
}
}());
</script>
<script>
(function($) {
$.fn.html2canvas = function(options) {
var date = new Date(),
$message = null,
timeoutTimer = false,
timer = date.getTime();
html2canvas.logging = options && options.logging;
html2canvas.Preload(this[0], $.extend({
complete: function(images) {
var queue = html2canvas.Parse(this[0], images, options),
$canvas = $(html2canvas.Renderer(queue, options)),
finishTime = new Date();
$canvas.css({ position: 'absolute', left: 0, top: 0 }).appendTo(document.body);
$canvas.siblings().toggle();
$(window).click(function() {
if (!$canvas.is(':visible')) {
$canvas.toggle().siblings().toggle();
throwMessage("Canvas Render visible");
} else {
$canvas.siblings().toggle();
$canvas.toggle();
throwMessage("Canvas Render hidden");
}
});
throwMessage('Screenshot created in ' + ((finishTime.getTime() - timer) / 1000) + " seconds<br />", 4000);
}
}, options));
function throwMessage(msg, duration) {
window.clearTimeout(timeoutTimer);
timeoutTimer = window.setTimeout(function() {
$message.fadeOut(function() {
$message.remove();
});
}, duration || 2000);
if ($message)
$message.remove();
$message = $('<div ></div>').html(msg).hide().fadeIn().appendTo('body');
}
};
})(jQuery);
</script>
</body>
</html>