Browse Source

Улучшение конвертирования Pdf

Book Pauk 6 years ago
parent
commit
c9b65a3c43
2 changed files with 30 additions and 23 deletions
  1. 11 4
      server/core/BookConverter/ConvertHtml.js
  2. 19 19
      server/core/BookConverter/ConvertPdf.js

+ 11 - 4
server/core/BookConverter/ConvertHtml.js

@@ -109,13 +109,20 @@ class ConvertHtml extends ConvertBase {
         //подозрение на чистый текст, надо разбить на параграфы
         if (isText || pars.length < buf.length/2000) {
             let total = 0;
+            let max = 0;
             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;
             }
 
-            total /= 20;
-            let i = spaceCounter.length - 1;
-            while (i > 0 && (!spaceCounter[i] || spaceCounter[i] < total)) i--;
+            let i = 0;
+            //если разброс не слишком большой
+            if (total < max*2) {
+                total /= 20;
+                i = spaceCounter.length - 1;
+                while (i > 0 && (!spaceCounter[i] || spaceCounter[i] < total)) i--;
+            }
 
             const parIndent = (i > 0 ? i : 0);
 

+ 19 - 19
server/core/BookConverter/ConvertPdf.js

@@ -22,16 +22,21 @@ class ConvertPdf extends ConvertHtml {
         const outFile = `${inputFiles.fileListDir}/${utils.randomHexString(10)}.xml`;
 
         //конвертируем в xml
-        await this.execConverter(this.pdfToHtmlPath, ['-c', '-s', '-xml', inputFiles.sourceFile, outFile]);
-        callback(50);
+        let perc = 0;
+        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);
-        callback(60);
+        callback(90);
 
         //парсим xml
         let lines = [];
         let inText = false;
         let title = '';
+        let prevTop = 0;
         let i = -1;
 
         const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
@@ -48,16 +53,19 @@ class ConvertPdf extends ConvertHtml {
                     let attrs = sax.getAttrsSync(tail);
                     const line = {
                         text: '',
-                        top: (attrs.top && attrs.top.value ? attrs.top.value : null),
-                        left: (attrs.left && attrs.left.value ? attrs.left.value : null),
-                        width: (attrs.width && attrs.width.value ? attrs.width.value : null),
-                        height: (attrs.height && attrs.height.value ? attrs.height.value : null),
+                        top: parseInt((attrs.top && attrs.top.value ? attrs.top.value : null), 10),
+                        left: parseInt((attrs.left && attrs.left.value ? attrs.left.value : null), 10),
+                        width: parseInt((attrs.width && attrs.width.value ? attrs.width.value : null), 10),
+                        height: parseInt((attrs.height && attrs.height.value ? attrs.height.value : null), 10),
                     };
 
                     if (line.width !== '0' || line.height !== '0') {
                         inText = true;
-                        i++;
-                        lines[i] = line;
+                        if (isNaN(line.top) || isNaN(prevTop) || (Math.abs(prevTop - line.top) > 3)) {
+                            i++;
+                            lines[i] = line;
+                        }
+                        prevTop = line.top;
                     }
                 }
             }
@@ -76,16 +84,8 @@ class ConvertPdf extends ConvertHtml {
         //найдем параграфы и отступы
         const indents = [];
         for (const line of lines) {
-            const top = parseInt(line.top);
-            const left = parseInt(line.left);
-
-            if (!isNaN(top)) {
-                line.top = top;
-            }
-
-            if (!isNaN(left)) {
-                indents[left] = 1;
-                line.left = left;
+            if (!isNaN(line.left)) {
+                indents[line.left] = 1;
             }
         }