在 HTML 页面上展示 PDF 文件示例代码

html 展示 pdf 示例

<!DOCTYPE html>
<html>
<head>
  <title>PDF.js Example</title>
</head>
<body>
 
<div>
  <canvas id="the-canvas"></canvas>
</div>
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.js"></script>
<!--<script src="./pdf.js"></script>-->
<script>
  // This is a simple example of using PDF.js to show a PDF.
  pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.worker.js';
 
  const url = 'hsx.pdf'; // Specify the URL of your PDF file here.
  const canvas = document.getElementById('the-canvas');
  const context = canvas.getContext('2d');
 
  // Loaded via <script> tag, create shortcut to access PDF.js exports.
  pdfjsLib.getDocument(url).promise.then(pdfDoc => {
    // Fetch the first page of the PDF.
    pdfDoc.getPage(1).then(page => {
      // Get viewport (dimensions) of the page.
      const viewport = page.getViewport({ scale: 1.5 });
      canvas.height = viewport.height;
      canvas.width = viewport.width;
 
      // Render the page into the canvas context.
      const renderContext = {
        canvasContext: context,
        viewport: viewport
      };
      page.render(renderContext).promise.then(() => {
        console.log('Page rendered!');
      });
    });
  });
</script>
 
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值