JavaScript如何提取PDF中的圖片和文字
本文是js文件處理系列三,前兩篇文章有介紹js文件處理,感興趣的可以查看導(dǎo)出pdf文件和word/excel/pdf/ppt在線預(yù)覽,本文補(bǔ)充一下js提前pdf中的文字和圖片的方法。
從 PDF 中提取文字 -核心代碼
其實(shí)核心代碼還是利用了pdf.js這個(gè)庫(kù),之前上一篇文章也有提及這個(gè)庫(kù),主要可以做pdfweb端的預(yù)覽。
文檔地址:mozilla.github.io/pdf.js/api/draft/module-pdfjsLib-PDFPageProxy.html
/** * Retrieves the text of a specif page within a PDF Document obtained through pdf.js * * @param {Integer} pageNum Specifies the number of the page * @param {PDFDocument} PDFDocumentInstance The PDF document obtained **/ function getPageText(pageNum, PDFDocumentInstance) { // Return a Promise that is solved once the text of the page is retrieven return new Promise(function (resolve, reject) { PDFDocumentInstance.getPage(pageNum).then(function (pdfPage) { // The main trick to obtain the text of the PDF page, use the getTextContent method pdfPage.getTextContent().then(function (textContent) { var textItems = textContent.items; var finalString = ''; // Concatenate the string of the item to the final string for (var i = 0; i < textItems.length; i++) { var item = textItems[i]; finalString += item.str + ' '; } // Solve promise with the text retrieven from the page resolve(finalString); }); }); }); }
從 PDF 中提取圖片
核心代碼如下:
// first here I open the document pdf.getDocument('haorooms.pdf').promise.then(async function (pdfObj) { // because I am testing, I just wanted to get page 7 const page = await pdfObj.getPage(7); // now I need to get the image information and for that I get the operator list const operators = await page.getOperatorList(); // this is for the paintImageXObject one, there are other ones, like the paintJpegImage which I assume should work the same way, this gives me the whole list of indexes of where an img was inserted const rawImgOperator = operators.fnArray .map((f, index) => (f === pdf.OPS.paintImageXObject ? index : null)) .filter((n) => n !== null); // now you need the filename, in this example I just picked the first one from my array, your array may be empty, but I knew for sure in page 7 there was an image... in your actual code you would use loops, such info is in the argsArray, the first arg is the filename, second arg is the width and height, but the filename will suffice here const filename = operators.argsArray[rawImgOperator[0]][0]; // now we get the object itself from page.objs using the filename page.objs.get(filename, async (arg) => { // and here is where we need the canvas, the object contains information such as width and height const canvas = ccc.createCanvas(arg.width, arg.height); const ctx = canvas.getContext('2d'); // now you need a new clamped array because the original one, may not contain rgba data, and when you insert you want to do so in rgba form, I think that a simple check of the size of the clamped array should work, if it's 3 times the size aka width*height*3 then it's rgb and shall be converted, if it's 4 times, then it's rgba and can be used as it is; in my case it had to be converted, and I think it will be the most common case const data = new Uint8ClampedArray(arg.width * arg.height * 4); let k = 0; let i = 0; while (i < arg.data.length) { data[k] = arg.data[i]; // r data[k + 1] = arg.data[i + 1]; // g data[k + 2] = arg.data[i + 2]; // b data[k + 3] = 255; // a i += 3; k += 4; } // now here I create the image data context const imgData = ctx.createImageData(arg.width, arg.height); imgData.data.set(data); ctx.putImageData(imgData, 0, 0); // get myself a buffer const buff = canvas.toBuffer(); // and I wrote the file, worked like charm, but this buffer encodes for a png image, which can be rather large, with an image conversion utility like sharp.js you may get better results by compressing the thing. fs.writeFile('test', buff); }); });
小結(jié)
本文主要介紹了js獲取pdf中文本和圖片的方法,其實(shí)pdf轉(zhuǎn)word也是大致這個(gè)思路,主要獲取文本和圖片,放到word文檔中。 本文主要是利用了pdfjs庫(kù),參考了issue github.com/mozilla/pdf.js/issues/13541
以上就是JavaScript如何提取PDF中的圖片和文字的詳細(xì)內(nèi)容,更多關(guān)于JavaScript提取PDF圖片和文字的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Bootstrap CSS組件之面包屑導(dǎo)航(breadcrumb)
這篇文章主要為大家詳細(xì)介紹了Bootstrap CSS組件之面包屑導(dǎo)航(breadcrumb),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12js中如何對(duì)json數(shù)組進(jìn)行排序
這篇文章主要介紹了js中如何對(duì)json數(shù)組進(jìn)行排序的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04深入理解 webpack 文件打包機(jī)制(小結(jié))
這篇文章主要介紹了深入理解 webpack 文件打包機(jī)制(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01基于input框覆蓋掉數(shù)字英文的實(shí)例講解
下面小編就為大家?guī)?lái)一篇基于input框覆蓋掉數(shù)字英文的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07一個(gè)js導(dǎo)致的jquery失效問(wèn)題的解決方法
這篇文章主要介紹了一個(gè)js導(dǎo)致的jquery失效問(wèn)題的解決方法,有需要的朋友可以參考一下2013-11-11javascript下function聲明一些小結(jié)
function聲明一些東西,我們都知道function和var一樣是預(yù)處理的在js里面,但是到底什么是函數(shù)聲明呢,我們來(lái)看幾個(gè)例子2007-12-12