1

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 is 1.3 than only will upload it in the server.

I know that there are various server side option available for identifying the pdf version but i want the client side available option so user don't need to re-upload the entire file again if it's not Version 1.3

Thanks in advance.

1 Answer 1

4

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 file
  let [file] = e.target.files;

  if (!file) return;

  // use a FileReader
  let reader = new FileReader();
  reader.onload = (e) => {
  
    // read the content
    let 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));
})
<input type="file" id="pdfUpload" />
<div>PDF Version: <span id="pdfVersion"></span></div>

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks.. it works.. actually after research i came to know that reader API have another method readAsBinaryString so which one is safer to use ? readAsText or readAsBinaryString ?
@user2746557 According to the MDN article readAsBinaryString has not been "standardized` and has actually been deprecated as of July 2012. readAsText also allows you to specify the text encoding and assume UTF-8 unless specified.
@user2746557 upon further reading at w3.org readAsBinaryString will only allow ASCII encoding which would probably be adequate for your needs too but still deprecated.
@user2746557 Please consider accepting this answer if it solves your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.