首页 > 其他分享 >TypeScript: pdf.js v4.0.379

TypeScript: pdf.js v4.0.379

时间:2024-04-07 16:25:07浏览次数:21  
标签:canvas TypeScript v4.0 PDF var pdf page viewport

 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>pdf v4.0.379 </title>
			<meta name="Keywords" content="涂聚文,塗聚文,捷為工作室,万年历,万年历查询,黄历,黄历查询,塗聚文,捷為工作室" />
    <meta name="Description" content="塗聚文,捷為工作室,信息流,物流,人力资源流,资本流的系统解决方案的开发与设计. " />
    <meta name="author" content="geovindu,塗聚文,涂聚文,Geovin Du" />
	<script src="../build/pdf.mjs" type="module"></script>
 <link rel="stylesheet" href="viewer.css">
	<script src="../build/pdf.mjs" type="module"></script>

<script type="module">
  // If absolute URL from the remote server is provided, configure the CORS
  // header on that server.
  var url = 'http://www.dusystem.com/PDFjs/4.0/web/123.pdf';

  // Loaded via <script> tag, create shortcut to access PDF.js exports.
  var { pdfjsLib } = globalThis;

  // The workerSrc property shall be specified.
  pdfjsLib.GlobalWorkerOptions.workerSrc = '../build/pdf.worker.mjs';

  var pdfDoc = null,
      pageNum = 1,
      pageRendering = false,
      pageNumPending = null,
      scale = 0.8,
      canvas = document.getElementById('the-canvas'),
      ctx = canvas.getContext('2d');

  /**
   * Get page info from document, resize canvas accordingly, and render page.
   * @param num Page number.
   */
  function renderPage(num) {
    pageRendering = true;
    // Using promise to fetch the page
    pdfDoc.getPage(num).then(function(page) {
      var viewport = page.getViewport({scale: scale});
      canvas.height = viewport.height;
      canvas.width = viewport.width;

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

      // Wait for rendering to finish
      renderTask.promise.then(function() {
        pageRendering = false;
        if (pageNumPending !== null) {
          // New page rendering is pending
          renderPage(pageNumPending);
          pageNumPending = null;
        }
      });
    });

    // Update page counters
    document.getElementById('page_num').textContent = num;
  }

  /**
   * If another page rendering in progress, waits until the rendering is
   * finised. Otherwise, executes rendering immediately.
   */
  function queueRenderPage(num) {
    if (pageRendering) {
      pageNumPending = num;
    } else {
      renderPage(num);
    }
  }

  /**
   * Displays previous page.
   */
  function onPrevPage() {
    if (pageNum <= 1) {
      return;
    }
    pageNum--;
    queueRenderPage(pageNum);
  }
  document.getElementById('prev').addEventListener('click', onPrevPage);

  /**
   * Displays next page.
   */
  function onNextPage() {
    if (pageNum >= pdfDoc.numPages) {
      return;
    }
    pageNum++;
    queueRenderPage(pageNum);
  }
  document.getElementById('next').addEventListener('click', onNextPage);

  /**
   * Asynchronously downloads PDF.
   */
  pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
    pdfDoc = pdfDoc_;
    document.getElementById('page_count').textContent = pdfDoc.numPages;

    // Initial/first page rendering
    renderPage(pageNum);
  });
</script>
<style type="text/css">
#the-canvas {
  border: 1px solid black;
  direction: ltr;
}	
</style>
</head>

<body>
	<h1>PDF.js Previous/Next example</h1>

<p>Please use <a href="https://mozilla.github.io/pdf.js/getting_started/#download"><i>official releases</i></a> in production environments.</p>

<div>
  <button id="prev">Previous</button>
  <button id="next">Next</button>
     
  <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>

<canvas id="the-canvas"></canvas>
</body>
</html>

  

<!doctype html>
<html>
<head>
<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>pdf using base64 encoded PDF</title>
			<meta name="Keywords" content="涂聚文,塗聚文,捷為工作室,万年历,万年历查询,黄历,黄历查询,塗聚文,捷為工作室" />
    <meta name="Description" content="塗聚文,捷為工作室,信息流,物流,人力资源流,资本流的系统解决方案的开发与设计. " />
    <meta name="author" content="geovindu,塗聚文,涂聚文,Geovin Du" />
	<script src="../build/pdf.mjs" type="module"></script>
 <link rel="stylesheet" href="viewer.css">
