惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cyber Attacks, Cyber Crime and Cyber Security
The Last Watchdog
The Last Watchdog
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
W
WeLiveSecurity
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News | PayPal Newsroom
D
DataBreaches.Net
博客园_首页
Y
Y Combinator Blog
F
Fortinet All Blogs
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
B
Blog RSS Feed
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
Help Net Security
Help Net Security
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
N
News and Events Feed by Topic
Schneier on Security
Schneier on Security
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
L
LINUX DO - 热门话题
Simon Willison's Weblog
Simon Willison's Weblog
雷峰网
雷峰网
G
Google Developers Blog
Cyberwarzone
Cyberwarzone
I
Intezer
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
C
Check Point Blog
AI
AI
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
博客园 - 司徒正美

博客园 - 重粒子

文件在线预览doc,docx转换pdf(一) 使用 vs code 搭建vue项目(一) post文件下载 css 文本超出2行就隐藏并且显示省略号 js监听全屏下的esc事件 js实现全屏和缩放 ionic 侧栏菜单用法 weblogic获取应用目录路径(war包) 使用pdf.js预览实现读取服务器外部文件 js禁用浏览器后退 java批量将多文件打包成zip格式 Angularjs自定义指令计算浏览器高度 AngularJS封装webupload实现文件夹上传 jquery监听滚动条 pdf预览(pdf.js) (钉钉)第三方WEB网站扫码登录 $q的基本用法 Jersey RESTful WebService框架学习(八)文件下载防乱码 巧用 Jersey RESTful WebService框架解决文件上传乱码
vue中集成pdfjs自定义分页
重粒子 · 2018-08-31 · via 博客园 - 重粒子
<template>
 <div id="div_read_area_scrool" class="no-scrollbar--x" :style="'text-align:center;height:'+ '500px;'+'overflow: auto;'">
    <div id="div_read_area"></div>
 </div>
</template>

<script>
// 参数:the-document={}
//    属性:
import pdfjsLib from 'pdfjs-dist'
import $ from 'jquery'
export default {
  name: 'ReadByPdf',
  data () {
    return {
      theReadOnline: {
        current: 1,
        numPages: 0,
        html: ''
      }
    }
  },
  methods: {
    // 初始化pdfjs
    initThePDFJSLIB: function () {
      pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdfjs-dist/build/pdf.worker.js'
    },
    // 根据页码获取pdf内容
    loadPDFForTheDocument (index) {
      // 实例化pdfjs
      pdfjsLib.getDocument("/static/110.pdf").then(pdf => {
        // 通过页码获取每页内容
        pdf.getPage(index).then(page => {
          // 获得总页数
          this.theReadOnline.numPages = pdf.numPages

          // 设置页面显示倍数 842×595
          let _clientWidth = document.documentElement.clientWidth * 0.618
          let view = _clientWidth % 842 > 0 ? 1.33 : 1

          // 实例化画布
          let canvas = document.createElement('canvas')
          let context = canvas.getContext('2d')

          let dpr = window.devicePixelRatio || 1
          let bsr = context.webkitBackingStorePixelRatio ||
                  context.mozBackingStorePixelRatio ||
                  context.msBackingStorePixelRatio ||
                  context.oBackingStorePixelRatio ||
                  context.backingStorePixelRatio || 1
          let ratio = dpr / bsr

          let viewport = page.getViewport(view)

          canvas.width = viewport.width * ratio
          canvas.height = viewport.height * ratio
          // canvas.style.width = viewport.width + 'px'
          // canvas.style.height = viewport.height + 'px'

          // context.setTransform(ratio, 0, 0, ratio, 0, 0)

          let renderContext = {
            canvasContext: context,
            viewport: viewport
          }

          // 追加上新的一页空间
          document.getElementById('div_read_area').appendChild(canvas)

          // 赋值上显示内容
          page.render(renderContext).then(() => {
          })
        })
      }).catch(err => {
        console.log(err)
      })
    },
    watchTheReadScroll: function () {
      let _theVue = this

      // 获取节点
      let _scrollContent = document.getElementById('div_read_area_scrool')

      // 绑定事件
      _scrollContent.addEventListener('scroll', function () {
        var viewH, contentH, scrollTop

        viewH = $(this).height()
        contentH = $(this).get(0).scrollHeight
        scrollTop = $(this).scrollTop()

        // 后期改成传递给父级
        if (scrollTop / (contentH - viewH) >= 1) { // 到达底部时,加载新内容
          _theVue.theReadOnline.current++
          if (_theVue.theReadOnline.current <= _theVue.theReadOnline.numPages) {
            _theVue.loadPDFForTheDocument(_theVue.theReadOnline.current)
          }
        }
      })
    }
  },
  mounted: function () {
    this.initThePDFJSLIB()
    this.watchTheReadScroll()
    this.loadPDFForTheDocument(1)
  }
}
</script>