JSPDF - How to delete blank last page in pdf download
up vote
1
down vote
favorite
I am using jspdf here.
I am getting a blank page while downloading the pdf.
When you click submit1 button charts will generate,and when you press download button those chart will be downloaded as pdf.
Everything is working but a blank page is coming at last of the page.
Here is my live demo in plunker. also here I have the html and javascript code
html
<div><img id="logo" style="width:30%" src="https://www.gstatic.com/webp/gallery3/3.png"/>dddddddd</div>
<input type="button" id="download" value="download" /> click to console
<div><button id="button1" class="button1">submit1</button></div>
<p id="bottom-content">footer</p>
script
$(document).ready(function() {
var index = 0;
var id = ;
$('#button1').on('click', function() {
$('body').append($("<div id='chart" + index + "'></div>"));
Highcharts.chart('chart' + index, {
title: {
text: 'Chart-'+index+''
},
series: [{
data: [1, 2, 3]
}]
});
var temp = "chart" + index + "";
console.log(temp);
id.push(temp);
index++;
});
$('#download').on('click', function() {
console.log(id);
var doc = new jsPDF('portrait', 'pt', 'a4', true);
var elementHandler = {
'#ignorePDF': function(element, renderer) {
return true;
}
};
var source = document.getElementById("logo");
doc.fromHTML(source, 15, 15, {
'width': 200,
'elementHandlers': elementHandler
});
var DOMURL = window.URL || window.webkitURL || window;
var elements = id;
// console.log(xx);
for (let i in elements) {
let svg = document.querySelectorAll('svg');
let canvas = document.createElement('canvas');
let canvasIE = document.createElement('canvas');
let context = canvas.getContext('2d');
let data1 = (new XMLSerializer()).serializeToString(svg[i]);
canvg(canvas, data1);
let svgBlob = new Blob([data1], {
type: 'image/svg+xml;charset=utf-8'
});
let url = DOMURL.createObjectURL(svgBlob);
let img = new Image();
img.src = url;
img.onload = function() {
context.canvas.width = $("#"+elements[i]).find('svg').width();
context.canvas.height = $("#"+elements[i]).find('svg').height();
context.drawImage(img, 0, 0);
// freeing up the memory as image is drawn to canvas
DOMURL.revokeObjectURL(url);
var dataUrl;
if (isIEBrowser()) { // Check of IE browser
var svg = $(elements[i]).highcharts().container.innerHTML;
canvg(canvasIE, svg[i]);
dataUrl = canvasIE.toDataURL('image/JPEG');
} else {
dataUrl = canvas.toDataURL('image/jpeg');
}
doc.addImage(dataUrl, 'JPEG', 20, 150, 560, 350);
let bottomContent = document.getElementById("bottom-content");
doc.fromHTML(bottomContent, 15, 700, {
'width': 560,
'elementHandlers': elementHandler
});
let logo = document.getElementById("logo");
doc.fromHTML(logo, 15, 200, {
'width': 560,
'elementHandlers': elementHandler
});
doc.addPage();
};
}
setTimeout(function() {
doc.save('TestChart"'+index+'".pdf');
}, 1000);
});
});
function isIEBrowser() {
var ieBrowser;
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv:11./)) // Internet Explorer
{
ieBrowser = true;
} else //Other browser
{
console.log('Other Browser');
ieBrowser = false;
}
return ieBrowser;
};
javascript html jspdf
add a comment |
up vote
1
down vote
favorite
I am using jspdf here.
I am getting a blank page while downloading the pdf.
When you click submit1 button charts will generate,and when you press download button those chart will be downloaded as pdf.
Everything is working but a blank page is coming at last of the page.
Here is my live demo in plunker. also here I have the html and javascript code
html
<div><img id="logo" style="width:30%" src="https://www.gstatic.com/webp/gallery3/3.png"/>dddddddd</div>
<input type="button" id="download" value="download" /> click to console
<div><button id="button1" class="button1">submit1</button></div>
<p id="bottom-content">footer</p>
script
$(document).ready(function() {
var index = 0;
var id = ;
$('#button1').on('click', function() {
$('body').append($("<div id='chart" + index + "'></div>"));
Highcharts.chart('chart' + index, {
title: {
text: 'Chart-'+index+''
},
series: [{
data: [1, 2, 3]
}]
});
var temp = "chart" + index + "";
console.log(temp);
id.push(temp);
index++;
});
$('#download').on('click', function() {
console.log(id);
var doc = new jsPDF('portrait', 'pt', 'a4', true);
var elementHandler = {
'#ignorePDF': function(element, renderer) {
return true;
}
};
var source = document.getElementById("logo");
doc.fromHTML(source, 15, 15, {
'width': 200,
'elementHandlers': elementHandler
});
var DOMURL = window.URL || window.webkitURL || window;
var elements = id;
// console.log(xx);
for (let i in elements) {
let svg = document.querySelectorAll('svg');
let canvas = document.createElement('canvas');
let canvasIE = document.createElement('canvas');
let context = canvas.getContext('2d');
let data1 = (new XMLSerializer()).serializeToString(svg[i]);
canvg(canvas, data1);
let svgBlob = new Blob([data1], {
type: 'image/svg+xml;charset=utf-8'
});
let url = DOMURL.createObjectURL(svgBlob);
let img = new Image();
img.src = url;
img.onload = function() {
context.canvas.width = $("#"+elements[i]).find('svg').width();
context.canvas.height = $("#"+elements[i]).find('svg').height();
context.drawImage(img, 0, 0);
// freeing up the memory as image is drawn to canvas
DOMURL.revokeObjectURL(url);
var dataUrl;
if (isIEBrowser()) { // Check of IE browser
var svg = $(elements[i]).highcharts().container.innerHTML;
canvg(canvasIE, svg[i]);
dataUrl = canvasIE.toDataURL('image/JPEG');
} else {
dataUrl = canvas.toDataURL('image/jpeg');
}
doc.addImage(dataUrl, 'JPEG', 20, 150, 560, 350);
let bottomContent = document.getElementById("bottom-content");
doc.fromHTML(bottomContent, 15, 700, {
'width': 560,
'elementHandlers': elementHandler
});
let logo = document.getElementById("logo");
doc.fromHTML(logo, 15, 200, {
'width': 560,
'elementHandlers': elementHandler
});
doc.addPage();
};
}
setTimeout(function() {
doc.save('TestChart"'+index+'".pdf');
}, 1000);
});
});
function isIEBrowser() {
var ieBrowser;
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv:11./)) // Internet Explorer
{
ieBrowser = true;
} else //Other browser
{
console.log('Other Browser');
ieBrowser = false;
}
return ieBrowser;
};
javascript html jspdf
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using jspdf here.
I am getting a blank page while downloading the pdf.
When you click submit1 button charts will generate,and when you press download button those chart will be downloaded as pdf.
Everything is working but a blank page is coming at last of the page.
Here is my live demo in plunker. also here I have the html and javascript code
html
<div><img id="logo" style="width:30%" src="https://www.gstatic.com/webp/gallery3/3.png"/>dddddddd</div>
<input type="button" id="download" value="download" /> click to console
<div><button id="button1" class="button1">submit1</button></div>
<p id="bottom-content">footer</p>
script
$(document).ready(function() {
var index = 0;
var id = ;
$('#button1').on('click', function() {
$('body').append($("<div id='chart" + index + "'></div>"));
Highcharts.chart('chart' + index, {
title: {
text: 'Chart-'+index+''
},
series: [{
data: [1, 2, 3]
}]
});
var temp = "chart" + index + "";
console.log(temp);
id.push(temp);
index++;
});
$('#download').on('click', function() {
console.log(id);
var doc = new jsPDF('portrait', 'pt', 'a4', true);
var elementHandler = {
'#ignorePDF': function(element, renderer) {
return true;
}
};
var source = document.getElementById("logo");
doc.fromHTML(source, 15, 15, {
'width': 200,
'elementHandlers': elementHandler
});
var DOMURL = window.URL || window.webkitURL || window;
var elements = id;
// console.log(xx);
for (let i in elements) {
let svg = document.querySelectorAll('svg');
let canvas = document.createElement('canvas');
let canvasIE = document.createElement('canvas');
let context = canvas.getContext('2d');
let data1 = (new XMLSerializer()).serializeToString(svg[i]);
canvg(canvas, data1);
let svgBlob = new Blob([data1], {
type: 'image/svg+xml;charset=utf-8'
});
let url = DOMURL.createObjectURL(svgBlob);
let img = new Image();
img.src = url;
img.onload = function() {
context.canvas.width = $("#"+elements[i]).find('svg').width();
context.canvas.height = $("#"+elements[i]).find('svg').height();
context.drawImage(img, 0, 0);
// freeing up the memory as image is drawn to canvas
DOMURL.revokeObjectURL(url);
var dataUrl;
if (isIEBrowser()) { // Check of IE browser
var svg = $(elements[i]).highcharts().container.innerHTML;
canvg(canvasIE, svg[i]);
dataUrl = canvasIE.toDataURL('image/JPEG');
} else {
dataUrl = canvas.toDataURL('image/jpeg');
}
doc.addImage(dataUrl, 'JPEG', 20, 150, 560, 350);
let bottomContent = document.getElementById("bottom-content");
doc.fromHTML(bottomContent, 15, 700, {
'width': 560,
'elementHandlers': elementHandler
});
let logo = document.getElementById("logo");
doc.fromHTML(logo, 15, 200, {
'width': 560,
'elementHandlers': elementHandler
});
doc.addPage();
};
}
setTimeout(function() {
doc.save('TestChart"'+index+'".pdf');
}, 1000);
});
});
function isIEBrowser() {
var ieBrowser;
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv:11./)) // Internet Explorer
{
ieBrowser = true;
} else //Other browser
{
console.log('Other Browser');
ieBrowser = false;
}
return ieBrowser;
};
javascript html jspdf
I am using jspdf here.
I am getting a blank page while downloading the pdf.
When you click submit1 button charts will generate,and when you press download button those chart will be downloaded as pdf.
Everything is working but a blank page is coming at last of the page.
Here is my live demo in plunker. also here I have the html and javascript code
html
<div><img id="logo" style="width:30%" src="https://www.gstatic.com/webp/gallery3/3.png"/>dddddddd</div>
<input type="button" id="download" value="download" /> click to console
<div><button id="button1" class="button1">submit1</button></div>
<p id="bottom-content">footer</p>
script
$(document).ready(function() {
var index = 0;
var id = ;
$('#button1').on('click', function() {
$('body').append($("<div id='chart" + index + "'></div>"));
Highcharts.chart('chart' + index, {
title: {
text: 'Chart-'+index+''
},
series: [{
data: [1, 2, 3]
}]
});
var temp = "chart" + index + "";
console.log(temp);
id.push(temp);
index++;
});
$('#download').on('click', function() {
console.log(id);
var doc = new jsPDF('portrait', 'pt', 'a4', true);
var elementHandler = {
'#ignorePDF': function(element, renderer) {
return true;
}
};
var source = document.getElementById("logo");
doc.fromHTML(source, 15, 15, {
'width': 200,
'elementHandlers': elementHandler
});
var DOMURL = window.URL || window.webkitURL || window;
var elements = id;
// console.log(xx);
for (let i in elements) {
let svg = document.querySelectorAll('svg');
let canvas = document.createElement('canvas');
let canvasIE = document.createElement('canvas');
let context = canvas.getContext('2d');
let data1 = (new XMLSerializer()).serializeToString(svg[i]);
canvg(canvas, data1);
let svgBlob = new Blob([data1], {
type: 'image/svg+xml;charset=utf-8'
});
let url = DOMURL.createObjectURL(svgBlob);
let img = new Image();
img.src = url;
img.onload = function() {
context.canvas.width = $("#"+elements[i]).find('svg').width();
context.canvas.height = $("#"+elements[i]).find('svg').height();
context.drawImage(img, 0, 0);
// freeing up the memory as image is drawn to canvas
DOMURL.revokeObjectURL(url);
var dataUrl;
if (isIEBrowser()) { // Check of IE browser
var svg = $(elements[i]).highcharts().container.innerHTML;
canvg(canvasIE, svg[i]);
dataUrl = canvasIE.toDataURL('image/JPEG');
} else {
dataUrl = canvas.toDataURL('image/jpeg');
}
doc.addImage(dataUrl, 'JPEG', 20, 150, 560, 350);
let bottomContent = document.getElementById("bottom-content");
doc.fromHTML(bottomContent, 15, 700, {
'width': 560,
'elementHandlers': elementHandler
});
let logo = document.getElementById("logo");
doc.fromHTML(logo, 15, 200, {
'width': 560,
'elementHandlers': elementHandler
});
doc.addPage();
};
}
setTimeout(function() {
doc.save('TestChart"'+index+'".pdf');
}, 1000);
});
});
function isIEBrowser() {
var ieBrowser;
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv:11./)) // Internet Explorer
{
ieBrowser = true;
} else //Other browser
{
console.log('Other Browser');
ieBrowser = false;
}
return ieBrowser;
};
javascript html jspdf
javascript html jspdf
edited 6 hours ago
Andrew Li
619419
619419
asked 7 hours ago
carreankush
567
567
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You can get the number of pages present in the document and then delete the last page
The following line will give you the number of pages present in the document:
var pageCount = doc.internal.getNumberOfPages();
The following code can be used to delete last page from the document:
doc.deletePage(pageCount)
Hope this helps!!
New contributor
Till here var pageCount = doc.internal.getNumberOfPages(); alert(pageCount); its showing correct,but when I use doc.deletePage(pageCount) , alert is showing only 1 always and page is showing only one that is blank,If you can please edit in my plunker it will be very helpful for me
– carreankush
4 hours ago
here : embed.plnkr.co/rdfCej
– Suhas NM
4 hours ago
did you get the answer you were looking for?
– Suhas NM
4 hours ago
Yes Thanks for your help,I need one more help that I need to display the logo in all pages of pdf while downloading,now its showing in first page.
– carreankush
4 hours ago
you can add a custom function for adding new page and in that function handle all the header/footer requirements for that page... something like -- addNewPage(pageNo) { //code to add image goes here }
– Suhas NM
4 hours ago
|
show 7 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can get the number of pages present in the document and then delete the last page
The following line will give you the number of pages present in the document:
var pageCount = doc.internal.getNumberOfPages();
The following code can be used to delete last page from the document:
doc.deletePage(pageCount)
Hope this helps!!
New contributor
Till here var pageCount = doc.internal.getNumberOfPages(); alert(pageCount); its showing correct,but when I use doc.deletePage(pageCount) , alert is showing only 1 always and page is showing only one that is blank,If you can please edit in my plunker it will be very helpful for me
– carreankush
4 hours ago
here : embed.plnkr.co/rdfCej
– Suhas NM
4 hours ago
did you get the answer you were looking for?
– Suhas NM
4 hours ago
Yes Thanks for your help,I need one more help that I need to display the logo in all pages of pdf while downloading,now its showing in first page.
– carreankush
4 hours ago
you can add a custom function for adding new page and in that function handle all the header/footer requirements for that page... something like -- addNewPage(pageNo) { //code to add image goes here }
– Suhas NM
4 hours ago
|
show 7 more comments
up vote
0
down vote
accepted
You can get the number of pages present in the document and then delete the last page
The following line will give you the number of pages present in the document:
var pageCount = doc.internal.getNumberOfPages();
The following code can be used to delete last page from the document:
doc.deletePage(pageCount)
Hope this helps!!
New contributor
Till here var pageCount = doc.internal.getNumberOfPages(); alert(pageCount); its showing correct,but when I use doc.deletePage(pageCount) , alert is showing only 1 always and page is showing only one that is blank,If you can please edit in my plunker it will be very helpful for me
– carreankush
4 hours ago
here : embed.plnkr.co/rdfCej
– Suhas NM
4 hours ago
did you get the answer you were looking for?
– Suhas NM
4 hours ago
Yes Thanks for your help,I need one more help that I need to display the logo in all pages of pdf while downloading,now its showing in first page.
– carreankush
4 hours ago
you can add a custom function for adding new page and in that function handle all the header/footer requirements for that page... something like -- addNewPage(pageNo) { //code to add image goes here }
– Suhas NM
4 hours ago
|
show 7 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can get the number of pages present in the document and then delete the last page
The following line will give you the number of pages present in the document:
var pageCount = doc.internal.getNumberOfPages();
The following code can be used to delete last page from the document:
doc.deletePage(pageCount)
Hope this helps!!
New contributor
You can get the number of pages present in the document and then delete the last page
The following line will give you the number of pages present in the document:
var pageCount = doc.internal.getNumberOfPages();
The following code can be used to delete last page from the document:
doc.deletePage(pageCount)
Hope this helps!!
New contributor
New contributor
answered 5 hours ago
Suhas NM
224
224
New contributor
New contributor
Till here var pageCount = doc.internal.getNumberOfPages(); alert(pageCount); its showing correct,but when I use doc.deletePage(pageCount) , alert is showing only 1 always and page is showing only one that is blank,If you can please edit in my plunker it will be very helpful for me
– carreankush
4 hours ago
here : embed.plnkr.co/rdfCej
– Suhas NM
4 hours ago
did you get the answer you were looking for?
– Suhas NM
4 hours ago
Yes Thanks for your help,I need one more help that I need to display the logo in all pages of pdf while downloading,now its showing in first page.
– carreankush
4 hours ago
you can add a custom function for adding new page and in that function handle all the header/footer requirements for that page... something like -- addNewPage(pageNo) { //code to add image goes here }
– Suhas NM
4 hours ago
|
show 7 more comments
Till here var pageCount = doc.internal.getNumberOfPages(); alert(pageCount); its showing correct,but when I use doc.deletePage(pageCount) , alert is showing only 1 always and page is showing only one that is blank,If you can please edit in my plunker it will be very helpful for me
– carreankush
4 hours ago
here : embed.plnkr.co/rdfCej
– Suhas NM
4 hours ago
did you get the answer you were looking for?
– Suhas NM
4 hours ago
Yes Thanks for your help,I need one more help that I need to display the logo in all pages of pdf while downloading,now its showing in first page.
– carreankush
4 hours ago
you can add a custom function for adding new page and in that function handle all the header/footer requirements for that page... something like -- addNewPage(pageNo) { //code to add image goes here }
– Suhas NM
4 hours ago
Till here var pageCount = doc.internal.getNumberOfPages(); alert(pageCount); its showing correct,but when I use doc.deletePage(pageCount) , alert is showing only 1 always and page is showing only one that is blank,If you can please edit in my plunker it will be very helpful for me
– carreankush
4 hours ago
Till here var pageCount = doc.internal.getNumberOfPages(); alert(pageCount); its showing correct,but when I use doc.deletePage(pageCount) , alert is showing only 1 always and page is showing only one that is blank,If you can please edit in my plunker it will be very helpful for me
– carreankush
4 hours ago
here : embed.plnkr.co/rdfCej
– Suhas NM
4 hours ago
here : embed.plnkr.co/rdfCej
– Suhas NM
4 hours ago
did you get the answer you were looking for?
– Suhas NM
4 hours ago
did you get the answer you were looking for?
– Suhas NM
4 hours ago
Yes Thanks for your help,I need one more help that I need to display the logo in all pages of pdf while downloading,now its showing in first page.
– carreankush
4 hours ago
Yes Thanks for your help,I need one more help that I need to display the logo in all pages of pdf while downloading,now its showing in first page.
– carreankush
4 hours ago
you can add a custom function for adding new page and in that function handle all the header/footer requirements for that page... something like -- addNewPage(pageNo) { //code to add image goes here }
– Suhas NM
4 hours ago
you can add a custom function for adding new page and in that function handle all the header/footer requirements for that page... something like -- addNewPage(pageNo) { //code to add image goes here }
– Suhas NM
4 hours ago
|
show 7 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203120%2fjspdf-how-to-delete-blank-last-page-in-pdf-download%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password