8

Some PDF files won't render in Chrome browser but will render fine in Firefox. All files render fine in all browsers if emeded directly.

<object id="content-view" :data="content_view.base64" type="application/pdf"></object>

The confusing part is that the problem is only for some files and not all. Files are stored in a folder that is not public and that's why they are served as base64 for the user to view.

screenshot

I tested the problematic files by using online base64 decoders and I get the same result. Rendered in FF, not rendered in Chrome.

I cannot share any of the PDF files. They are all from the same source, scanned from the same device, PDF version 1.4, 4 pages.

I have tried:

  • using iframe, embed and object (same result)
  • unblocking Insecure content in Chrome site settings
  • opening and re-saving in Adobe Acrobat
  • using online PDF analyzers to see if any problems present (none found)
kopz
  • 738
  • 11
  • 20

3 Answers3

1

I had the exact same issue. I noticed that some of the PDFs that are more than 1MB isn't loading.

I found a solution here: Open base64 encoded pdf file using javascript. Issue with file size larger than 2 MB

Need to change the Base64 string to a BLOB. Then create a URL to be used for iframe src.

Here is the code:

base64PDFToBlobUrl( base64 ) {
  const binStr = atob( base64 );
  const len = binStr.length;
  const arr = new Uint8Array(len);
  for (let i = 0; i < len; i++) {
    arr[ i ] = binStr.charCodeAt( i );
  }
  const blob =  new Blob( [ arr ], { type: 'application/pdf' } );
  const url = URL.createObjectURL( blob );
  return url;
}

This will return a url that you can put inside your iframe, embed or object src. This way, you can still load the PDF in a page without opening it in another tab.

Eldon
  • 131
  • 1
  • 4
0

Rather than use the browsers native PDF renderer, you could use the JS one written by Mozilla.

ViewerJS provides a nice interface to this and if you want to embed it fullsize in a page, then you can place it in an iframe and control that with iFrame-resizer.

David Bradshaw
  • 11,859
  • 3
  • 41
  • 70
0

Instead of opening the PDF file in HTML object element, open it in a new window using Blob URL.

Please refer

Creating a BLOB from a Base64 string in JavaScript

to convert Base64 to Blob

Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26