Home > Software engineering >  Failed to load PDF but can see the data
Failed to load PDF but can see the data

Time:11-05

I am getting this error that Chrome failed to load the PDF document. I can see the content of the data is the console window so I have the data being returned I just not sure why it will not display? If I File.WriteAllBytes to disk it will open fine so it maybe something with the creating the new Blob

Failed to load PDF document.

Debugger

ts code

 printItems(versionKeys: string[]): JQueryPromise<any> {
        console.log('printItems');
        $.ajax({
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(versionKeys),
            url: this.apiUrls.PrintTemplates,
            success: function (data, status, xhr) {
                console.log('printItems');
                console.log(data);
                let blob = new Blob([data.Content], { type: data.ContentType });
                var url = URL.createObjectURL(blob);
                console.log(url);
                window.open(url);
                console.log('success');
            }
        });

        return;
    }

Error

enter image description here

CodePudding user response:

I converted the base64 string to a bytes

  var binary_string = window.atob(data.Content)
  var len = data.Content.length;
  var bytes = new Uint8Array(len);
  for (var i = 0; i < len; i  ) {
      bytes[i] = binary_string.charCodeAt(i);
  }
  let blob = new Blob([bytes.buffer], { type: data.ContentType })

  var url = URL.createObjectURL(blob);

  window.open(url);
  • Related