title: Reduce PDF Document Size before Uploading in JavaScript?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>As suggested by several sources, one method is to resize the image file, generate the PDF file with <a href="https://parall.ax/products/jspdf" rel="noreferrer">jspdf</a> and start uploading the PDF file.</p>
  
  <pre><code>// 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();
      $("&lt;img/&gt;").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($("&lt;img/&gt;").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');
  });
  </code></pre>
  
  <p>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 <a href="https://pdfcandy.com/result/10b2ea70.html" rel="noreferrer">this one</a>.</p>
  
  <p>Is it Possible to doing that? Please kindly suggest and help me on that, thanks.</p>