<script type="module">
  // atob() is used to convert base64 encoded PDF to binary-like data.
  // (See also https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/
  // Base64_encoding_and_decoding.)
  var pdfData = atob(
    'JVBERi0xLjcKCjEgMCBvYmogICUgZW50cnkgcG9pbnQKPDwKICAvVHlwZSAvQ2F0YWxvZwog' +
    'IC9QYWdlcyAyIDAgUgo+PgplbmRvYmoKCjIgMCBvYmoKPDwKICAvVHlwZSAvUGFnZXMKICAv' +
    'TWVkaWFCb3ggWyAwIDAgMjAwIDIwMCBdCiAgL0NvdW50IDEKICAvS2lkcyBbIDMgMCBSIF0K' +
    'Pj4KZW5kb2JqCgozIDAgb2JqCjw8CiAgL1R5cGUgL1BhZ2UKICAvUGFyZW50IDIgMCBSCiAg' +
    'L1Jlc291cmNlcyA8PAogICAgL0ZvbnQgPDwKICAgICAgL0YxIDQgMCBSIAogICAgPj4KICA+' +
    'PgogIC9Db250ZW50cyA1IDAgUgo+PgplbmRvYmoKCjQgMCBvYmoKPDwKICAvVHlwZSAvRm9u' +
    'dAogIC9TdWJ0eXBlIC9UeXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4KZW5kb2Jq' +
    'Cgo1IDAgb2JqICAlIHBhZ2UgY29udGVudAo8PAogIC9MZW5ndGggNDQKPj4Kc3RyZWFtCkJU' +
    'CjcwIDUwIFRECi9GMSAxMiBUZgooSGVsbG8sIHdvcmxkISkgVGoKRVQKZW5kc3RyZWFtCmVu' +
    'ZG9iagoKeHJlZgowIDYKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDEwIDAwMDAwIG4g' +
    'CjAwMDAwMDAwNzkgMDAwMDAgbiAKMDAwMDAwMDE3MyAwMDAwMCBuIAowMDAwMDAwMzAxIDAw' +
    'MDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9v' +
    'dCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G');

  // Loaded via <script> tag, create shortcut to access PDF.js exports.
  var { pdfjsLib } = globalThis;

  // The workerSrc property shall be specified.
  pdfjsLib.GlobalWorkerOptions.workerSrc = '../build/pdf.worker.mjs';

  // Using DocumentInitParameters object to load binary data.
  var loadingTask = pdfjsLib.getDocument({data: pdfData});
  loadingTask.promise.then(function(pdf) {
    console.log('PDF loaded');

    // Fetch the first page
    var pageNumber = 1;
    pdf.getPage(pageNumber).then(function(page) {
      console.log('Page loaded');

      var scale = 1.5;
      var viewport = page.getViewport({scale: 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
      };
      var renderTask = page.render(renderContext);
      renderTask.promise.then(function () {
        console.log('Page rendered');
      });
    });
  }, function (reason) {
    // PDF loading error
    console.error(reason);
  });
</script>
<style type="text/css">
#the-canvas {
  border: 1px solid black;
  direction: ltr;
}	
</style>
</head>

<body>
	<h1>PDF.js 'Hello, base64!' example</h1>

<p>Please use <a href="https://mozilla.github.io/pdf.js/getting_started/#download"><i>official releases</i></a> in production environments.</p>

<canvas id="the-canvas"></canvas>
</body>
</html>

  

https://github.com/dealfonso/pdfjs-viewer
https://mozilla.github.io/pdf.js/getting_started
https://mozilla.github.io/pdf.js/getting_started
https://mozilla.github.io/pdf.js/examples/
https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples

标签:canvas,TypeScript,v4.0,PDF,var,pdf,page,viewport
From: https://www.cnblogs.com/geovindu/p/18119302

