Open WInzelrac opened 2 years ago
I have exactly them same problem. On PC it works, but on mobile devices its just blank. Did you manage to find a fix @WInzelrac?
First of all, if you get error like this "Uncaught (in promise) ReferenceError: jsPDF is not defined" use
window.jsPDF = window.jspdf.jsPDF;
var pdf = new jsPDF('l', 'pt', [PDF_Width, PDF_Height]);
instead of
var pdf = new jsPDF('', 'pt', 'a4');
Each browser compiles js code differently. So you could get this error on mobile browser, and don't get it on PC browser.
The rest of your code works for me on PC and mobile devices. Also set css style cursor: pointer; to button, it really helps on iphone.
Also check my code, that works on PC and Mobile.
var dwn_btn = document.getElementById('matrix-download');
if (typeof(dwn_btn) != 'undefined' && dwn_btn != null)
{
document.getElementById ("matrix-download").addEventListener ("click", download_pdf, false);
}
function download_pdf(){
$('.matrix-btn-loader').addClass('active');
var clone = $('#matrix-wrap').clone().addClass('to_download' ).prop('id', '');
$('body').append(clone);
var html2canvas_options = {
scale: 2,
onclone: function(canvas) {
$('.matrix-wrap.to_download').remove();
}
};
var HTML_Width = $('.matrix-wrap.to_download').width();
var HTML_Height = $('.matrix-wrap.to_download').height();
var top_left_margin = 30;
var PDF_Width = HTML_Width + (top_left_margin * 2);
var PDF_Height = HTML_Height + (top_left_margin * 2);
window.jsPDF = window.jspdf.jsPDF;
html2canvas($('.matrix-wrap.to_download')[0], html2canvas_options).then(function (canvas) {
var imgData = canvas.toDataURL("image/jpeg");
var pdf = new jsPDF('l', 'pt', [PDF_Width, PDF_Height]);
pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, HTML_Width, HTML_Height);
$('.matrix-btn-loader').removeClass('active');
pdf.save(`matrix3.pdf`);
});
}
Hey, I've used HTML2canvas and jsPDF to create a pdf of a form and save it. My problem is : It works perfectly fine on a computer, but on mobile devices like iPad or phones (apple or android), the canvas created is blank. It has the size the canvas should have but totally empty. So, I wonder if their is a config i've missed or anything. Thanks.
Here is my code :