Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

javascript - 如何在Rails中显示PDF?(How do I show a PDF in Rails?)

I'm using pdfjs_viewer-rails gem and when I try to load a custom PDF in this way(我正在使用pdfjs_viewer-rails gem,当我尝试以这种方式加载自定义PDF时)

<%= pdfjs_viewer pdf_url: "http://biblioteca2.ucab.edu.ve/anexos/biblioteca/marc/texto/AAM8264.pdf", style: :full %> I'm getting this error:(我收到此错误:) PDF.js v1.10.100 (build: ea29ec83) Message: file origin does not match viewer's I searched in internet and as I understand I have to setup CORS but I don't understand how.(我在互联网上进行搜索,据我了解我必须设置CORS,但我不知道如何。) If there's any other way to show PDF without print and download button which isn't the google toobar method I apreciate it.(如果还有其他方法可以显示没有打印和下载按钮的PDF,而这不是Google toobar方法,那么我对此表示赞赏。) (I know it's impossible to prevent a PDF being downloaded, this is just a work for an assignature for my college).((我知道阻止PDF的下载是不可能的,这只是我的大学的一项工作)。)   ask by NewbieOnRails translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 %>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...