简体   繁体   English

使用nodejs将具有表格的pdf转换为xlsx / xls

[英]convert a pdf having a table to xlsx/xls using nodejs

I am having a pdf that contain a table and having data. 我有一个包含表和数据的pdf。

I want to convert it to xlsx /xls , I have tried many way ie pdf to json and json to xlsx/xls but I am not getting the result has I want, i want the json data in key and value pair 我想将其转换为xlsx / xls ,我尝试了很多方法,例如将pdf转换为json ,将json转换为xlsx / xls,但是我没有得到想要的结果,我想要键和值对中json数据

code

let fs = require('fs'),
PDFParser = require("pdf2json");
let pdfParser = new PDFParser();

pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
pdfParser.on("pdfParser_dataReady", pdfData => {
    fs.writeFile("./pdf2.json", JSON.stringify(pdfData),(error) => { 
                if(error)
                {
                    console.log(error);
                }
         });
});

pdfParser.loadPDF("./Sample Data.pdf");

my pdf look like this 我的pdf看起来像这样

you can use pdf2table or pdfreader to read data from pdf file and shape that data to required format of json. 您可以使用pdf2table或pdfreader从pdf文件中读取数据并将该数据成形为所需的json格式。

Adding sample codes for reference 添加示例代码以供参考

//pdf2table // pdf2table

var pdf2table = require('pdf2table');
var fs = require('fs');

fs.readFile('./tests.pdf', function (err, buffer) {
    if (err) return console.log(err);

    pdf2table.parse(buffer, function (err, rows, rowsdebug) {
        if(err) return console.log(err);
        console.log(rows);
    });
});

//pdfreader // pdf阅读器

var pdfreader = require('pdfreader');
var table = new pdfreader.TableParser();

new pdfreader.PdfReader().parseFileItems('tests.pdf', function(err, item){
  if( err ) {
      console.log( err )
  } else {
      console.log( item.text)
     //create json as you want
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM