有没有一种方法可以计算只有js(没有Node.js)的doc、docx、pdf页面

Is there a way to count doc, docx, pdf pages with only js (without Node.js)?

本文关键字:js Node 没有 docx 页面 pdf doc 一种 方法 有没有 计算      更新时间:2023-09-26

我需要计算doc、docx和pdf文件中的页数
我知道用PHP、NodeJS 是可能的

但若文件在服务器上,那个么只使用javascript就可以实现吗?

https://www.npmjs.com/package/docx-pdf-pagecount可以用来获取docx和pdf的页数。

const getPageCount = require('docx-pdf-pagecount');
getPageCount('E:/sample/document/aa/test.docx')
  .then(pages => {
    console.log(pages);
  })
  .catch((err) => {
    console.log(err);
  });

getPageCount('E:/sample/document/vb.pdf')
  .then(pages => {
    console.log(pages);
  })
  .catch((err) => {
    console.log(err);
  });
**To find out page number of PDF files**
<script src="js/pdf.js?1444224269"></script>
PDFJS.workerSrc    =   "js/pdf.worker.js";
var fileLocation   =   'test.pdf';
var numPages    =   0;
PDFJS.getDocument(fileLocation).then(function(pdf) {
numPages   =   pdf.numPages;
});

首先需要从这里下载zip文件

在之后

  • 在目标页面中包含PDFPageCount.js库
  • 按以下格式调用函数

PDFPageCount.getPageCount(目标PDF文件,回调函数)

示例

**PDFPageCount.getPageCount("HTML5_draft.pdf",callbackFunc);**

一旦检测到页面计数,回调函数将获取页面计数作为返回参数