Перевод pdf-конвертера на использование pdfalto
This commit is contained in:
@@ -24,9 +24,14 @@ class ConvertPdf extends ConvertHtml {
|
|||||||
const inpFile = inputFiles.sourceFile;
|
const inpFile = inputFiles.sourceFile;
|
||||||
const outFile = `${inputFiles.filesDir}/${utils.randomHexString(10)}.xml`;
|
const outFile = `${inputFiles.filesDir}/${utils.randomHexString(10)}.xml`;
|
||||||
|
|
||||||
|
const pdfaltoPath = `${this.config.dataDir}/pdfalto/pdfalto`;
|
||||||
|
|
||||||
|
if (!await fs.pathExists(pdfaltoPath))
|
||||||
|
throw new Error('Внешний конвертер pdfalto не найден');
|
||||||
|
|
||||||
//конвертируем в xml
|
//конвертируем в xml
|
||||||
let perc = 0;
|
let perc = 0;
|
||||||
await this.execConverter(this.pdfToHtmlPath, ['-nodrm', '-c', '-s', '-xml', inpFile, outFile], () => {
|
await this.execConverter(pdfaltoPath, [inpFile, outFile], () => {
|
||||||
perc = (perc < 80 ? perc + 10 : 40);
|
perc = (perc < 80 ? perc + 10 : 40);
|
||||||
callback(perc);
|
callback(perc);
|
||||||
}, abort);
|
}, abort);
|
||||||
@@ -39,13 +44,10 @@ class ConvertPdf extends ConvertHtml {
|
|||||||
let lines = [];
|
let lines = [];
|
||||||
let images = [];
|
let images = [];
|
||||||
let loading = [];
|
let loading = [];
|
||||||
let inText = false;
|
|
||||||
let bold = false;
|
|
||||||
let italic = false;
|
|
||||||
let title = '';
|
let title = '';
|
||||||
let prevTop = 0;
|
let prevTop = 0;
|
||||||
let i = -1;
|
let i = -1;
|
||||||
let titleCount = 0;
|
|
||||||
|
|
||||||
const loadImage = async(image) => {
|
const loadImage = async(image) => {
|
||||||
const src = path.parse(image.src);
|
const src = path.parse(image.src);
|
||||||
@@ -71,97 +73,68 @@ class ConvertPdf extends ConvertHtml {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
/* let tOpen = (bold ? '<b>' : '');
|
||||||
if (!cutCounter && inText) {
|
|
||||||
let tOpen = (bold ? '<b>' : '');
|
|
||||||
tOpen += (italic ? '<i>' : '');
|
tOpen += (italic ? '<i>' : '');
|
||||||
let tClose = (italic ? '</i>' : '');
|
let tClose = (italic ? '</i>' : '');
|
||||||
tClose += (bold ? '</b>' : '');
|
tClose += (bold ? '</b>' : '');
|
||||||
|
lines[i].text += `${tOpen}${text}${tClose} `;*/
|
||||||
|
const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||||
|
if (tag == 'page')
|
||||||
|
putImage(100000);
|
||||||
|
|
||||||
lines[i].text += `${tOpen}${text}${tClose} `;
|
if (tag == 'textline') {
|
||||||
if (titleCount < 2 && text.trim() != '') {
|
const attrs = sax.getAttrsSync(tail);
|
||||||
title += text + (titleCount ? '' : ' - ');
|
const line = {
|
||||||
titleCount++;
|
text: '',
|
||||||
|
top: parseInt((attrs.vpos && attrs.vpos.value ? attrs.vpos.value : null), 10),
|
||||||
|
left: parseInt((attrs.hpos && attrs.hpos.value ? attrs.hpos.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) {
|
||||||
|
if (isNaN(line.top) || isNaN(prevTop) || (Math.abs(prevTop - line.top) > 3)) {
|
||||||
|
putImage(line.top);
|
||||||
|
i++;
|
||||||
|
lines[i] = line;
|
||||||
|
}
|
||||||
|
prevTop = line.top;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
if (tag == 'string') {
|
||||||
if (!cutCounter) {
|
const attrs = sax.getAttrsSync(tail);
|
||||||
if (inText) {
|
if (attrs.content && attrs.content.value) {
|
||||||
switch (tag) {
|
lines[i].text += `${attrs.content.value} `;
|
||||||
case 'i':
|
|
||||||
italic = true;
|
|
||||||
break;
|
|
||||||
case 'b':
|
|
||||||
bold = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (tag == 'text' && !inText) {
|
if (tag == 'illustration') {
|
||||||
let attrs = sax.getAttrsSync(tail);
|
const attrs = sax.getAttrsSync(tail);
|
||||||
const line = {
|
if (attrs.type && attrs.type.value == 'image') {
|
||||||
text: '',
|
let src = (attrs.fileid && attrs.fileid.value ? attrs.fileid.value : '');
|
||||||
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;
|
|
||||||
if (isNaN(line.top) || isNaN(prevTop) || (Math.abs(prevTop - line.top) > 3)) {
|
|
||||||
putImage(line.top);
|
|
||||||
i++;
|
|
||||||
lines[i] = line;
|
|
||||||
}
|
|
||||||
prevTop = line.top;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tag == 'image') {
|
|
||||||
const attrs = sax.getAttrsSync(tail);
|
|
||||||
const src = (attrs.src && attrs.src.value ? attrs.src.value : '');
|
|
||||||
if (src) {
|
if (src) {
|
||||||
const image = {
|
const image = {
|
||||||
isImage: true,
|
isImage: true,
|
||||||
src,
|
src,
|
||||||
data: '',
|
data: '',
|
||||||
type: '',
|
type: '',
|
||||||
top: parseInt((attrs.top && attrs.top.value ? attrs.top.value : null), 10) || 0,
|
top: parseInt((attrs.vpos && attrs.vpos.value ? attrs.vpos.value : null), 10) || 0,
|
||||||
};
|
};
|
||||||
loading.push(loadImage(image));
|
loading.push(loadImage(image));
|
||||||
images.push(image);
|
images.push(image);
|
||||||
images.sort((a, b) => a.top - b.top)
|
images.sort((a, b) => a.top - b.top)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tag == 'page') {
|
|
||||||
putImage(100000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
/*
|
||||||
const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||||
if (inText) {
|
|
||||||
switch (tag) {
|
|
||||||
case 'i':
|
|
||||||
italic = false;
|
|
||||||
break;
|
|
||||||
case 'b':
|
|
||||||
bold = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tag == 'text')
|
|
||||||
inText = false;
|
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
let buf = this.decode(data).toString();
|
let buf = this.decode(data).toString();
|
||||||
sax.parseSync(buf, {
|
sax.parseSync(buf, {
|
||||||
onStartNode, onEndNode, onTextNode
|
onStartNode
|
||||||
});
|
});
|
||||||
|
|
||||||
putImage(100000);
|
putImage(100000);
|
||||||
|
|||||||
@@ -287,6 +287,7 @@ function getAttrsSync(tail) {
|
|||||||
let waitEq = false;
|
let waitEq = false;
|
||||||
|
|
||||||
const pushResult = () => {
|
const pushResult = () => {
|
||||||
|
name = name.toLowerCase();
|
||||||
if (name != '') {
|
if (name != '') {
|
||||||
let ns = '';
|
let ns = '';
|
||||||
if (name.indexOf(':') >= 0) {
|
if (name.indexOf(':') >= 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user