Улучшение конвертирования Pdf
This commit is contained in:
@@ -109,13 +109,20 @@ class ConvertHtml extends ConvertBase {
|
|||||||
//подозрение на чистый текст, надо разбить на параграфы
|
//подозрение на чистый текст, надо разбить на параграфы
|
||||||
if (isText || pars.length < buf.length/2000) {
|
if (isText || pars.length < buf.length/2000) {
|
||||||
let total = 0;
|
let total = 0;
|
||||||
|
let max = 0;
|
||||||
for (let i = 0; i < spaceCounter.length; i++) {
|
for (let i = 0; i < spaceCounter.length; i++) {
|
||||||
total += (spaceCounter[i] ? spaceCounter[i] : 0);
|
const sc = (spaceCounter[i] ? spaceCounter[i] : 0);
|
||||||
|
max = (sc > max ? sc : max);
|
||||||
|
total += sc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let i = 0;
|
||||||
|
//если разброс не слишком большой
|
||||||
|
if (total < max*2) {
|
||||||
total /= 20;
|
total /= 20;
|
||||||
let i = spaceCounter.length - 1;
|
i = spaceCounter.length - 1;
|
||||||
while (i > 0 && (!spaceCounter[i] || spaceCounter[i] < total)) i--;
|
while (i > 0 && (!spaceCounter[i] || spaceCounter[i] < total)) i--;
|
||||||
|
}
|
||||||
|
|
||||||
const parIndent = (i > 0 ? i : 0);
|
const parIndent = (i > 0 ? i : 0);
|
||||||
|
|
||||||
|
|||||||
@@ -22,16 +22,21 @@ class ConvertPdf extends ConvertHtml {
|
|||||||
const outFile = `${inputFiles.fileListDir}/${utils.randomHexString(10)}.xml`;
|
const outFile = `${inputFiles.fileListDir}/${utils.randomHexString(10)}.xml`;
|
||||||
|
|
||||||
//конвертируем в xml
|
//конвертируем в xml
|
||||||
await this.execConverter(this.pdfToHtmlPath, ['-c', '-s', '-xml', inputFiles.sourceFile, outFile]);
|
let perc = 0;
|
||||||
callback(50);
|
await this.execConverter(this.pdfToHtmlPath, ['-c', '-s', '-xml', inputFiles.sourceFile, outFile], () => {
|
||||||
|
perc = (perc < 80 ? perc + 10 : 40);
|
||||||
|
callback(perc);
|
||||||
|
});
|
||||||
|
callback(80);
|
||||||
|
|
||||||
const data = await fs.readFile(outFile);
|
const data = await fs.readFile(outFile);
|
||||||
callback(60);
|
callback(90);
|
||||||
|
|
||||||
//парсим xml
|
//парсим xml
|
||||||
let lines = [];
|
let lines = [];
|
||||||
let inText = false;
|
let inText = false;
|
||||||
let title = '';
|
let title = '';
|
||||||
|
let prevTop = 0;
|
||||||
let i = -1;
|
let i = -1;
|
||||||
|
|
||||||
const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||||
@@ -48,17 +53,20 @@ class ConvertPdf extends ConvertHtml {
|
|||||||
let attrs = sax.getAttrsSync(tail);
|
let attrs = sax.getAttrsSync(tail);
|
||||||
const line = {
|
const line = {
|
||||||
text: '',
|
text: '',
|
||||||
top: (attrs.top && attrs.top.value ? attrs.top.value : null),
|
top: parseInt((attrs.top && attrs.top.value ? attrs.top.value : null), 10),
|
||||||
left: (attrs.left && attrs.left.value ? attrs.left.value : null),
|
left: parseInt((attrs.left && attrs.left.value ? attrs.left.value : null), 10),
|
||||||
width: (attrs.width && attrs.width.value ? attrs.width.value : null),
|
width: parseInt((attrs.width && attrs.width.value ? attrs.width.value : null), 10),
|
||||||
height: (attrs.height && attrs.height.value ? attrs.height.value : null),
|
height: parseInt((attrs.height && attrs.height.value ? attrs.height.value : null), 10),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (line.width !== '0' || line.height !== '0') {
|
if (line.width !== '0' || line.height !== '0') {
|
||||||
inText = true;
|
inText = true;
|
||||||
|
if (isNaN(line.top) || isNaN(prevTop) || (Math.abs(prevTop - line.top) > 3)) {
|
||||||
i++;
|
i++;
|
||||||
lines[i] = line;
|
lines[i] = line;
|
||||||
}
|
}
|
||||||
|
prevTop = line.top;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -76,16 +84,8 @@ class ConvertPdf extends ConvertHtml {
|
|||||||
//найдем параграфы и отступы
|
//найдем параграфы и отступы
|
||||||
const indents = [];
|
const indents = [];
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const top = parseInt(line.top);
|
if (!isNaN(line.left)) {
|
||||||
const left = parseInt(line.left);
|
indents[line.left] = 1;
|
||||||
|
|
||||||
if (!isNaN(top)) {
|
|
||||||
line.top = top;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isNaN(left)) {
|
|
||||||
indents[left] = 1;
|
|
||||||
line.left = left;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user