You could proxy the PDF file through your own server.(您可以通过自己的服务器代理PDF文件。)
This should resolve any issues you have with CORS or other cross-domain problems.(这样可以解决您在CORS上遇到的任何问题或其他跨域问题。)
Step 1 - Add new route for proxy controller(步骤1-为代理控制器添加新路由)
get "proxy/:url" => "proxy#index", :constraints => { :url => /.*/ }
Step 2 - Create the proxy controller(第2步-创建代理控制器)
require 'open-uri'
class ProxyController < ApplicationController
def index
url = params[:url]
url.gsub!(/(https?:/)/, '1/')
# Note there is no error handling here.
# This is only proof of concept.
data = open(url)
send_data data.read,
:type => data.content_type,
:disposition => 'inline'
end
end
Step 3 - Modify view to use proxy(步骤3-修改视图以使用代理)
<%= pdfjs_viewer
pdf_url: "/proxy/http://remote-server.com/remotefile.pdf",
style: :full %>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…