Добавлено извлечение схемы документа в ConvertPdfImages, мелкий рефакторинг
This commit is contained in:
@@ -85,7 +85,7 @@ class ConvertDjvu extends ConvertJpegPng {
|
|||||||
files.sort((a, b) => a.base.localeCompare(b.base));
|
files.sort((a, b) => a.base.localeCompare(b.base));
|
||||||
|
|
||||||
//схема документа (outline)
|
//схема документа (outline)
|
||||||
const djvusedResult = await this.execConverter(djvusedPath, ['-u', '-e', 'print-outline', inputFiles.sourceFile]);
|
const djvusedResult = await this.execConverter(djvusedPath, ['-u', '-e', 'print-outline', inputFiles.sourceFile], null, abort);
|
||||||
const outline = [];
|
const outline = [];
|
||||||
const lines = djvusedResult.stdout.match(/\(".*"\s*?"#\d+".*?\)/g);
|
const lines = djvusedResult.stdout.match(/\(".*"\s*?"#\d+".*?\)/g);
|
||||||
if (lines) {
|
if (lines) {
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ class ConvertPdf extends ConvertHtml {
|
|||||||
const outFile = `${outBasename}.xml`;
|
const outFile = `${outBasename}.xml`;
|
||||||
|
|
||||||
const pdftohtmlPath = '/usr/bin/pdftohtml';
|
const pdftohtmlPath = '/usr/bin/pdftohtml';
|
||||||
|
|
||||||
if (!await fs.pathExists(pdftohtmlPath))
|
if (!await fs.pathExists(pdftohtmlPath))
|
||||||
throw new Error('Внешний конвертер pdftohtml не найден');
|
throw new Error('Внешний конвертер pdftohtml не найден');
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ const fs = require('fs-extra');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const utils = require('../../utils');
|
const utils = require('../../utils');
|
||||||
|
|
||||||
|
const sax = require('../../sax');
|
||||||
|
|
||||||
const ConvertJpegPng = require('./ConvertJpegPng');
|
const ConvertJpegPng = require('./ConvertJpegPng');
|
||||||
|
|
||||||
class ConvertPdfImages extends ConvertJpegPng {
|
class ConvertPdfImages extends ConvertJpegPng {
|
||||||
@@ -24,19 +26,25 @@ class ConvertPdfImages extends ConvertJpegPng {
|
|||||||
if (!await fs.pathExists(pdftoppmPath))
|
if (!await fs.pathExists(pdftoppmPath))
|
||||||
throw new Error('Внешний конвертер pdftoppm не найден');
|
throw new Error('Внешний конвертер pdftoppm не найден');
|
||||||
|
|
||||||
|
const pdftohtmlPath = '/usr/bin/pdftohtml';
|
||||||
|
if (!await fs.pathExists(pdftohtmlPath))
|
||||||
|
throw new Error('Внешний конвертер pdftohtml не найден');
|
||||||
|
|
||||||
|
const inpFile = inputFiles.sourceFile;
|
||||||
const dir = `${inputFiles.filesDir}/`;
|
const dir = `${inputFiles.filesDir}/`;
|
||||||
const baseFile = `${dir}${path.basename(inputFiles.sourceFile)}`;
|
const outBasename = `${dir}${utils.randomHexString(10)}`;
|
||||||
const jpgFiles = `${baseFile}.tmp`;
|
const outFile = `${outBasename}.tmp`;
|
||||||
|
|
||||||
//конвертируем в jpeg
|
//конвертируем в jpeg
|
||||||
let perc = 0;
|
let perc = 0;
|
||||||
await this.execConverter(pdftoppmPath, ['-jpeg', '-jpegopt', `quality=${pdfQuality},progressive=y`, inputFiles.sourceFile, jpgFiles], () => {
|
await this.execConverter(pdftoppmPath, ['-jpeg', '-jpegopt', `quality=${pdfQuality},progressive=y`, inpFile, outFile], () => {
|
||||||
perc = (perc < 100 ? perc + 1 : 40);
|
perc = (perc < 100 ? perc + 1 : 40);
|
||||||
callback(perc);
|
callback(perc);
|
||||||
}, abort);
|
}, abort);
|
||||||
|
|
||||||
const limitSize = 2*this.config.maxUploadFileSize;
|
const limitSize = 2*this.config.maxUploadFileSize;
|
||||||
let jpgFilesSize = 0;
|
let jpgFilesSize = 0;
|
||||||
|
|
||||||
//ищем изображения
|
//ищем изображения
|
||||||
let files = [];
|
let files = [];
|
||||||
await utils.findFiles(async(file) => {
|
await utils.findFiles(async(file) => {
|
||||||
@@ -53,19 +61,47 @@ class ConvertPdfImages extends ConvertJpegPng {
|
|||||||
files.sort((a, b) => a.base.localeCompare(b.base));
|
files.sort((a, b) => a.base.localeCompare(b.base));
|
||||||
|
|
||||||
//схема документа (outline)
|
//схема документа (outline)
|
||||||
//const djvusedResult = await this.execConverter(djvusedPath, ['-u', '-e', 'print-outline', inputFiles.sourceFile]);
|
const outXml = `${outBasename}.xml`;
|
||||||
|
await this.execConverter(pdftohtmlPath, ['-nodrm', '-i', '-c', '-s', '-xml', inpFile, outXml], null, abort);
|
||||||
const outline = [];
|
const outline = [];
|
||||||
/*const lines = djvusedResult.stdout.match(/\(".*"\s*?"#\d+".*?\)/g);
|
|
||||||
if (lines) {
|
let inOutline = 0;
|
||||||
lines.forEach(l => {
|
let inItem = false;
|
||||||
const m = l.match(/"(.*)"\s*?"#(\d+)"/);
|
let pageNum = 0;
|
||||||
if (m) {
|
|
||||||
outline[m[2]] = m[1];
|
const onTextNode = (text, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||||
}
|
if (inOutline > 0 && inItem && pageNum) {
|
||||||
});
|
outline[pageNum] = text;
|
||||||
}*/
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onStartNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||||
|
if (tag == 'outline')
|
||||||
|
inOutline++;
|
||||||
|
|
||||||
|
if (inOutline > 0 && tag == 'item') {
|
||||||
|
const attrs = sax.getAttrsSync(tail);
|
||||||
|
pageNum = (attrs.page && attrs.page.value ? attrs.page.value : 0);
|
||||||
|
inItem = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onEndNode = (tag, tail, singleTag, cutCounter, cutTag) => {// eslint-disable-line no-unused-vars
|
||||||
|
if (tag == 'outline')
|
||||||
|
inOutline--;
|
||||||
|
if (tag == 'item')
|
||||||
|
inItem = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
const dataXml = await fs.readFile(outXml);
|
||||||
|
const buf = this.decode(dataXml).toString();
|
||||||
|
sax.parseSync(buf, {
|
||||||
|
onStartNode, onEndNode, onTextNode
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
await utils.sleep(100);
|
await utils.sleep(100);
|
||||||
|
//формируем список файлов
|
||||||
let i = 0;
|
let i = 0;
|
||||||
const imageFiles = files.map(f => {
|
const imageFiles = files.map(f => {
|
||||||
i++;
|
i++;
|
||||||
|
|||||||
Reference in New Issue
Block a user