在Javascript中从pdf中提取文本
我想知道是否可以通过仅使用Javascript来获取PDF文件中的文本?
如果是,有人可以告诉我怎么做?
I wonder if is possible to get the text inside of a PDF file by using only Javascript? If yes, can anyone show me how?
我知道有一些服务器端的java,c#等库但我不想使用服务器。
谢谢
I know there are some server-side java, c#, etc libraries but I would prefer not using a server. thanks
这是一个古老的问题,但因为pdf.js多年来一直在发展,我会想给出一个新的答案。也就是说,它可以在本地完成,而不涉及任何服务器或外部服务。新的pdf.js有一个函数:page.getTextContent()。您可以从中获取文本内容。我用以下代码成功完成了它。
This is an ancient question, but because pdf.js has been developing over the years, I would like to give a new answer. That is, it can be done locally without involving any server or external service. The new pdf.js has a function: page.getTextContent(). You can get the text content from that. I've done it successfully with the following code.
-
您在每一步中得到的都是承诺。您需要以这种方式编码:
.then(function(){...})
以继续下一步。
1) PDFJS.getDocument(data).then(function(pdf){
2) pdf.getPage(i).then(function(page){
3) page.getTextContent()。then(function(textContent){
什么你最终得到的是一个字符串数组 textContent.bidiTexts []
。你将它们连接起来得到1页的文本。文本块的坐标用于判断换行符或空格是否需要要插入。(这可能不完全健壮,但从我的测试看似没问题。)
What you finally get is an string array textContent.bidiTexts[]
. You concatenate them to get the text of 1 page. Text blocks' coordinates are used to judge whether newline or space need to be inserted. (This may not be totally robust, but from my test it seems ok.)
输入参数 data
需要是URL或ArrayBuffer类型数据。我使用 FileReader
API中的ReadAsArrayBuffer(文件)函数来获取数据。
The input parameter data
needs to be either a URL or ArrayBuffer type data. I used the ReadAsArrayBuffer(file) function in FileReader
API to get the data.
希望这会有所帮助。
注意:根据其他一些用户的说法,库已更新并导致代码中断。根据下面 async5 的注释,您需要替换 tex tContent.bidiTexts
with textContent.items
。
Note: According to some other user, the library has updated and caused the code to break. According to the comment by async5 below, you need to replace textContent.bidiTexts
with textContent.items
.
function Pdf2TextClass(){
var self = this;
this.complete = 0;
/**
*
* @param data ArrayBuffer of the pdf file content
* @param callbackPageDone To inform the progress each time
* when a page is finished. The callback function's input parameters are:
* 1) number of pages done;
* 2) total number of pages in file.
* @param callbackAllDone The input parameter of callback function is
* the result of extracted text from pdf file.
*
*/
this.pdfToText = function(data, callbackPageDone, callbackAllDone){
console.assert( data instanceof ArrayBuffer || typeof data == 'string' );
PDFJS.getDocument( data ).then( function(pdf) {
var div = document.getElementById('viewer');
var total = pdf.numPages;
callbackPageDone( 0, total );
var layers = {};
for (i = 1; i <= total; i++){
pdf.getPage(i).then( function(page){
var n = page.pageNumber;
page.getTextContent().then( function(textContent){
if( null != textContent.bidiTexts ){
var page_text = "";
var last_block = null;
for( var k = 0; k < textContent.bidiTexts.length; k++ ){
var block = textContent.bidiTexts[k];
if( last_block != null && last_block.str[last_block.str.length-1] != ' '){
if( block.x < last_block.x )
page_text += "\r\n";
else if ( last_block.y != block.y && ( last_block.str.match(/^(\s?[a-zA-Z])$|^(.+\s[a-zA-Z])$/) == null ))
page_text += ' ';
}
page_text += block.str;
last_block = block;
}
textContent != null && console.log("page " + n + " finished."); //" content: \n" + page_text);
layers[n] = page_text + "\n\n";
}
++ self.complete;
callbackPageDone( self.complete, total );
if (self.complete == total){
window.setTimeout(function(){
var full_text = "";
var num_pages = Object.keys(layers).length;
for( var j = 1; j <= num_pages; j++)
full_text += layers[j] ;
callbackAllDone(full_text);
}, 1000);
}
}); // end of page.getTextContent().then
}); // end of page.then
} // of for
});
}; // end of pdfToText()
}; // end of class