PDFJsAnnotations Uncaught (in promise) SyntaxError: Unexpected end of JSON input

2.1k Views Asked by At

tried to load saved json from local storage using JSON.parse(localStorage.getItem("CanvaData"))

  ready() {
    console.log("Plugin initialized successfully");

    var arr = JSON.parse(localStorage.getItem("CanvaData"));
    console.log(arr)
    // localStorage.clear();
    pdf.loadFromJSON(arr)
  },

but as you can see it's throwing SyntaxError: Unexpected end of JSON input .

how to resolve this issue?

1

There are 1 best solutions below

0
On BEST ANSWER

So after investigating for hours i found the issue that causes this.

i saved the json into local storage as a single string

localStorage.setItem('CanvaData', JSON.stringify(pdf.serializePdf()));

now i changed it to save the data as json to local storage

localStorage.setItem('CanvaData', pdf.serializePdf());

after that i modified the load from json as the following:

ready() {
    console.log("Plugin initialized successfully");

     var data123 = localStorage.getItem('CanvaData');
     pdf.loadFromJSON(JSON.parse(data123))
  },

Fixed.