pdf.js源码学习从helloworld开始

首先我们先在web服务器下新建一个helloworld文件夹,把我们编译好的pdf.js和pdf.worker.js拷贝到该目录下然后再创建一个hello.js和一个hello.html得到如下图文件目录:
这里写图片描述
hello.js代码如下:


'use strict';

//
// Fetch the PDF document from the URL using promises
//
//使用pdf.js内置的对象PDFJS读取pdf文件
PDFJS.getDocument('helloworld.pdf').then(function(pdf) {
  // Using promise to fetch the page
  //获取文档第一页
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport(scale);
    //
    // Prepare canvas using PDF page dimensions
    //获取文档第一页内容并显示在画布上
    var canvas = document.getElementById('the-canvas');
    var context = canvas.getContext('2d');
    canvas.height = viewport.height;
    canvas.width = viewport.width;

    //
    // Render PDF page into canvas context
    //
    var renderContext = {
      canvasContext: context,
      viewport: viewport
    };
    page.render(renderContext);
  });
});

hello.html

<!doctype html>
<html>

<head>
<!--引入pdf.js和pdf.worker.js-->
  <script src="pdf.js"></script>
  <script>
    PDFJS.workerSrc="pdf.worker.js";
  </script>
  <script src="hello.js"></script>
</head>

<body>
    <!--定义一个画布-->
  <canvas id="the-canvas" style="border:1px solid black;"/>
</body>

</html>

这样我们就实现了在浏览器上显示第一个pdf文件了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值