Javascript 如何获取用户上传的 .PDF 的页数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10253669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the number of pages of a .PDF uploaded by user?
提问by sparkle
I have a file input, and before "uploading" i need to calculate the number of pages of that .pdf in JAVASCRIPT (eg. JQuery...)
我有一个文件输入,在“上传”之前,我需要计算 JAVASCRIPT 中该 .pdf 的页数(例如 JQuery ...)
回答by Sid Thakur
In case you use pdf.js you may reference an example on github('.../examples/node/getinfo.js') with following code that prints number of pages in a pdf file.
如果您使用 pdf.js,您可以使用以下代码引用github('.../examples/node/getinfo.js') 上的示例,该代码在 pdf 文件中打印页数。
const pdfjsLib = require('pdfjs-dist');
...
pdfjsLib.getDocument(pdfPath).then(function (doc) {
var numPages = doc.numPages;
console.log('# Document Loaded');
console.log('Number of Pages: ' + numPages);
}
回答by Sajjad Shirazy
and a pure javascript solution:
和一个纯 javascript 解决方案:
var input = document.getElementById("files");
var reader = new FileReader();
reader.readAsBinaryString(input.files[0]);
reader.onloadend = function(){
var count = reader.result.match(/\/Type[\s]*\/Page[^s]/g).length;
console.log('Number of Pages:',count );
}
回答by groodt
As has been stated in the other answers, something like pdf.js is be what you are looking for. I've taken a look at the API and it does include a numPages() function to return the total number of pages. It also seems to count pages for me when viewing the demo page from Mozilla.
正如其他答案中所述,您正在寻找诸如 pdf.js 之类的东西。我查看了 API,它确实包含一个 numPages() 函数来返回总页数。从 Mozilla查看演示页面时,它似乎也为我计算页面数。
It depends if you are able to use modern browsers and experimental technology for your solution. pdf.js is very impressive, but it is still experimental according to the github page.
这取决于您是否能够将现代浏览器和实验技术用于您的解决方案。pdf.js 非常令人印象深刻,但根据github 页面,它仍然是实验性的。
If you are able to count the pages on the server after uploading, then you should look at pdftoolsor similar.
如果您能够在上传后计算服务器上的页面数,那么您应该查看pdftools或类似工具。
Something like pdftools --countpages
is what you are looking for
类似的东西pdftools --countpages
就是你要找的
回答by José Antonio Postigo
You could also use pdf-lib
.
您也可以使用pdf-lib
.
You will need to read the file from the input field and then make use of pdf-lib
to get the number of pages. The code would be like this:
您需要从输入字段中读取文件,然后使用pdf-lib
来获取页数。代码如下:
import { PDFDocument } from 'pdf-lib';
...
const readFile = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
reader.readAsArrayBuffer(file);
});
}
const async getNumPages = (file) => {
const arrayBuffer = await readFile(file);
const pdf = await PDFDocument.load(arrayBuffer);
return pdf.getPages();
}
And then just get the number of pages of the attached file with:
然后只需使用以下命令获取附件的页数:
const numPages = await getNumPages(input.files[0]);
being input
the variable which stores the reference to the DOM element of the file input.
是input
存储对文件输入的 DOM 元素的引用的变量。