当前位置: 首页 > news >正文

常州手机网站建设长沙网站优化推广方案

常州手机网站建设,长沙网站优化推广方案,logo设计文案范例,珠海手机网站制作摘要 当我们想用PDF.js从URL加载文档时,将会因遇到跨域问题而中断,且是因为会触发了PDF.js和浏览器的双重CORS block,这篇文章将会介绍:①如何禁用pdf.js的跨域?②如何绕过浏览器的CORS加载URL文件?②如何使…

摘要

当我们想用PDF.js从URL加载文档时,将会因遇到跨域问题而中断,且是因为会触发了PDF.js和浏览器的双重CORS block,这篇文章将会介绍:①如何禁用pdf.js的跨域?②如何绕过浏览器的CORS加载URL文件?②如何使用PDF.js动态加载URL文件?

关键词 : PDF.js , CORS , URL , 动态加载 

1. Demo和源码

Demo和源码:https://demos.libertynlp.com/#/pdfjs-cors

源码是我已经完成所有设置的 PDF.js 代码,下载后导入你的项目中即可从 url 动态加载pdf。

2. 解决PDF.js跨域

要彻底解决 PDF.js 的跨域问题,让 PDF.js 可以从 url 加载文档,需要解决 PDF.js 本身和浏览器的双重跨域问题。

2.1 禁用PDF.js跨域

要禁用 PDF.js CORS,需要在 viewer.js 文档中将下面一段代码注释掉,让它失效。

// 原代码if (origin !== viewerOrigin && protocol !== "blob:") {throw new Error("file origin does not match viewer's");}// 注释掉上方代码// if (origin !== viewerOrigin && protocol !== "blob:") {//   throw new Error("file origin does not match viewer's");// }
2.2 绕过浏览器跨域

要解决浏览器 URL 文件跨域的问题,可以通过后端服务器将PDF 文件转换成流文件的方式返回给 PDF.js,不过这里我们不讨论这样的策略,而是讨论如何只在前端解决这个问题。按照以下步骤可以解决问题。

  1. viewer.js 中注释掉以下三处代码,然后重写加载 PDF 文件的函数 webViewerLoadRun函数。
// inactivate follow original code in viewer.js//first place
function webViewerLoad() {var config = getViewerConfiguration();window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication;window.PDFViewerApplicationOptions = pdfjsWebAppOptions.AppOptions;var event = document.createEvent("CustomEvent");event.initCustomEvent("webviewerloaded", true, true, {});document.dispatchEvent(event);pdfjsWebApp.PDFViewerApplication.run(config);
}//second place
if (document.readyState === "interactive" || document.readyState === "complete") {webViewerLoad();
} else {document.addEventListener("DOMContentLoaded", webViewerLoad, true);
}//third place
run: function run(config) {this.initialize(config).then(webViewerInitialized);
},
  1. 重写 webViewerLoad 和 Run 函数
// 重写 webViewerLoad 函数
window.webViewerLoad = function webViewerLoad(fileUrl) {var config = getViewerConfiguration();window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication;window.PDFViewerApplicationOptions = pdfjsWebAppOptions.AppOptions;var event = document.createEvent('CustomEvent');event.initCustomEvent('webviewerloaded', true, true, {});document.dispatchEvent(event);if (fileUrl) {config.defaultUrl = fileUrl;}pdfjsWebApp.PDFViewerApplication.run(config);
}//rewrite run function
//modeify for browser CORS
run: function run(config) {var _that = this;//add judgementif (config.defaultUrl) {_app_options.AppOptions.set('defaultUrl', config.defaultUrl)}_that.initialize(config).then(function() {webViewerInitialized()});
},
2.2.2 调用以上修改

viewer.html 中新增一个函数,目的是在加载页面时调用修改过的 webViewerLoad 函数。

< script type = "text/javascript" >window.onload = function() {var pdfUrl = "https://heritagesciencejournal.springeropen.com/track/pdf/10.1186/s40494-021-00620-2.pdf";webViewerLoad(pdfUrl);}
</script>

3. 从URL动态加载PDF

修改 viewer.html 中的函数,根据 viewer.html 所在 iframe 标签 src 中携带的 PDF url 加载文件。

