问题
I am trying to convert pdf pages to canvas using pdf.js. I used the answer in Render .pdf to single Canvas using pdf.js and ImageData by K3N to achieve this. The code is avaliable here Fiddle1.
The problem is that this seems to work with certain pdfs only.
For example the code works fine for http://arxiv.org/pdf/1207.0102v2.pdf in Fiddle2.
However, when I tried the same code for http://infolab.stanford.edu/pub/papers/google.pdf in Fiddle3 it failed to work.
Why is this happening and can it be fixed?
回答1:
It is supposed to work with all pdf files, unless they are corrupted. The error you have here is:
XMLHttpRequest cannot load http://infolab.stanford.edu/pub/papers/google.pdf. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.
In other words, you can't load it this way because http://infolab.stanford.edu/pub/papers/google.pdf doesn't let you do to so. When you will have it on your server and load it with proper response headers, it will most probably work.
For more information about this error, refer to Why am I seeing an "origin is not allowed by Access-Control-Allow-Origin" error here?
If you don't host these files, you can pipe them through a proxy (which can be a third-party app or your server). For example, Ivan Žužak, developed urlreq–a tool which does exactly what we need in this situation.
Instead of using the direct link to pdf file, use Ivan's proxy url:
http://urlreq.appspot.com/req?method=GET&url=http%3A%2F%2Finfolab.stanford.edu%2Fpub%2Fpapers%2Fgoogle.pdf
JSFIDDLE
来源:https://stackoverflow.com/questions/32909940/does-pdf-js-only-work-with-certain-pdfs