Load pdf on foreign url with pdf.js
Asked Answered
A

5

12

I am trying to load pdf from another server to the viewer of pdf.js in my server.I got error "PDF.js v1.4.20 (build: b15f335) Message: file origin does not match viewer's"

I already checked many answer, many of them said that pass the pdf url through a proxy like:- link

After searching a lot i found that they release a new patch in which they have lock down any CDR request, correct me if i am wrong:-Here is the link

but in their user manual they specified that it is possible here is the link

I tried all method but not able to enable CDR on my server and many methods didn't work.

Please help me to resolve this issue. My Basic idea is to show pdf(which is hosted on 3rd party server) on my pdf reader(that i made it from pdf.js).

Alphonse answered 22/5, 2016 at 18:48 Comment(2)
Yes, it's locked down for demo viewer. People who wants take a risk of content spoofing can remove this protection, in this case customized viewer will be able to displays unrelated/prank/offensive content under the viewer's domain name.Lemley
See also #37365162Lemley
A
23

I resolved this issue by comment this lines in viewer.js

if (fileOrigin !== viewerOrigin) {
throw new Error('file origin does not match viewer\'s');
}

and use proxy like this. http://192.168.0.101/web/viewer.html?file=https://cors-anywhere.herokuapp.com/pathofpdf.pdf

Alphonse answered 22/5, 2016 at 20:33 Comment(5)
Or you can edit the condition to allow for the file's origin.Favour
I have no "if (fileOrigin !== viewerOrigin)" in my viewer.js fileFawkes
please explain on how to use proxy.Zannini
@Fawkes it is if (origin !== viewerOrigin && protocol !== "blob:")Adjacent
Is there a way without editing the viewer.js file?Shindig
H
8

Add your domain/origin to HOSTED_VIEWER_ORIGINS array

Hottentot answered 27/11, 2019 at 7:1 Comment(0)
G
0

I resolved this issue by adding this line in viewer.js

    var LOCAL_AUTO_DETECT_ORIGIN = window.location.origin;

  var HOSTED_VIEWER_ORIGINS = ['null', 'http://mozilla.github.io', 'https://mozilla.github.io'];

  HOSTED_VIEWER_ORIGINS.push(LOCAL_AUTO_DETECT_ORIGIN);
Gatecrasher answered 27/9, 2021 at 7:16 Comment(0)
A
0

The problem in my case was the link wasnt in https while the site is secured

Alo answered 27/9, 2021 at 14:34 Comment(0)
S
0

pdfjs respect CORS settings. Do the following

  1. Go to viewer.js file and find the location of HOSTED_VIEWER_ORIGINS. Below that line add your domain to the array HOSTED_VIEWER_ORIGINS like this
    const LOCAL_AUTO_DETECT_ORIGIN = window.location.origin;
    HOSTED_VIEWER_ORIGINS.push(LOCAL_AUTO_DETECT_ORIGIN);
  1. if your file is hosted n AWS S3 bucket, then set CORS policy on the bucket to allow all your domains read files from that bucket

That should solve your prblem

Stalnaker answered 4/12, 2022 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.