I am currently working on converting HTML content into a real image on the client's side. After conducting research on canvas2base64, canvas2base64, and html2base64 on Stack Overflow, I have found that the generated images are always based on base64 code rather than a real picture.
While the quality of the generated image is satisfactory, I am facing an issue when trying to insert it into a PDF using jsPDF as it does not support base64 images.
My current setup includes using jquery.js, jspdf.js, html2canvas.js, and FileSaver.js.
I am looking for a solution to generate a genuine image in formats such as .png or .jpg from a canvas without relying on base64 code. Ideally, I would like to save this image in the user's localStorage for easy reference and use in my code.
The following code aims to generate an image and insert it into a PDF with the dimensions of a DIN A4 sheet:
function print() {
//const filename = 'Loveletter_.pdf';
html2canvas(document.querySelector('.finishedLetter')).then(function(canvas) {
var pdf = new jsPDF('p', 'mm', 'a4');
$("#test_me").attr("src", canvas.toDataURL("image/png", 1.0));
var ImgForPDF = document.getElementById('test_me').getAttribute('src');
pdf.addImage(ImgForPDF, 'PNG', 0, 0, 211, 298);
});
pdf.save('Loveletter_.pdf');
}