您需要编写一个按发票格式的函数(fn company1,fn company2 ...)。
以下是使用三个不同函数从pdf.js-extract
模块的导出数据中检索数据的示例:
// Sample invoice
let sampleInvoice =
{
"pages":
[
{
"content":
[
{
"x": 348.41,
"y": 125.59899999999993,
"str": "Invoice Number",
"dir": "ltr",
"width": 61.61760000000001,
"height": 8.8,
"fontName": "g_d0_f2"
},
{
"x": 451.935,
"y": 125.59899999999993,
"str": "INV-3337",
"dir": "ltr",
"width": 37.171200000000006,
"height": 8.8,
"fontName": "g_d0_f2"
}
]
}
]
};
// Create alerts for test functions in browser
alert(searchByPosition(sampleInvoice.pages, 450, 125));
alert(searchByPrev(sampleInvoice.pages, 'Invoice Number'));
alert(searchByFormat(sampleInvoice.pages, /INV-\d+$/));
function searchByPosition(pages,x,y)
{
// Set position range (difference max)
let range = 10;
// Init x and y positions
x = Math.floor(x/range), y = Math.floor(y/range);
// Loop in all pages
for(let i = 0; i < pages.length; i++)
// Loop in all content
for(let j = 0; j < pages[i].content.length; j++)
// Test position x and y and if match return content
if(Math.floor(pages[i].content[j].x/range) == x && Math.floor(pages[i].content[j].y/range) == y)
// Return result
return pages[i].content[j].str;
// No results found
return 'NotFound';
}
function searchByPrev(pages,txt)
{
// Init txt
txt = txt.toLowerCase();
// Loop in all pages
for(let i = 0; i < pages.length; i++)
// Loop in all content
for(let j = 0; j < pages[i].content.length; j++)
// Test text and if match return next content
// (If you write j-1, you can have searchByNext function)
if(pages[i].content[j].str.toLowerCase() == txt && pages[i].content[j+1])
// Return result
return pages[i].content[j+1].str;
// No results found
return 'NotFound';
}
function searchByFormat(pages,regex)
{
// Loop in all pages
for(let i = 0; i < pages.length; i++)
// Loop in all content
for(let j = 0; j < pages[i].content.length; j++)
// Test regex and if match return content
if(regex.test(pages[i].content[j].str))
// Return result
return pages[i].content[j].str;
// No results found
return 'NotFound';
}
请试用这里:https://jsfiddle.net/dkhqzg6s/
console.log(JSON.stringify(data));
的返回结果吗? - Liberateurconsole.log(JSON.stringify(data));
- Liberateur{ "x": 348.41, "y": 125.59899999999993, "str": "发票号码", "dir": "ltr", "width": 61.61760000000001, "height": 8.8, "fontName": "g_d0_f2" }, { "x": 451.935, "y": 125.59899999999993, "str": "INV-3337", "dir": "ltr", "width": 37.171200000000006, "height": 8.8, "fontName": "g_d0_f2" },
], } - Ayushi Gupta