相关文章

  • HOW - Typescript 类型声明文件
    目录一、背景二、如何添加类型定义支持智能提示方法一:使用JSDoc注释方法二:使用TypeScript编写类型声明文件.d.ts方法三:JSDoc注释转Typescript类型声明文件总结二、使用TypeScript编写类型声明文件1.创建类型声明文件2.编写类型声明2.1dec......
  • Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--可视化编辑pdf
    Pdfium.Net.Free支持.NETFramework4.0.NETFramework4.5.NETStandard2.0.Net8.0可以和PdfiumViewer.Free共同使用预览pdf,也可以直接引用Pdfium.Net.Free操作pdf,解决部分.NetCore调用的问题,Pdfium.Net.Free封装了现有Pdfium的函数,实现了部分操作pdf的功能,部分功......
  • 凡人修仙传8w字大纲pdf
    凡人修仙传8w字大纲pdf全资料包括主角韩立的一生、法宝法器、修炼体系、人物、妖兽、地理、宗门、设定、丹药、灵草、术法、材料、功法典籍、阵法等等详细进行了介绍。总pdf136页,内容来自于网络,仅用于学习交流,版权归原作者所有,侵权删。pdf永久网盘地址:https:/......
  • MC14516BDR2G倒数计数器芯片中文资料PDF数据手册参数引脚图图片特性概述
    产品概述:MC14516B同步正数/倒数二进制计数器在一个单片结构中使用MOSP沟道和N沟道增强模式器件构造。此计数器可通过对预设输入(P0、P1、P2、P3)应用所需的二进制值,然后将预设启用(PE)置于高电平,来进行预设。计数方向通过向UP/DOWN输入应用高电平(用于正数)或低电平(用于......
  • 使用 5 行 Python 代码拆分或合并 PDF 文件
    目录:拆分PDF文件合并PDF文件如果希望从大型PDF文件中提取特定页面或将多个PDF文件合并为一个文件,可以使用一些PDF编辑器来完成,但是你可能会发现拆分或合并功能在很多免费版PDF编辑工具中通常不可用。在本文中,我将分享一个简单的解决方案,用几行Python代......
  • CAT809JTBI-GT3中文资料规格书PDF数据手册引脚图产品参数特性概述
    产品概述:CAT809是一款µP监督电路,监控数字系统中的电源。CAT809可在基于工业温度范围运行的应用中直接替代MAX809。此器件会产生重置信号,当电源电压降至阈值阈值以下且电源升至该水平140ms之后发出该信号。安森美半导体使用的底层浮动门极技术AE²™可用于提供任......
  • MC14516BDR2G倒数计数器芯片中文资料PDF数据手册参数引脚图图片特性概述
    产品概述:MC14516B同步正数/倒数二进制计数器在一个单片结构中使用MOSP沟道和N沟道增强模式器件构造。此计数器可通过对预设输入(P0、P1、P2、P3)应用所需的二进制值,然后将预设启用(PE)置于高电平,来进行预设。计数方向通过向UP/DOWN输入应用高电平(用于正数)或低电平(用于......
  • pdffactory pro 8注册码序列号下载 附教程
    PdfFactoryPro可以说是一款行业专业且技术领先的的PDF虚拟打印机软件。其不仅占用系统内存小巧,功能强大,可支持用户无需使用Acrobat来创建AdobePDF即可以进行PDF组件的创建和打印。同时,现在全新的PdfFactoryPro8也正式上线来袭,全面新增添加了如书签、作业订购、信头和自动电......
  • Python实战:将Pdf文件转换为有声读物
    1.引言有声读物是现代生活中不可或缺的一部分,它可以让我们在通勤、健身等场合享受阅读的乐趣。然而,将Pdf文件转换为有声读物需要一定的技术支持。通过Python实现Pdf文件转换为有声读物,我们可以加深对编程语言的理解,同时也能够体会到编程带来的便利。2.环境准备在......
  • Vue3 + TypeScript + Vite 初始项目搭建(ESLint、Prettier、Sass、Stylelint、husky、p
    仓库地址仓库地址:https://gitee.com/tongchaowei/vue-ts-vite-template项目源码下载:https://gitee.com/tongchaowei/vue-ts-vite-template/releases全局安装pnpm包管理工具执行如下命令在系统全局安装pnpm包管理工具:npmipnpm-g使用Vite脚手架创建Vue3......