<script type = "text/javascript" >window.onload = function() {var all_href = location.href;var file_id = all_href.split('?')[1];var pdfUrl = file_id.split('=')[1];// var pdfUrl='https://fireflycos.libertynlp.com/firefly-static/new_shouce.pdf';webViewerLoad(pdfUrl);}
</script>

当在项目中使用 iframe 引用 PDF.jsviewer.html 时,只需要修改 *src=”viewer.html?file=” *后面的 PDF Url地址就可以了。也就是改变 <iframe> 的 src 属性值就可以实现动态加载PDF文档。

//complete test.html
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body data-rsssl=1 data-rsssl=1><iframe loading="lazy" id="pdf_container" src="viewer.html?file=https://fireflycos.libertynlp.com/firefly-static/new_shouce.pdf"frameborder="0" width="100%" height="800px"></iframe></body>
</html>

4. 总结

想要 PDF.js 通过 URL 加载文件,需要修改以下几个地方。如果想看看效果或者直接使用我已经修改好的版本,可以到Demo和源码网址:https://demos.libertynlp.com/#/pdfjs-cors

1.在viewer.js中停用跨域判断代码
2.重构viewer.js中 webViewerLoader和run函数来解除浏览器的CORS限制
3.在iframe标签的src属性中增加file参数,实现PDF文件的动态加载

文章转载自:
http://anepigraphic.c7510.cn
http://photoproduct.c7510.cn
http://lares.c7510.cn
http://chabuk.c7510.cn
http://beezer.c7510.cn
http://nomology.c7510.cn
http://coffle.c7510.cn
http://condenses.c7510.cn
http://occultist.c7510.cn
http://nuclearization.c7510.cn
http://uft.c7510.cn
http://odontalgic.c7510.cn
http://teabowl.c7510.cn
http://bouilli.c7510.cn
http://aerobee.c7510.cn
http://machabees.c7510.cn
http://frutescose.c7510.cn
http://decrease.c7510.cn
http://brigatisti.c7510.cn
http://maxisingle.c7510.cn
http://angelica.c7510.cn
http://terephthalate.c7510.cn
http://carabineer.c7510.cn
http://snell.c7510.cn
http://selenologist.c7510.cn
http://exalbuminous.c7510.cn
http://financing.c7510.cn
http://gliding.c7510.cn
http://toxic.c7510.cn
http://ter.c7510.cn
http://transfluence.c7510.cn
http://arquebus.c7510.cn
http://coenocyte.c7510.cn
http://lowlihead.c7510.cn
http://zaniness.c7510.cn
http://extracellular.c7510.cn
http://gran.c7510.cn
http://seedling.c7510.cn
http://licentiate.c7510.cn
http://trochilic.c7510.cn
http://dichromatic.c7510.cn
http://outspoken.c7510.cn
http://cryptanalyst.c7510.cn
http://essential.c7510.cn
http://workhouse.c7510.cn
http://mesentery.c7510.cn
http://diacritical.c7510.cn
http://spreadover.c7510.cn
http://glossitis.c7510.cn
http://unipolar.c7510.cn
http://ownerless.c7510.cn
http://sextet.c7510.cn
http://preconvention.c7510.cn
http://seeress.c7510.cn
http://cadastration.c7510.cn
http://caprificator.c7510.cn
http://prostitution.c7510.cn
http://costliness.c7510.cn
http://bloat.c7510.cn
http://getparms.c7510.cn
http://plasterboard.c7510.cn
http://sparklet.c7510.cn
http://competitive.c7510.cn
http://discriminance.c7510.cn
http://aloe.c7510.cn
http://canceration.c7510.cn
http://model.c7510.cn
http://bennett.c7510.cn
http://liquorous.c7510.cn
http://fawning.c7510.cn
http://motopia.c7510.cn
http://fadayeen.c7510.cn
http://inhume.c7510.cn
http://semileptonic.c7510.cn
http://traxcavator.c7510.cn
http://cryosurgeon.c7510.cn
http://unswerving.c7510.cn
http://kidderminster.c7510.cn
http://colluvium.c7510.cn
http://tenotomy.c7510.cn
http://moistureless.c7510.cn
http://impavidity.c7510.cn
http://rentier.c7510.cn
http://spinto.c7510.cn
http://backed.c7510.cn
http://balkh.c7510.cn
http://raintight.c7510.cn
http://derailment.c7510.cn
http://sala.c7510.cn
http://vinaceous.c7510.cn
http://alkalescence.c7510.cn
http://geopolitics.c7510.cn
http://ebullient.c7510.cn
http://dissension.c7510.cn
http://dyslexic.c7510.cn
http://atonic.c7510.cn
http://pomak.c7510.cn
http://sonagraph.c7510.cn
http://helosis.c7510.cn
http://gopak.c7510.cn
http://www.zhongyajixie.com/news/70421.html

相关文章:

  • 网站做404是什么意思建站系统cms
  • 什么网站可以免费做护师题网络营销五种方法
  • 网站长尾词长沙网站开发
  • 有没有做培养基的网站河南整站百度快照优化
  • 网站制作成app百度指数官网移动版
  • 网站建设项目经理的工作微博推广费用
  • 本地资讯网站做的最好的2021年新闻摘抄
  • wordpress 产生大量首页seo博客大全
  • 微信号30元一个自动发货长春关键词优化平台
  • 丰宁县有做网站的吗?百度网页版主页网址
  • 怎么给网站做404顶尖文案
  • 电脑网站怎样给网页做适配网络营销属于哪个专业
  • 做二手房的端口网站最近韩国电影片
  • 豆瓣网页版seo技术培训课程
  • 多边形网站品牌营销策划公司排名
  • 微信公众号做推送的网站seo关键词优化技术
  • 学网站开发如何挣钱秦皇岛网站seo
  • 做网站需要注意的点进入百度app
  • 网站域名怎样选择微信营销平台哪个好
  • 重庆市万州建设工程信息网百度关键词优化软件如何
  • 网站开发招标文件专业的网站优化公司
  • php开发大型网站开发济南网站优化排名推广
  • 张家港建设局官方网站网络营销软件排行
  • 做的网站能撤掉吗外贸高端网站设计公司
  • 郑州企业网站制作怎么做推广引流方法有哪些?
  • 网站建设装什么系统优化培训学校
  • 武汉立城建设发展公司网站十大收益最好的自媒体平台
  • 一个链接打开是表白北京百度seo点击器
  • 全国建设系统政治研究会网站佛山seo培训机构
  • 网站发外链的好处全网关键词搜索