-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtf2pdf.js
1030 lines (935 loc) · 43.1 KB
/
rtf2pdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Module for the management of documents, RTF DOCUMENTS and generation of PDFs from
* Data (Json) or PDF to text
* OpenOffice, Tesseract and Gostscript are required for conversion
* @module rtf2pdf
* @version 0.0.1
* @license MIT
* @author jankstar
*
*/
const rtf2pdf = (() => {
const pdfjsLib = require('pdfjs-dist/legacy/build/pdf.js');
const fs = require('fs').promises;
const path = require('path');
const promise_exec = require("child_process").exec;
const { v4: uuidv4 } = require('uuid');
/**
* @property {Array} DOC_BUFFER Array of documents in memory
* @property {String} DIRECTORY the directory used for files
* @property {Number} BUFFER the buffer for the EXEC
* @property {String} LANGU the language in ISO natation
* @property {Array} STATUS Array of strings as Status
*/
var DOC_BUFFER = [], //Documents in local memory
DIRECTORY = 'temp/', //Directory for temp data
BUFFER = (1024 * 500),
LANGU = 'en-UK', //Language for converting
STATUS = ['01_new'] //Document status info
/**
* Executes cmd as a shell command in a promis and returns stdout
* @param {String} cmd Shell Command started as Promise
* @returns {Promise} Promise returns stdout or error
* @property {Number} BUFFER the buffer for the EXEC
*/
function exec(cmd) {
return new Promise((resolve, reject) => {
try {
console.log(cmd);
promise_exec(cmd, { maxBuffer: BUFFER }, (error, stdout, stderr) => {
if (error) {
reject(error);
} else if (stdout) {
resolve(stdout);
} else {
resolve(stderr);
}
});
} catch (err) {
reject(err);
}
});
}
/**
* Returns the language from ISO in tesseract notation
* @param {String} iLangu Language in ISO e.g. 'de-DE'.
* @returns {String} Language for tesseract e.g. 'deu'.
*/
function _convertLangu(iLangu) {
if (iLangu.substring(0, 2) == 'de') return 'deu';
if (iLangu.substring(0, 2) == 'en') return 'eng';
}
/**
* Decoding Umlaute utf-8 in ascii/latin1
* @param { String } str data string input
* @returns { String } Converted data string
*/
function _rtfDecoding(str) {
str = str.replace(/\xa0/gm, ' '); //no-break Space wird Space
str = str.replace(/[^\x00-\x7f]/gm, function (match) {
if (match == "€") return "\\'80"; //Euro ist reines Unicode, das geht nicht
return "\\'" + ((match.charCodeAt(0).toString(16))).slice(-4)
})
return str;
}
/**
* Saves a base64 as a file;
* generates a file name from uuid in temp directory if necessary
* @async
* @param {String} iBase64 data from the binary file
* @param {String} iFilename Name of the file, if not given, then generated in temp
* @returns {*} { filename, error } of the file
*/
async function _base64ToFile(iBase64, iFilename) {
try {
if (!iBase64) { throw Error(`Base64 data must be provided.`) }
let lFieldName = iFilename || DIRECTORY + uuidv4() + '.pdf'
const buffDocument = Buffer.from(iBase64, 'base64');
await fs.writeFile(lFieldName, buffDocument, 'binary');
return { filename: lFieldName, error: undefined }
} catch (err) {
console.log(err)
return { filename: undefined, error: err }
}
}
/**
* Creates a base64 string from a file
* @async
* @param {String} iFilename name of a file (binary)
* @returns {String} base64 data from the file
*/
async function _fileToBase64(iFilename) {
let lData = undefined
try {
if (!iFilename) { throw Error(`No filename specified.`) }
lData = await fs.readFile(iFilename, 'binary')
const lBuff = Buffer.from(lData,'binary')
return lBuff.toString('base64');
} catch (err) {
console.log(err)
}
return undefined;
}
/**
* Replaces variables from an RTF string
* @param {String} iLangu Sprache, z.B. 'de-DE'
* @param {Object} iVar Variablen als json
* @param {String} iData rtf-Daten file
* @param {Array} iElements Array der Elemente (Tabelle/Felder)
* @param {Number} iCount Index bei Array als Sub-Aufruf eines Array
* @returns {String} Rückgabewert als rtf-Daten file
*/
function _replaceVarInRTF(iLangu, iVar, iData, iElements, iCount, iProtocol) {
let rData = '';
try {
iLangu = iLangu || LANGU || 'de-DE';
iVar = iVar || {};
iElements = iElements || [];
iProtocol = iProtocol || [];
if (typeof iVar != 'object') {
iVar = {}
iProtocol.push("Error - iVar is not a object.")
}
let lBefore = undefined;
iElements.forEach((element) => {
if (!lBefore) {
//der erste Eintrag
rData = iData.substring(0, element.start)
} else {
//wir starten bei dem Ende des vorherigen Feld
rData += iData.substring(lBefore.end, element.start)
}
if (element.type == "field") {
//jetzt das Feld ersetzen, wenn möglich
//zuerst mögloche json Parameter quoten
let lElementName = element.name.replace(/\"[]*: /gm, '"=').replace(/\'[]*: /gm, "'=");
//jetzt die Patameter trennen - 0 ist Name, alle anderen sind Parameter
let lFieldName = lElementName.split(/:/);
if (lFieldName && lFieldName[0]) {
let lFieldNameLevel = lFieldName[0].split('.');
let lReplace = iVar
lFieldNameLevel.forEach((varLevel) => {
if (typeof iCount == 'number' && lReplace[iCount] && lReplace[iCount][varLevel]) {
//das ist ein array und ein Feld
lReplace = lReplace[iCount][varLevel];
} else if (lReplace[varLevel]) {
//das ist nur ein Feld
lReplace = lReplace[varLevel];
}
})
if (typeof lReplace == 'string' || typeof lReplace == 'number' || lReplace instanceof Date) {
if (typeof lReplace == 'string' && /\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}[0-9\.\+Z]*/.test(lReplace)) {
//Datum kann auch als String formatiert sein wg. json, dann in Date object umwandeln
lReplace = new Date(lReplace);
}
if (typeof lReplace == 'string') {
lReplace = _rtfDecoding(lReplace.toString('utf8'));
}
if (typeof lReplace == 'number' || lReplace instanceof Date) {
if (!lFieldName[1]) {
//sytle
lReplace = _rtfDecoding(lReplace.toLocaleString(iLangu));
} else {
try {
//jetzt den 1ten Parameter wieder zurück zum Json konvertieren
let option = JSON.parse('{ ' + lFieldName[1].replace(/=/gm, ':').replace(/'/gm, '"') + ' }');
lReplace = _rtfDecoding(lReplace.toLocaleString(iLangu, option));
} catch (err) {
console.log(err.message);
lReplace = _rtfDecoding(lReplace.toLocaleString(iLangu));
}
}
}
rData += `${lReplace}`;
iProtocol.push(`Replace "${iData.substring(element.start, element.end)}" with "${lReplace}"`)
} else {
console.log(`Variable for field ${lFieldName} not found.`)
iProtocol.push(`Variable for field ${lFieldName} not found.`)
}
}
} else if (element.type == "array") {
//Determine the number of entries in the first array
let lCountArray = 0;
let lReplace = iVar
element.fields.forEach((field) => {
if (lCountArray > 0) { return } //only the first array!
let lFieldName = field.name.split(/:/);
if (lFieldName && lFieldName[0]) {
let lFieldNameLevel = lFieldName[0].split('.');
lFieldNameLevel.forEach((varLevel) => {
if (lReplace[0]) {
//this field is a (first) array
return
} else if (lReplace[varLevel]) {
//that is only one field
lReplace = lReplace[varLevel];
}
})
}
if (lReplace[0]) {
lCountArray = lReplace.length;
}
})
for (i = 0; i < lCountArray; i++) {
rData += _replaceVarInRTF(iLangu, iVar, element.name, element.fields, i, iProtocol)
}
} else {
//Error
}
lBefore = element;
})
if (!lBefore) {
//there were no fields ...
rData = iData
} else {
//append the rest to the last field
rData += iData.substring(lBefore.end, iData.length)
}
} catch (err) {
console.log(err);
iProtocol.push(`Error: ${err.message}`)
}
return rData
}
/**
* Creates an elements array from an RTF string for RTF tags and variables.
* @param {String} iData rtf-Data file as string
* @returns {Array} Elements als Array
*/
function _createElementsFromRTF(iData) {
let lMyMap = [];
let lRexEx2 = /:#field:|:#array:|:#include:|#:|\\row }|\\row \\/gm;
lMyMap = Array.from(iData.matchAll(lRexEx2), (m) => {
return { tag: m[0], index: m["index"] };
});
let elements = []
let field = { type: "field", start: 0, end: 0, start_tag: undefined, end_tag: undefined, name: '' };
let table = { type: "array", start: 0, end: 0, start_tag: undefined, end_tag: undefined, name: '', fields: [], trowd: undefined }
lMyMap.forEach((element) => {
if (element.tag.substring(0, 2) == ':#') {
if (field.start > 0) {
console.log(`Start-Element ${element.tag} ohne Ende-Tag.`)
}
field = { type: "field", start: 0, end: 0, start_tag: undefined, end_tag: undefined, name: '' };
field.start = element.index;
field.start_tag = element;
}
if (element.tag == '#:') {
if (field.start && field.start >= 0) {
field.end = element.index + element.tag.length;
field.end_tag = element;
field.name = iData.substring(field.start + field.start_tag.tag.length, field.end - field.end_tag.tag.length)
field.name = field.name.replace(/\\[a-zA-Z0-9]|{|}/gm, ""); //no rtf control indicators in variables
field.name = field.name.replace(/[^\x20-\xff]/gm, ""); //no control characters like nl
if (table.start && table.start >= 0) {
//it is a field in a table
field.start -= table.start;
field.end -= table.start;
table.fields.push(field)
} else {
//it is a field without a table
elements.push(field)
}
}
field = { type: "field", start: 0, end: 0, start_tag: undefined, end_tag: undefined, name: '' };
}
if (element.tag == '\\row \\') {
//Ende einer inneren Zeile der Tabelle
table = { type: "array", start: 0, end: 0, start_tag: undefined, end_tag: undefined, name: '', fields: [], trowd: undefined }
table.start = element.index;
table.start_tag = element;
}
if (element.tag == '\\row }') {
//last row of a table - tag,
if (table.start && table.start >= 0
//only if there are also fields in the table!
&& table.fields.length > 0) {
table.end = element.index;
table.end_tag = element;
table.name = iData.substring(table.start, table.end);
//console.log(`${iData.substring(table.start, table.start + 10)} ** ${iData.substring(table.end - 10, table.end)}`)
elements.push(table)
}
table = { type: "array", start: 0, end: 0, start_tag: undefined, end_tag: undefined, name: '', fields: [], trowd: undefined }
}
});
return elements;
}
/**
* Class representing a document
* @example
* let lDoc = new Document({subject: 'test', filename:'test.pdf', type: 'PDF'})
*
*/
class Document {
/**
* Create a document
* @param {*} value is a structure of the fields of a document
* id
* date
* subject
* status
* task
* in_use
* filename
* file_extension
* type
* root_of
* template
* data
* info
* metadata
* cathegory
* langu
* num_page
* body
* base64
* protocol
*/
constructor(value) {
var now = new Date();
value = value || {}
this.id = value.id || uuidv4()
this.date = value.date || now.toISOString()
this.subject = value.subject || ""
this.status = value.status || STATUS[0]
this.task = value.task || "create"
this.in_use = value.in_use || "completed"
this.filename = value.filename || ""
this.file_extension = path.extname(this.filename.toLowerCase()) || ""
this.type = value.type || ""
if (!this.type) {
this.type = this.file_extension.replaceAll(/\./g,'') //clear all points
}
this.type = this.type.toUpperCase()
this.root_of = value.root_of || ""
this.template = value.template || ""
this.data = value.data || {}
this.info = value.info || {}
this.metadata = value.metadata || {}
this.category = value.category || []
this.inputpath = value.inputpath || ""
this.langu = value.langu || "de-DE"
this.num_pages = value.num_pages || 0
this.body = value.body || ""
this.base64 = value.base64 || ""
//
this.protocol = []
this.is_file = false || (this.filename != '');
}
setValue(value) {
value = value || {}
this.id = value.id || this.id
this.date = value.date || this.date
this.subject = value.subject || this.subject
this.status = value.status || this.status
this.task = value.task || this.task
this.in_use = value.in_use || this.in_use
this.filename = value.filename || this.filename
this.type = value.type || this.type
this.type = this.type.toUpperCase()
this.root_of = value.root_of || this.root_of
this.template = value.template || this.template
this.data = value.data || this.data
this.info = value.info || this.info
this.metadata = value.metadata || this.metadata
this.category = value.category || this.category
this.inputpath = value.inputpath || this.inputpath
this.langu = value.langu || this.langu
this.num_pages = value.num_pages || this.num_pages
this.body = value.body || this.body
this.base64 = value.base64 || this.base64
this.is_file = false || (this.filename != '');
}
/**
* Checks in the document, i.e., the file in filename becomes a base64 and the file is deleted
* @async
* @param {Boolean} iNotDel if set, the file is not deleted
*/
async checkIn(iNotDel) {
try {
if (!this.type) { throw Error(`Type of document not defined.`) }
if (!this.base64) {
if (!this.filename) { throw Error(`CheckIn requires either base64 or a file (filename).`) }
this.base64 = await _fileToBase64(this.filename)
}
if (!iNotDel && this.filename) {
fs.rm(this.filename)
}
} catch (err) {
console.log(err)
this.protocol.push(`Error: ${err.message}`)
}
}
/**
* Checks out the document, i.e. a file is created from the base64 in filename
* @async
*/
async checkOut() {
let { filename: lFilename, error: err } = await _base64ToFile(this.base64, this.filename)
if (err) {
console.log(err)
this.protocol.push(`Error: ${err.message}`)
} else {
this.filename = lFilename
}
}
/**
* @param {Boolean} iReWrite true -> rewrite the template
* @returns true/false
*/
isTemplate(iReWrite) {
try {
if (!this.template) throw Error("No template name found.")
if (this.type != 'RTF') throw Error(`Wrong type ${this.type}`)
if (!this.subject) throw Error("Subject is missing.")
let newTemplate = rtf2pdf.findDocBySubject(this.subject, this.type, this.template)
if (newTemplate) {
if (!iReWrite) throw Error(`There is already a template with this name ${this.template}.`)
newTemplate = this;
} else {
DOC_BUFFER.push(this)
this.protocol.push("Document saved as template.")
}
return true;
} catch (err) {
console.log(err)
this.protocol.push(`Error: ${err.message}`)
return false;
}
}
/**
* Converts a PDF (file or base64) to text Body, start OCR if necessary.
* @async
* @property {String} filename
* @property {String} base64
* @return {String} Body
*/
async convertPDFToTextBody() {
let me = this
try {
if (this.type != 'PDF') { throw Error(`Wrong document type ${me.type} - PDF expects.`) }
if (!this.filename) {
//a file is required -> base64 to file
if (this.base64) {
let { filename: lFilename, error: err } = await _base64ToFile(this.base64)
if (err) { throw Error(err) }
if (lFilename) {
this.filename = lFilename
} else {
throw Error(`No file created.`)
}
} else {
throw Error("If no file, then base64 is required.")
}
}
const loadingTask = pdfjsLib.getDocument(this.filename);
await loadingTask.promise
.then(async (doc) => {
//Process Document
me.num_pages = doc.numPages;
me.protocol.push(`# Document Loaded '${me.filename}'`);
me.protocol.push(`# Number of Pages: '${me.num_pages}'`);
let lastPromise; // will be used to chain promises
lastPromise = doc.getMetadata().then((data) => {
me.protocol.push("# Metadata Loaded");
me.info = data.info;
me.metadata = data.metadata;
}, (err) => {
//when reject
me.protocol.push(err.message)
console.log(err.message)
});
//loadPage is a function for processing a page
const loadPage = function (pageNum) {
//Process page
return doc
.getPage(pageNum)
.then((page) => {
me.protocol.push(`# Page ${pageNum}`);
const viewport = page.getViewport({ scale: 1.0 });
me.protocol.push(`# Size: ${viewport.width} x ${viewport.height}`);
return page
.getTextContent()
.then((content) => {
// Content contains lots of information about the text layout and
// styles, but we need only strings at the moment
const strings = content.items.map((item) => {
return item.str;
});
me.protocol.push(`## Text Content`);
me.body = me.body + strings;
if (me.body) {
me.body = me.body + "\n";
me.task = 'OCR';
}
}, (err) => {
//when reject
me.protocol.push(err.message)
console.log(err.message)
})
}, (err) => {
//when reject
me.protocol.push(err.message)
console.log(err.message)
});
};
// Loading of the first page will wait on metadata and subsequent loadings
// will wait on the previous pages.
for (let i = 1; i <= doc.numPages; i++) {
lastPromise = lastPromise.then(loadPage.bind(null, i));
}
return lastPromise;
})
.then(async () => {
me.protocol.push(`# End of Document`);
if (!me.body || me.body == "") {
me.protocol.push(`# Start GS for jpeg extracting`);
await fs.mkdir(DIRECTORY + me.id);
me.task = 'converting'
const gs_data = await exec(`gs -dSAFER -dBATCH -dNOPAUSE -r1200 -sDEVICE=jpeg -sOutputFile=${DIRECTORY + me.id}/page%03d.jpg ` + me.filename)
me.protocol.push(gs_data);
var files = await fs.readdir(DIRECTORY + me.id)
files = files.sort()
for (let file of files) {
if (path.extname(file) == '.jpg') {
me.protocol.push(`# Read and OCR convert file ${file} by tesseract`);
me.task = 'OCR'
const tes_data = await exec(`tesseract ${DIRECTORY + me.id}/${file} ${DIRECTORY + me.id}/${file} -l ${_convertLangu(me.langu)} ${__dirname}/gosseract.ini`)
if (tes_data) {
me.protocol.push(`Tesseract: ${tes_data}`);
} else {
me.protocol.push(`Tesseract ends without a message.`);
}
const file_data = await fs.readFile(DIRECTORY + me.id + "/" + file + ".txt", 'utf8')
me.body = me.body + file_data;
//now the files jpg/txt can be deleted
await fs.rm(DIRECTORY + me.id + "/" + file + ".txt");
await fs.rm(DIRECTORY + me.id + "/" + file);
}
}
//now the directory can be deleted
await fs.rmdir(DIRECTORY + me.id);
}
me.protocol.push(`# Conversion of the docuemnt ${me.id} successfully completed.`);
}, (err) => {
//when reject
me.protocol.push(`Error from GS exec: ${err.message}`);
console.log(err.message)
}
);
} catch (err) {
me.protocol.push(`Error: ${err.message}`);
console.log(err.message)
}
return this.body
}
}
return {
Document,
/**
* Sets the module variables DIRECTORY, BUFFER, LANGU, STATUS[]
* @param {Object} iValue as {DIRECTORY,BUFFER,LANGU,STATUS}
*/
setConfig(iValue) {
let lValue = iValue || {}
DIRECTORY = lValue.DIRECTORY || DIRECTORY;
BUFFER = lValue.BUFFER || BUFFER
LANGU = lValue.LANGU || LANGU
STATUS = lValue.STATUS || STATUS
},
/**
* Saves a base64 as a file;
* generates a file name from uuid in temp directory if necessary
* @async
* @param {String} iBase64 data from the binary file
* @param {String} iFilename Name of the file, if not given, then generated in temp
* @returns {*} { filename, error } of the file
*/
base64ToFile: _base64ToFile,
/**
* Creates a base64 string from a file
* @async
* @param {String} iFilename name of a file (binary)
* @returns {String} base64 data from the file
*/
fileToBase64: _fileToBase64,
/**
* Decoding Umlaute utf-8 in ascii/latin1
* @param { String } str data string input
* @returns { String } Converted data string
*/
rtfDecoding: _rtfDecoding,
/**
* Checks and installs ghostscript, tesseract und libreOffice
* @async
* @param {Boolean} iInstall true - it is installed if not found
* @param {String} iLangu the language for tesseract
* @returns {Object} { data: {gs, tesserct, soffice}, error:[String]}
*/
async checkInstallation(iInstall, iLangu) {
iLangu = iLangu || 'de-DE';
let lLanguOffice = iLangu.substring(0, 2)
const lInst = [
{
"wich": "which gs",
"check": "gs",
"install": "apt-get -y update; apt-get -y install ghostscript",
"error": "Error from installation ghostscript:",
"return": "gs",
"success": "The gs programme is installed"
}, {
"wich": "which tesseract",
"check": "tesseract",
"install": 'apt-get -y update; apt-get -y install tesseract-ocr; apt-get -y install tesseract-ocr-' + _convertLangu(iLangu),
"error": "Error from installation tesseract-ocr:",
"return": "tesseract",
"success": "The tesseract programme is installed"
}, {
"wich": "which soffice",
"check": "soffice",
"install": 'apt-get -y update; apt-get -y install libreoffice libreoffice-l10n-' + lLanguOffice + ' libreoffice-help-' + lLanguOffice,
"error": "Error from installation libreoffice:",
"return": "soffice",
"success": "The soffice programme is installed"
}
]
var lData = {
"gs": false,
"tesseract": false,
"soffice": false
}
var lCheckThis = ''
var lInstallMe = ''
var ltError = []
try {
for (let i = 0; i < lInst.length; i++) {
lCheckThis = await exec(lInst[i].wich);
if (!lCheckThis.includes(lInst[i].check)) {
if (iInstall) {
//Programm nicht gefunden -> installieren
lInstallMe = await exec(lInst[i].install)
lCheckThis = await exec(lInst[i].wich);
if (!lCheckThis.includes(lInst[i].check)) {
ltError.push(`${lInst[i].error}: ${lInst[i].install}: ${lInstallMe}`);
lData[lInst[i].return] = false;
} else {
lData[lInst[i].return] = true;
}
} else {
ltError.push(`${lInst[i].error}`);
lData[lInst[i].return] = false;
}
} else {
lData[lInst[i].return] = true;
}
if (lData[lInst[i].return] == true) {
console.log(lInst[i].success)
} else {
console.log(lInst[i].error)
}
}
return { data: lData, error: ltError };
} catch (err) {
ltError.push(err.message);
return { data: lData, error: ltError };
}
},
/**
* Returns a document for a name and a type (optional)
* @param {String} iSubject
* @param {String} iType optional
* @param {String} iTemplate optional
* @returns {Document}
*/
findDocBySubject(iSubject, iType, iTemplate) {
for (let index = 0; index < DOC_BUFFER.length; index++) {
if (iType && iTemplate) {
if (DOC_BUFFER[index].subject == iSubject
&& DOC_BUFFER[index].type == iType
&& DOC_BUFFER[index].template == iTemplate) {
return new Document(DOC_BUFFER[index]);
}
} else {
if (iType) {
if (DOC_BUFFER[index].subject == iSubject && DOC_BUFFER[index].type == iType) {
return new Document(DOC_BUFFER[index]);
}
} else {
if (iTemplate) {
if (DOC_BUFFER[index].subject == iSubject
&& DOC_BUFFER[index].template == iTemplate) {
return new Document(DOC_BUFFER[index]);
}
} else {
if (DOC_BUFFER[index].subject == iSubject) {
return new Document(DOC_BUFFER[index]);
}
}
}
}
}
return undefined
},
/**
* Supplies the data for a template by name (template) as string
* @param {String} iName Template name
* @returns {*} { doc: Document, error: Error }
*/
async getTemplate(iName) {
let lDoc = undefined;
try {
lDoc = rtf2pdf.findDocBySubject(iName, 'RTF', iName)
if (!lDoc) { throw Error(`Template ${iName} not found.`) }
if (!lDoc.body) {
if (!lDoc.filename) { throw Error(`Template ${iName} cannot be loaded.`) }
lDoc.body = await fs.readFile(lDoc.filename, 'utf-8');
}
return { doc: lDoc, error: undefined };
} catch (err) {
return { doc: undefined, error: err }
}
},
/**
* Defines a template and writes it to the buffer array
* @param {String} iName
* @param {String} iFileName
* @returns {Document} Template
*/
newTemplateByFile(iName, iFileName) {
try {
let lDoc = new Document({ subject: iName, template: iName, filename: iFileName, task: 'RTF_from_File' });
lDoc.protocol.push(`Start 'newTemplateByFile' with name ${iName}`)
if (!lDoc.subject) { throw Error(`The name of the template or the subject must be specified.`) }
if (!iFileName) { throw Error(`A file name must be specified.`) }
if (path.extname(iFileName.toLowerCase()) != '.rtf') { throw Error(`The filename must be RTF for a template.`) }
lDoc.type = 'RTF'
let newTemplate = rtf2pdf.findDocBySubject(lDoc.subject, lDoc.type, lDoc.template)
if (newTemplate) {
throw Error(`There is already a template with this name ${iName}.`)
} else {
DOC_BUFFER.push(lDoc)
}
return lDoc;
} catch (err) {
this.protocol.push(`Error: ${err.message}`);
return undefined;
}
},
/**
* Generates a document (correspondence) for a template with specified data.
* @async
* @param {*} iLangu Language used for the document in ISO
* @param {*} iTemplate Template used
* @param {*} iVar Variables for replacement in the template json structure
* @returns {Document} returns a document with the RFT in the body
*/
async createDocAsCorrespondence(iLangu, iTemplate, iVar) {
let lDoc = new Document({ subject: `Correspondence of template ${iTemplate}`, template: iTemplate, task: 'Create_Correspondence' });
lDoc.protocol.push(`Start 'createDocAsCorrespondence' with template ${iTemplate}`)
try {
let { doc: lTemplate, error: err } = await this.getTemplate(iTemplate);
if (err || !lTemplate || !lTemplate.body) {
err = err || {}
err.message = err.message || 'template not found'
console.log(err)
lDoc.protocol.push(`Error: ${err.message}`)
return lDoc;
}
let elements = _createElementsFromRTF(lTemplate.body);
//console.log(elements);
//Replace fields
lDoc.body = _replaceVarInRTF(iLangu, iVar, lTemplate.body, elements, undefined, lDoc.protocol);
lDoc.protocol.push(`Correspondence created.`)
lDoc.type = 'RTF';
return lDoc;
} catch (err) {
err.message = err.message || err;
console.log(err.message)
lDoc.protocol.push(`Error: ${err.message}`)
return lDoc;
}
},
/**
* Converts RTF to PDF and delivers row data (string as binary)
* @async
* @param {String} iData as RTF
* @returns {Object} { data , rtf_filename , pdf_filename ,error } data is PDF as string in raw data (binary)
*/
async convertRtfToPDFByData(iData, iNotDel) {
try {
let lFileName = uuidv4();
await fs.writeFile(`${DIRECTORY}${lFileName}.rtf`, iData, 'utf8');
let lMessage = await exec(`soffice --headless --norestore --convert-to pdf ${DIRECTORY}${lFileName}.rtf --outdir ${DIRECTORY}`);
let lData = await fs.readFile(`${DIRECTORY}${lFileName}.pdf`, 'binary');
if (!iNotDel) {
fs.rm(`${DIRECTORY}${lFileName}.rtf`);
fs.rm(`${DIRECTORY}${lFileName}.pdf`);
return { data: lData, rtf_filename: '', pdf_filename: '', error: undefined }
}
return { data: lData, rtf_filename: `${DIRECTORY}${lFileName}.rtf`, pdf_filename: `${DIRECTORY}${lFileName}.pdf`, error: undefined }
} catch (err) {
return { data: undefined, rtf_filename: '', pdf_filename: '', error: err }
}
},
/**
* Generates a PDF file from an RTF file
* @async
* @param {Document} iDoc RDF document
* @param {Boolean} iNotDel if 'true', the temporary files for the RTF and PDF document are not deleted but returned
* @returns {Document} a new document as PDF
*/
async convertRtfToPDF(iDoc, iNotDel) {
let lNewDoc = new Document({ subject: iDoc.subject, template: iDoc.template, task: 'RTF_to_PDF'});
lNewDoc.protocol = lNewDoc.protocol.concat(iDoc.protocol)
lNewDoc.protocol.push(`Start 'convertRtfToPDF' with subject '${iDoc.subject}'`)
try {
if (!iDoc.body) { throw Error(`Document has an RTF body.`) }
if (iDoc.type != 'RTF') { throw Error(`Document is not of type RTF`) }
let { data: lData, rtf_filename: lRTFFilename, pdf_filename: lPDFFilename, error: err } = await rtf2pdf.convertRtfToPDFByData(iDoc.body, iNotDel)
if (err) { throw err }
iDoc.filename = lRTFFilename
const lBuff = Buffer.from(lData,'binary')
lNewDoc.base64 = lBuff.toString('base64');
lNewDoc.filename = lPDFFilename;
lNewDoc.type = 'PDF';
lNewDoc.checkIn(iNotDel);
} catch (err) {
console.log(err)
lNewDoc.protocol.push(`Error: ${err.message}`)
}
return lNewDoc;
},
/**
* Test routine generates a PDF from test/Brief1.rtf
* and test data a PDF
* and a text body from a test PDF file.
* @async
*/
async test() {
//Checking the installation of gs, tesseract and openoffice
let lInstData = await rtf2pdf.checkInstallation(true, "de-DE")
console.log(`\nStart 'checkInstallation'`)
for (element in lInstData.data) {
console.log(`** ${element}: ${lInstData.data[element]}`)
}
//Regsitrate new RTF template
let lDoc1 = rtf2pdf.newTemplateByFile('test', `${__dirname}/test/Brief1.rtf`);
if (lDoc1 && lDoc1.filename) {
console.log(`\nRTF Template file ${lDoc1.filename} found.`)
} else {
if (lDoc1) {
console.log(lDoc1.protocol)
} else {
console.log(`RTF Template document not found.`)
}
return;
}
//Create a correspondence as a new document for an RTF template
let lDoc2 = await rtf2pdf.createDocAsCorrespondence('de-DE', 'test',
{
"name": {
"vorname": "Hans-Joachim",
"nachname": "Lüdenscheid",
"strasse": "An der großen Brücke",
"hnr": "192a",
"telnr": "+49 151 99886677",
"email": "hjo.luedie@mac.com"
},
"to": {
"titel": "Herr",
"name1": "Hans Müller",
"strasse": "Unter den Eichen",
"hnr": "142",
"plz": "14163",
"ort": "Berlin",
"anrede": "Sehr geehrter Herr Müller"
},
"leistung": [
{ "nr": 100, "bezeichnung": "Haselnüsse", "preis": 4.50 },
{ "nr": 101, "bezeichnung": "Sonderzeichen €¢¥£µ©®§", "preis": 6.75 }
],
"auftrag": {
"datum": new Date()
}
}
)
//Generate a PDF document from RTF document
if (lDoc2 && lDoc2.body) {
console.log(`\nRTF document subject ${lDoc2.subject} created.\n`)
lDoc2.protocol.forEach((text) => {
console.log('** ' + text)
})
let lDoc3 = await rtf2pdf.convertRtfToPDF(lDoc2, true)
if (lDoc3 && lDoc3.filename) {
console.log(`\nPDF document ${lDoc3.filename} created.\n`)
lDoc2.checkIn() //<-- delete temp file
lDoc3.checkIn() //<-- delete temp file
} else {
if (lDoc3) {