Skip to content Skip to sidebar Skip to footer

How To Know The Pdf Version Using Javascript + Html5

I want to check the version of the PDF using client side available options (javascript, HTML5 or others) as i have to validate that PDF version must be 1.3 and if the PDF version i

Solution 1:

You'll have to use the FileReader API.

Begin by creating a FileReader and registering an onload handler.

As a the first few bytes of a PDF should be plain ASCII we can read them as text and should look something like %PDF-1.3 (where 1.3 is the version) so if we get bytes 5 to 7 (Yes I know it says file.slice(5, 8) I didn't write the spec ;)).

Should be fairly straight forward to drop in to form validation. Left as an exercise for the reader

CAVEAT

This is a simple example and will work for x.x versions but would fail to read versions x.xx properly without modification.

const pdfUpload = document.getElementById('pdfUpload');
const pdfVersion = document.getElementById('pdfVersion');

// wait for input to be "changed"
pdfUpload.addEventListener('change', (e) => {

  // grab the selected filelet [file] = e.target.files;

  if (!file) return;

  // use a FileReaderlet reader = newFileReader();
  reader.onload = (e) => {
  
    // read the contentlet versionString = e.target.result;
    pdfVersion.innerText = versionString;
  };

  // PDF file should start something like '%PDF-1.3'// grab only the bits we need
  reader.readAsText(file.slice(5, 8));
})
<inputtype="file"id="pdfUpload" /><div>PDF Version: <spanid="pdfVersion"></span></div>

Post a Comment for "How To Know The Pdf Version Using Javascript + Html5"