最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript如何提取PDF中的圖片和文字

 更新時(shí)間:2024年11月27日 10:59:45   作者:haorooms  
這篇文章主要為大家詳細(xì)介紹了JavaScript如何實(shí)現(xiàn)提取PDF中的圖片和文字,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

本文是js文件處理系列三,前兩篇文章有介紹js文件處理,感興趣的可以查看導(dǎo)出pdf文件和word/excel/pdf/ppt在線預(yù)覽,本文補(bǔ)充一下js提前pdf中的文字和圖片的方法。

從 PDF 中提取文字 -核心代碼

其實(shí)核心代碼還是利用了pdf.js這個(gè)庫,之前上一篇文章也有提及這個(gè)庫,主要可以做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庫,參考了issue github.com/mozilla/pdf.js/issues/13541

以上就是JavaScript如何提取PDF中的圖片和文字的詳細(xì)內(nèi)容,更多關(guān)于JavaScript提取PDF圖片和文字的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • JavaScript常用正則驗(yàn)證函數(shù)實(shí)例小結(jié)【年齡,數(shù)字,Email,手機(jī),URL,日期等】

    JavaScript常用正則驗(yàn)證函數(shù)實(shí)例小結(jié)【年齡,數(shù)字,Email,手機(jī),URL,日期等】

    這篇文章主要介紹了JavaScript常用正則驗(yàn)證函數(shù),結(jié)合實(shí)例形式總結(jié)分析了javascript針對年齡、數(shù)字、Email、手機(jī)、URL、日期等格式常用正則驗(yàn)證技巧,需要的朋友可以參考下
    2017-01-01
  • 使用Echarts設(shè)置地圖并觸發(fā)點(diǎn)擊事件的代碼

    使用Echarts設(shè)置地圖并觸發(fā)點(diǎn)擊事件的代碼

    這篇文章主要給大家介紹了關(guān)于使用Echarts設(shè)置地圖并觸發(fā)點(diǎn)擊事件的的相關(guān)資料,ECharts是一款基于JavaScript的數(shù)據(jù)可視化庫,可以用于創(chuàng)建各種類型的交互式圖表,包括地圖,需要的朋友可以參考下
    2023-09-09
  • 一文了解你不知道的JavaScript生成器篇

    一文了解你不知道的JavaScript生成器篇

    ES6引入了一個(gè)新的函數(shù)類型,發(fā)現(xiàn)它并不符合這種運(yùn)行到結(jié)束的特性。這類新的函數(shù)被稱為生成器。生成器的出現(xiàn)是我們知道原來有時(shí)代碼并不會(huì)順利的運(yùn)行,可以通過暫停的方式進(jìn)行異步回調(diào),讓我們摒棄了此前的認(rèn)知。本文就來聊聊JavaScript中生成器的相關(guān)知識(shí)
    2022-11-11
  • JavaScript 中問號(hào)的三種用法 ??和?.以及?:

    JavaScript 中問號(hào)的三種用法 ??和?.以及?:

    本文主要介紹了JavaScript 中問號(hào)的三種用法 ??和?.以及?: ,分別是空值合并操作符、可選鏈操作符和三目運(yùn)算,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-04-04
  • js刪除Array數(shù)組中指定元素的兩種方法

    js刪除Array數(shù)組中指定元素的兩種方法

    下面小編就為大家?guī)硪黄猨s刪除Array數(shù)組中指定元素的兩種方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-08-08
  • JavaScript中的Promise詳解

    JavaScript中的Promise詳解

    現(xiàn)在網(wǎng)上有非常多的Promise文章,但都是給你一堆代碼,或者某些核心代碼,讓你看完之后感覺,嗯,很厲害,但還是不知所云,不知其所以然。那么本文真正從一個(gè)小白開始帶你深入淺出,一步一步實(shí)現(xiàn)自己的 Promise,這種自己造輪子的過程一定是進(jìn)步最快的過程,快上車開始吧
    2022-11-11
  • 實(shí)現(xiàn)非常簡單的js雙向數(shù)據(jù)綁定

    實(shí)現(xiàn)非常簡單的js雙向數(shù)據(jù)綁定

    Angular實(shí)現(xiàn)了雙向綁定機(jī)制。所謂的雙向綁定,無非是從界面的操作能實(shí)時(shí)反映到數(shù)據(jù),數(shù)據(jù)的變更能實(shí)時(shí)展現(xiàn)到界面。本文給大家詳細(xì)介紹js雙向數(shù)據(jù)綁定,感興趣的朋友參考下
    2015-11-11
  • Webpack在異步請求JS文件時(shí)如何獲取JS?Bundle的機(jī)制

    Webpack在異步請求JS文件時(shí)如何獲取JS?Bundle的機(jī)制

    Webpack是一個(gè)現(xiàn)代JavaScript應(yīng)用程序的靜態(tài)模塊打包工具,它的主要作用是將項(xiàng)目中的多個(gè)模塊按照依賴關(guān)系進(jìn)行打包,生成一個(gè)或多個(gè)靜態(tài)資源文件,這篇文章主要介紹了Webpack在異步請求JS文件時(shí)如何獲取JS?Bundle機(jī)制的相關(guān)資料,需要的朋友可以參考下
    2026-01-01
  • ES6中Generator與異步操作實(shí)例分析

    ES6中Generator與異步操作實(shí)例分析

    這篇文章主要介紹了ES6中Generator與異步操作,結(jié)合實(shí)例形式分析Generator的概念、功能及相關(guān)操作技巧,需要的朋友可以參考下
    2017-03-03
  • JavaScript文件的同步和異步加載的實(shí)現(xiàn)代碼

    JavaScript文件的同步和異步加載的實(shí)現(xiàn)代碼

    本篇文章主要介紹了JavaScript文件的同步和異步加載的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08

最新評(píng)論

阳朔县| 聂拉木县| 隆德县| 沭阳县| 当阳市| 运城市| 绥阳县| 饶平县| 扶余县| 汝阳县| 永和县| 肇州县| 姚安县| 青冈县| 含山县| 韩城市| 泗阳县| 巴彦县| 尼玛县| 乌拉特后旗| 大余县| 兖州市| 禹城市| 迭部县| 西昌市| 宜宾市| 唐山市| 华亭县| 晴隆县| 资中县| 镇远县| 昭通市| 竹山县| 杭锦后旗| 和田县| 永康市| 平江县| 藁城市| 霸州市| 通许县| 沙洋县|