I am working on uploading a document (PDF) that mostly contains an image taken onsite from a Mobile Phone Camera, with help of the CamScanner(CS) app.
I could be able to handle the image file upload to reduce the size of the image, yet I could not be able to reduce the size of the PDF document before uploading.
As suggested by several sources, one method is to resize the image file, generate the PDF file with jspdf and start uploading the PDF file.
// Compress image and convert to pdf
function resizeBase64Img(base64, width, height) {
var canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
var context = canvas.getContext("2d");
var deferred = $.Deferred();
$("<img/>").attr("src", "data:image/gif;base64," + base64).on('load',function() {
context.scale(width/this.width, height/this.height);
context.drawImage(this, 0, 0);
deferred.resolve($("<img/>").attr("src", canvas.toDataURL()));
});
return deferred.promise();
}
//select my image in base64
var imageStr64 = "/9j/4RiDRXhpZgAATU0AKgA...";
//resize image, add image and create the pdf
resizeBase64Img(imageStr64, 1000, 1000).then(function(newImg){
var doc = new jsPDF();
doc.addImage($(newImg).attr('src'), 15, 90, 180,180);
doc.save('mypdf.pdf');
});
However, I want to clearly separate between uploading the image and uploading the PDF document but I could not find any open source library or answer except several online PDF compressors such as this one.
Is it Possible to doing that? Please kindly suggest and help me on that, thanks.