hdoc-tools 0.11.6 → 0.11.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hdoc-build-db.js +152 -152
- package/hdoc-build-pdf.js +172 -172
- package/hdoc-build.js +9 -1
- package/hdoc-bump.js +108 -108
- package/hdoc-create.js +90 -90
- package/hdoc-db.js +94 -94
- package/hdoc-help.js +49 -49
- package/hdoc-module.js +390 -390
- package/hdoc-validate.js +548 -548
- package/hdoc-ver.js +42 -42
- package/package.json +1 -1
- package/templates/doc-header-non-git.html +22 -22
- package/templates/doc-header.html +29 -29
- package/templates/pdf/css/custom-block.css +90 -90
- package/templates/pdf/css/fonts.css +221 -221
- package/templates/pdf/css/hdocs-pdf.css +246 -246
- package/templates/pdf/css/vars.css +393 -393
- package/templates/pdf/template-footer.html +19 -19
- package/templates/pdf/template-header.html +37 -37
- package/templates/pdf/template.html +20 -20
- package/templates/pdf-header-non-git.html +12 -12
- package/templates/pdf-header.html +16 -16
- package/validateNodeVer.js +12 -12
- package/LICENSE +0 -21
- package/templates/init/.npmignore +0 -7
package/hdoc-build-db.js
CHANGED
@@ -1,153 +1,153 @@
|
|
1
|
-
(function () {
|
2
|
-
'use strict';
|
3
|
-
|
4
|
-
const path = require('path'),
|
5
|
-
hdoc_index = require(path.join(__dirname, 'hdoc-db.js')),
|
6
|
-
database = require('better-sqlite3');
|
7
|
-
|
8
|
-
const db_schema = {
|
9
|
-
hdoc_index: [
|
10
|
-
'resource_url UNINDEXED',
|
11
|
-
'book_id',
|
12
|
-
'book_audience UNINDEXED',
|
13
|
-
'book_tags',
|
14
|
-
'doc_title',
|
15
|
-
'doc_content',
|
16
|
-
'doc_preview UNINDEXED',
|
17
|
-
'doc_family_id',
|
18
|
-
'doc_md5_hash UNINDEXED',
|
19
|
-
'doc_lastmod'
|
20
|
-
],
|
21
|
-
hdoc_meta: [
|
22
|
-
'resource_url',
|
23
|
-
'book_id',
|
24
|
-
'contributor_count INTEGER UNINDEXED',
|
25
|
-
'edit_url UNINDEXED',
|
26
|
-
'last_commit UNINDEXED',
|
27
|
-
'pdf_size INTEGER UNINDEXED'
|
28
|
-
],
|
29
|
-
hdoc_contributors: [
|
30
|
-
'resource_url',
|
31
|
-
'book_id',
|
32
|
-
'login',
|
33
|
-
'name',
|
34
|
-
'avatar UNINDEXED',
|
35
|
-
'url UNINDEXED'
|
36
|
-
]
|
37
|
-
};
|
38
|
-
|
39
|
-
exports.create_table = function (db, table, cols, virtual, fts5) {
|
40
|
-
const table_created = hdoc_index.create_table(db, table, cols, virtual, fts5);
|
41
|
-
if (table_created !== null) {
|
42
|
-
return `\nError creating table: ${table_created}`;
|
43
|
-
} else {
|
44
|
-
console.log(`\nTable created: ${table}`);
|
45
|
-
}
|
46
|
-
return null;
|
47
|
-
};
|
48
|
-
|
49
|
-
exports.create_db = function (db_path, doc_id) {
|
50
|
-
let response = {
|
51
|
-
error: null,
|
52
|
-
db: null
|
53
|
-
};
|
54
|
-
console.log('\nPerforming SQlite index creation...');
|
55
|
-
let db_name = path.join(db_path, doc_id, 'index.db');
|
56
|
-
response.db = new database(db_name);
|
57
|
-
response.db.pragma('encoding="UTF-8"');
|
58
|
-
console.log(`\nDatabase created: ${db_name}`);
|
59
|
-
|
60
|
-
// Create tables
|
61
|
-
for (const key in db_schema) {
|
62
|
-
if (db_schema.hasOwnProperty(key)) {
|
63
|
-
const virtual = key === 'hdoc_index' ? true : false;
|
64
|
-
const fts5 = key === 'hdoc_index' ? true : false;
|
65
|
-
const table_created_error = this.create_table(response.db, key, db_schema[key], virtual, fts5);
|
66
|
-
if (table_created_error !== null) {
|
67
|
-
console.log(table_created_error);
|
68
|
-
}
|
69
|
-
}
|
70
|
-
}
|
71
|
-
return response;
|
72
|
-
};
|
73
|
-
|
74
|
-
exports.populate_index = async function (db, doc_id, book_config, index_records, verbose = false) {
|
75
|
-
let response = {
|
76
|
-
success: false,
|
77
|
-
index_success_count: 0,
|
78
|
-
error: ''
|
79
|
-
};
|
80
|
-
|
81
|
-
if (!book_config.tags) book_config.tags = [];
|
82
|
-
|
83
|
-
let indexPromises = [];
|
84
|
-
for (let i = 0; i < index_records.length; i++) {
|
85
|
-
indexPromises.push(index_records[i]);
|
86
|
-
}
|
87
|
-
await Promise.all(indexPromises.map(async (file) => {
|
88
|
-
let index_path_name = file.relative_path.replace('\\', '/');
|
89
|
-
index_path_name = '/' + index_path_name.replace(path.extname(file.relative_path), '');
|
90
|
-
const index_vals = [
|
91
|
-
index_path_name,
|
92
|
-
doc_id,
|
93
|
-
book_config.audience.join(','),
|
94
|
-
book_config.tags.join(','),
|
95
|
-
file.index_html.fm_props.title,
|
96
|
-
file.index_html.text,
|
97
|
-
file.index_html.preview,
|
98
|
-
book_config.productFamily,
|
99
|
-
file.md5,
|
100
|
-
file.lastmod
|
101
|
-
];
|
102
|
-
const index_response = hdoc_index.insert_record(db, 'hdoc_index', db_schema.hdoc_index, index_vals);
|
103
|
-
if (!index_response.success) {
|
104
|
-
console.log(`Index record creation failed - ${doc_id}/${file.index_html.fm_props.title}: ${index_response.error}`);
|
105
|
-
} else {
|
106
|
-
// Now add metadata
|
107
|
-
const meta_vals = [
|
108
|
-
index_path_name,
|
109
|
-
doc_id,
|
110
|
-
file.metadata.contributor_count,
|
111
|
-
file.metadata.edit_url,
|
112
|
-
file.metadata.last_commit,
|
113
|
-
file.pdf_size
|
114
|
-
];
|
115
|
-
const meta_response = await hdoc_index.insert_record(db, 'hdoc_meta', db_schema.hdoc_meta, meta_vals);
|
116
|
-
if (!meta_response.success) {
|
117
|
-
console.log(`Index metadata record creation failed - ${doc_id}/${index_response.row_id}/${file.index_html.fm_props.title}: ${meta_response.error}`);
|
118
|
-
} else {
|
119
|
-
if (verbose) {
|
120
|
-
console.log(`Inserted index record ${index_response.row_id}: ${doc_id} - ${file.index_html.fm_props.title}`);
|
121
|
-
console.log(`Inserted index metadata record for index ID: ${meta_response.row_id}`);
|
122
|
-
}
|
123
|
-
|
124
|
-
// Now add contributor records
|
125
|
-
for (let j = 0; j < file.contributors.length; j++) {
|
126
|
-
const contrib_vals = [
|
127
|
-
index_path_name,
|
128
|
-
doc_id,
|
129
|
-
file.contributors[j].login,
|
130
|
-
file.contributors[j].name,
|
131
|
-
file.contributors[j].avatar_url,
|
132
|
-
file.contributors[j].html_url
|
133
|
-
];
|
134
|
-
const cont_response = await hdoc_index.insert_record(db, 'hdoc_contributors', db_schema.hdoc_contributors, contrib_vals);
|
135
|
-
if (!cont_response.success) {
|
136
|
-
console.log(`Index document contributor record creation failed - ${doc_id}/${index_response.row_id}/${file.index_html.fm_props.title}: ${cont_response.error}`);
|
137
|
-
continue;
|
138
|
-
}
|
139
|
-
if (verbose) {
|
140
|
-
console.log(`Inserted document contributor recordL ${cont_response.row_id}`);
|
141
|
-
}
|
142
|
-
}
|
143
|
-
response.index_success_count++;
|
144
|
-
}
|
145
|
-
}
|
146
|
-
|
147
|
-
}));
|
148
|
-
|
149
|
-
response.success = true;
|
150
|
-
console.log(`\nIndex Build Complete: ${response.index_success_count} document index records created.`);
|
151
|
-
return response;
|
152
|
-
};
|
1
|
+
(function () {
|
2
|
+
'use strict';
|
3
|
+
|
4
|
+
const path = require('path'),
|
5
|
+
hdoc_index = require(path.join(__dirname, 'hdoc-db.js')),
|
6
|
+
database = require('better-sqlite3');
|
7
|
+
|
8
|
+
const db_schema = {
|
9
|
+
hdoc_index: [
|
10
|
+
'resource_url UNINDEXED',
|
11
|
+
'book_id',
|
12
|
+
'book_audience UNINDEXED',
|
13
|
+
'book_tags',
|
14
|
+
'doc_title',
|
15
|
+
'doc_content',
|
16
|
+
'doc_preview UNINDEXED',
|
17
|
+
'doc_family_id',
|
18
|
+
'doc_md5_hash UNINDEXED',
|
19
|
+
'doc_lastmod'
|
20
|
+
],
|
21
|
+
hdoc_meta: [
|
22
|
+
'resource_url',
|
23
|
+
'book_id',
|
24
|
+
'contributor_count INTEGER UNINDEXED',
|
25
|
+
'edit_url UNINDEXED',
|
26
|
+
'last_commit UNINDEXED',
|
27
|
+
'pdf_size INTEGER UNINDEXED'
|
28
|
+
],
|
29
|
+
hdoc_contributors: [
|
30
|
+
'resource_url',
|
31
|
+
'book_id',
|
32
|
+
'login',
|
33
|
+
'name',
|
34
|
+
'avatar UNINDEXED',
|
35
|
+
'url UNINDEXED'
|
36
|
+
]
|
37
|
+
};
|
38
|
+
|
39
|
+
exports.create_table = function (db, table, cols, virtual, fts5) {
|
40
|
+
const table_created = hdoc_index.create_table(db, table, cols, virtual, fts5);
|
41
|
+
if (table_created !== null) {
|
42
|
+
return `\nError creating table: ${table_created}`;
|
43
|
+
} else {
|
44
|
+
console.log(`\nTable created: ${table}`);
|
45
|
+
}
|
46
|
+
return null;
|
47
|
+
};
|
48
|
+
|
49
|
+
exports.create_db = function (db_path, doc_id) {
|
50
|
+
let response = {
|
51
|
+
error: null,
|
52
|
+
db: null
|
53
|
+
};
|
54
|
+
console.log('\nPerforming SQlite index creation...');
|
55
|
+
let db_name = path.join(db_path, doc_id, 'index.db');
|
56
|
+
response.db = new database(db_name);
|
57
|
+
response.db.pragma('encoding="UTF-8"');
|
58
|
+
console.log(`\nDatabase created: ${db_name}`);
|
59
|
+
|
60
|
+
// Create tables
|
61
|
+
for (const key in db_schema) {
|
62
|
+
if (db_schema.hasOwnProperty(key)) {
|
63
|
+
const virtual = key === 'hdoc_index' ? true : false;
|
64
|
+
const fts5 = key === 'hdoc_index' ? true : false;
|
65
|
+
const table_created_error = this.create_table(response.db, key, db_schema[key], virtual, fts5);
|
66
|
+
if (table_created_error !== null) {
|
67
|
+
console.log(table_created_error);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
return response;
|
72
|
+
};
|
73
|
+
|
74
|
+
exports.populate_index = async function (db, doc_id, book_config, index_records, verbose = false) {
|
75
|
+
let response = {
|
76
|
+
success: false,
|
77
|
+
index_success_count: 0,
|
78
|
+
error: ''
|
79
|
+
};
|
80
|
+
|
81
|
+
if (!book_config.tags) book_config.tags = [];
|
82
|
+
|
83
|
+
let indexPromises = [];
|
84
|
+
for (let i = 0; i < index_records.length; i++) {
|
85
|
+
indexPromises.push(index_records[i]);
|
86
|
+
}
|
87
|
+
await Promise.all(indexPromises.map(async (file) => {
|
88
|
+
let index_path_name = file.relative_path.replace('\\', '/');
|
89
|
+
index_path_name = '/' + index_path_name.replace(path.extname(file.relative_path), '');
|
90
|
+
const index_vals = [
|
91
|
+
index_path_name,
|
92
|
+
doc_id,
|
93
|
+
book_config.audience.join(','),
|
94
|
+
book_config.tags.join(','),
|
95
|
+
file.index_html.fm_props.title,
|
96
|
+
file.index_html.text,
|
97
|
+
file.index_html.preview,
|
98
|
+
book_config.productFamily,
|
99
|
+
file.md5,
|
100
|
+
file.lastmod
|
101
|
+
];
|
102
|
+
const index_response = hdoc_index.insert_record(db, 'hdoc_index', db_schema.hdoc_index, index_vals);
|
103
|
+
if (!index_response.success) {
|
104
|
+
console.log(`Index record creation failed - ${doc_id}/${file.index_html.fm_props.title}: ${index_response.error}`);
|
105
|
+
} else {
|
106
|
+
// Now add metadata
|
107
|
+
const meta_vals = [
|
108
|
+
index_path_name,
|
109
|
+
doc_id,
|
110
|
+
file.metadata.contributor_count,
|
111
|
+
file.metadata.edit_url,
|
112
|
+
file.metadata.last_commit,
|
113
|
+
file.pdf_size
|
114
|
+
];
|
115
|
+
const meta_response = await hdoc_index.insert_record(db, 'hdoc_meta', db_schema.hdoc_meta, meta_vals);
|
116
|
+
if (!meta_response.success) {
|
117
|
+
console.log(`Index metadata record creation failed - ${doc_id}/${index_response.row_id}/${file.index_html.fm_props.title}: ${meta_response.error}`);
|
118
|
+
} else {
|
119
|
+
if (verbose) {
|
120
|
+
console.log(`Inserted index record ${index_response.row_id}: ${doc_id} - ${file.index_html.fm_props.title}`);
|
121
|
+
console.log(`Inserted index metadata record for index ID: ${meta_response.row_id}`);
|
122
|
+
}
|
123
|
+
|
124
|
+
// Now add contributor records
|
125
|
+
for (let j = 0; j < file.contributors.length; j++) {
|
126
|
+
const contrib_vals = [
|
127
|
+
index_path_name,
|
128
|
+
doc_id,
|
129
|
+
file.contributors[j].login,
|
130
|
+
file.contributors[j].name,
|
131
|
+
file.contributors[j].avatar_url,
|
132
|
+
file.contributors[j].html_url
|
133
|
+
];
|
134
|
+
const cont_response = await hdoc_index.insert_record(db, 'hdoc_contributors', db_schema.hdoc_contributors, contrib_vals);
|
135
|
+
if (!cont_response.success) {
|
136
|
+
console.log(`Index document contributor record creation failed - ${doc_id}/${index_response.row_id}/${file.index_html.fm_props.title}: ${cont_response.error}`);
|
137
|
+
continue;
|
138
|
+
}
|
139
|
+
if (verbose) {
|
140
|
+
console.log(`Inserted document contributor recordL ${cont_response.row_id}`);
|
141
|
+
}
|
142
|
+
}
|
143
|
+
response.index_success_count++;
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
}));
|
148
|
+
|
149
|
+
response.success = true;
|
150
|
+
console.log(`\nIndex Build Complete: ${response.index_success_count} document index records created.`);
|
151
|
+
return response;
|
152
|
+
};
|
153
153
|
})();
|
package/hdoc-build-pdf.js
CHANGED
@@ -1,173 +1,173 @@
|
|
1
|
-
(function () {
|
2
|
-
'use strict';
|
3
|
-
|
4
|
-
const axios = require('axios'),
|
5
|
-
cheerio = require('cheerio'),
|
6
|
-
fs = require('fs-extra'),
|
7
|
-
mime = require('mime-types'),
|
8
|
-
path = require('path'),
|
9
|
-
hdoc = require(path.join(__dirname, 'hdoc-module.js'));
|
10
|
-
|
11
|
-
let hb_logo = '',
|
12
|
-
footer = '',
|
13
|
-
header = '';
|
14
|
-
|
15
|
-
const get_footer = function (template_path) {
|
16
|
-
let footer_content = null;
|
17
|
-
try {
|
18
|
-
footer_content = fs.readFileSync(path.join(template_path, 'template-footer.html'), 'utf8');
|
19
|
-
} catch (err) {
|
20
|
-
console.log(`Error loading template: ${err}`);
|
21
|
-
}
|
22
|
-
return footer_content;
|
23
|
-
};
|
24
|
-
|
25
|
-
const get_header = function (template_path) {
|
26
|
-
let header_content = null;
|
27
|
-
try {
|
28
|
-
header_content = fs.readFileSync(path.join(template_path, 'template-header.html'), 'utf8');
|
29
|
-
} catch (err) {
|
30
|
-
console.log(`Error loading template: ${err}`);
|
31
|
-
}
|
32
|
-
return header_content;
|
33
|
-
};
|
34
|
-
|
35
|
-
exports.process_images = async function (file_path, html_source, verbose) {
|
36
|
-
const book_work_root = file_path.path.replace(file_path.relativePath, '');
|
37
|
-
if (verbose) console.log('Parsing img tags from HTML source');
|
38
|
-
|
39
|
-
// Use cheerio to parse html
|
40
|
-
const $ = cheerio.load(html_source);
|
41
|
-
|
42
|
-
// Get iFrames from HTML, to replace with a tags
|
43
|
-
let iframes = [];
|
44
|
-
const iframe_html = $('iframe').map(function () {
|
45
|
-
const response = {
|
46
|
-
html: $.html(this),
|
47
|
-
src: $(this).attr('src'),
|
48
|
-
title: $(this).attr('title') ? $(this).attr('title') : 'No Link Title Provided'
|
49
|
-
};
|
50
|
-
return response;
|
51
|
-
}).get();
|
52
|
-
iframes.push(...iframe_html);
|
53
|
-
for (let i = 0; i < iframes.length; i++) {
|
54
|
-
const link = `<p><a href="${iframes[i].src}">${iframes[i].title}</a></p>`;
|
55
|
-
const regex = new RegExp(`<iframe.*src="${iframes[i].src.replace('/', '\\/')}".*</iframe>`);
|
56
|
-
html_source = html_source.replace(regex, link);
|
57
|
-
}
|
58
|
-
|
59
|
-
// Get image links from HTML, to embed into the pdf
|
60
|
-
let imgs = [];
|
61
|
-
const srcs = $('img').map(function (i) {
|
62
|
-
return $(this).attr('src');
|
63
|
-
}).get();
|
64
|
-
imgs.push(...srcs);
|
65
|
-
for (let i = 0; i < imgs.length; i++) {
|
66
|
-
if (!hdoc.valid_url(imgs[i])) {
|
67
|
-
// Internal link
|
68
|
-
const image_path = path.join(book_work_root, imgs[i].replace('_books/', ''));
|
69
|
-
try {
|
70
|
-
const image_buffer = fs.readFileSync(image_path);
|
71
|
-
const mime_type = mime.lookup(image_path);
|
72
|
-
let image_b64 = image_buffer.toString("base64");
|
73
|
-
image_b64 = `data:${mime_type};base64,${image_b64}`;
|
74
|
-
html_source = html_source.replace(imgs[i], image_b64);
|
75
|
-
} catch (err) {
|
76
|
-
console.log('Error reading image from HTML source [', image_path, '] -', err);
|
77
|
-
return null;
|
78
|
-
}
|
79
|
-
} else {
|
80
|
-
// External Link
|
81
|
-
try {
|
82
|
-
const file_response = await axios.get(imgs[i]);
|
83
|
-
if (file_response.status === 200) {
|
84
|
-
const image_buffer = file_response.data;
|
85
|
-
const mime_type = mime.lookup(imgs[i]);
|
86
|
-
let image_b64 = image_buffer.toString("base64");
|
87
|
-
image_b64 = `data:${mime_type};base64,${image_b64}`;
|
88
|
-
html_source = html_source.replace(imgs[i], image_b64);
|
89
|
-
} else {
|
90
|
-
throw `Unexpected Status ${file_response.status}`;
|
91
|
-
}
|
92
|
-
} catch (err) {
|
93
|
-
console.log(`Error downloading external source [${imgs[i]}] - ${err}`);
|
94
|
-
}
|
95
|
-
}
|
96
|
-
}
|
97
|
-
|
98
|
-
return html_source;
|
99
|
-
};
|
100
|
-
|
101
|
-
exports.generate_pdf = async function (browser, pdf_template_path, pdf_template_content, book_config, html_source, target_file, css_templates, verbose = false) {
|
102
|
-
let pdf_size = 0;
|
103
|
-
|
104
|
-
// Cache footer
|
105
|
-
if (footer === '') footer = get_footer(pdf_template_path);
|
106
|
-
|
107
|
-
// Read svg logo file into buffer, convert to B64 string
|
108
|
-
if (hb_logo === '') {
|
109
|
-
const hb_logo_path = path.join(pdf_template_path, 'images', 'hornbill-logo-full.svg');
|
110
|
-
try {
|
111
|
-
const hb_logo_file_buffer = fs.readFileSync(hb_logo_path);
|
112
|
-
hb_logo = hb_logo_file_buffer.toString("base64");
|
113
|
-
hb_logo = `data:image/svg+xml;base64,${hb_logo}`;
|
114
|
-
} catch (err) {
|
115
|
-
console.log('Error reading logo from template:', err);
|
116
|
-
return pdf_size;
|
117
|
-
}
|
118
|
-
}
|
119
|
-
|
120
|
-
// Cache header
|
121
|
-
if (header === '') {
|
122
|
-
header = get_header(pdf_template_path).replace('{{book_title}}', book_config.title).replace('{{hb_logo}}', hb_logo);
|
123
|
-
}
|
124
|
-
|
125
|
-
html_source = pdf_template_content.replace('{{book_title}}', book_config.title).replace('{{document_content}}', html_source);
|
126
|
-
|
127
|
-
const page = await browser.newPage();
|
128
|
-
|
129
|
-
// To reflect CSS used for screens instead of print
|
130
|
-
await page.emulateMediaType('screen');
|
131
|
-
|
132
|
-
// Set HTML content from HTML source
|
133
|
-
await page.setContent(html_source, {
|
134
|
-
waitUntil: 'domcontentloaded'
|
135
|
-
});
|
136
|
-
for (let i = 0; i < css_templates.length; i++) {
|
137
|
-
try {
|
138
|
-
await page.addStyleTag({
|
139
|
-
content: css_templates[i]
|
140
|
-
});
|
141
|
-
} catch (e) {
|
142
|
-
console.log(`Error applying template for [${target_file}]: ${e}`);
|
143
|
-
}
|
144
|
-
}
|
145
|
-
|
146
|
-
try {
|
147
|
-
const pdf_gen = await page.pdf({
|
148
|
-
path: target_file,
|
149
|
-
printBackground: true,
|
150
|
-
format: 'A4',
|
151
|
-
displayHeaderFooter: true,
|
152
|
-
headerTemplate: header,
|
153
|
-
footerTemplate: footer,
|
154
|
-
margin: {
|
155
|
-
top: "90px",
|
156
|
-
right: "30px",
|
157
|
-
bottom: "60px",
|
158
|
-
left: "30px"
|
159
|
-
},
|
160
|
-
timeout: 0
|
161
|
-
});
|
162
|
-
let currdate = new Date;
|
163
|
-
let datetime = currdate.toISOString();
|
164
|
-
if (verbose) console.log(`[${datetime}] PDF generation success: ${target_file}`);
|
165
|
-
|
166
|
-
pdf_size = pdf_gen.byteLength;
|
167
|
-
} catch (err) {
|
168
|
-
console.log(`Error generating PDF ${target_file} - ${err}`);
|
169
|
-
}
|
170
|
-
await page.close();
|
171
|
-
return pdf_size;
|
172
|
-
};
|
1
|
+
(function () {
|
2
|
+
'use strict';
|
3
|
+
|
4
|
+
const axios = require('axios'),
|
5
|
+
cheerio = require('cheerio'),
|
6
|
+
fs = require('fs-extra'),
|
7
|
+
mime = require('mime-types'),
|
8
|
+
path = require('path'),
|
9
|
+
hdoc = require(path.join(__dirname, 'hdoc-module.js'));
|
10
|
+
|
11
|
+
let hb_logo = '',
|
12
|
+
footer = '',
|
13
|
+
header = '';
|
14
|
+
|
15
|
+
const get_footer = function (template_path) {
|
16
|
+
let footer_content = null;
|
17
|
+
try {
|
18
|
+
footer_content = fs.readFileSync(path.join(template_path, 'template-footer.html'), 'utf8');
|
19
|
+
} catch (err) {
|
20
|
+
console.log(`Error loading template: ${err}`);
|
21
|
+
}
|
22
|
+
return footer_content;
|
23
|
+
};
|
24
|
+
|
25
|
+
const get_header = function (template_path) {
|
26
|
+
let header_content = null;
|
27
|
+
try {
|
28
|
+
header_content = fs.readFileSync(path.join(template_path, 'template-header.html'), 'utf8');
|
29
|
+
} catch (err) {
|
30
|
+
console.log(`Error loading template: ${err}`);
|
31
|
+
}
|
32
|
+
return header_content;
|
33
|
+
};
|
34
|
+
|
35
|
+
exports.process_images = async function (file_path, html_source, verbose) {
|
36
|
+
const book_work_root = file_path.path.replace(file_path.relativePath, '');
|
37
|
+
if (verbose) console.log('Parsing img tags from HTML source');
|
38
|
+
|
39
|
+
// Use cheerio to parse html
|
40
|
+
const $ = cheerio.load(html_source);
|
41
|
+
|
42
|
+
// Get iFrames from HTML, to replace with a tags
|
43
|
+
let iframes = [];
|
44
|
+
const iframe_html = $('iframe').map(function () {
|
45
|
+
const response = {
|
46
|
+
html: $.html(this),
|
47
|
+
src: $(this).attr('src'),
|
48
|
+
title: $(this).attr('title') ? $(this).attr('title') : 'No Link Title Provided'
|
49
|
+
};
|
50
|
+
return response;
|
51
|
+
}).get();
|
52
|
+
iframes.push(...iframe_html);
|
53
|
+
for (let i = 0; i < iframes.length; i++) {
|
54
|
+
const link = `<p><a href="${iframes[i].src}">${iframes[i].title}</a></p>`;
|
55
|
+
const regex = new RegExp(`<iframe.*src="${iframes[i].src.replace('/', '\\/')}".*</iframe>`);
|
56
|
+
html_source = html_source.replace(regex, link);
|
57
|
+
}
|
58
|
+
|
59
|
+
// Get image links from HTML, to embed into the pdf
|
60
|
+
let imgs = [];
|
61
|
+
const srcs = $('img').map(function (i) {
|
62
|
+
return $(this).attr('src');
|
63
|
+
}).get();
|
64
|
+
imgs.push(...srcs);
|
65
|
+
for (let i = 0; i < imgs.length; i++) {
|
66
|
+
if (!hdoc.valid_url(imgs[i])) {
|
67
|
+
// Internal link
|
68
|
+
const image_path = path.join(book_work_root, imgs[i].replace('_books/', ''));
|
69
|
+
try {
|
70
|
+
const image_buffer = fs.readFileSync(image_path);
|
71
|
+
const mime_type = mime.lookup(image_path);
|
72
|
+
let image_b64 = image_buffer.toString("base64");
|
73
|
+
image_b64 = `data:${mime_type};base64,${image_b64}`;
|
74
|
+
html_source = html_source.replace(imgs[i], image_b64);
|
75
|
+
} catch (err) {
|
76
|
+
console.log('Error reading image from HTML source [', image_path, '] -', err);
|
77
|
+
return null;
|
78
|
+
}
|
79
|
+
} else {
|
80
|
+
// External Link
|
81
|
+
try {
|
82
|
+
const file_response = await axios.get(imgs[i]);
|
83
|
+
if (file_response.status === 200) {
|
84
|
+
const image_buffer = file_response.data;
|
85
|
+
const mime_type = mime.lookup(imgs[i]);
|
86
|
+
let image_b64 = image_buffer.toString("base64");
|
87
|
+
image_b64 = `data:${mime_type};base64,${image_b64}`;
|
88
|
+
html_source = html_source.replace(imgs[i], image_b64);
|
89
|
+
} else {
|
90
|
+
throw `Unexpected Status ${file_response.status}`;
|
91
|
+
}
|
92
|
+
} catch (err) {
|
93
|
+
console.log(`Error downloading external source [${imgs[i]}] - ${err}`);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
return html_source;
|
99
|
+
};
|
100
|
+
|
101
|
+
exports.generate_pdf = async function (browser, pdf_template_path, pdf_template_content, book_config, html_source, target_file, css_templates, verbose = false) {
|
102
|
+
let pdf_size = 0;
|
103
|
+
|
104
|
+
// Cache footer
|
105
|
+
if (footer === '') footer = get_footer(pdf_template_path);
|
106
|
+
|
107
|
+
// Read svg logo file into buffer, convert to B64 string
|
108
|
+
if (hb_logo === '') {
|
109
|
+
const hb_logo_path = path.join(pdf_template_path, 'images', 'hornbill-logo-full.svg');
|
110
|
+
try {
|
111
|
+
const hb_logo_file_buffer = fs.readFileSync(hb_logo_path);
|
112
|
+
hb_logo = hb_logo_file_buffer.toString("base64");
|
113
|
+
hb_logo = `data:image/svg+xml;base64,${hb_logo}`;
|
114
|
+
} catch (err) {
|
115
|
+
console.log('Error reading logo from template:', err);
|
116
|
+
return pdf_size;
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
// Cache header
|
121
|
+
if (header === '') {
|
122
|
+
header = get_header(pdf_template_path).replace('{{book_title}}', book_config.title).replace('{{hb_logo}}', hb_logo);
|
123
|
+
}
|
124
|
+
|
125
|
+
html_source = pdf_template_content.replace('{{book_title}}', book_config.title).replace('{{document_content}}', html_source);
|
126
|
+
|
127
|
+
const page = await browser.newPage();
|
128
|
+
|
129
|
+
// To reflect CSS used for screens instead of print
|
130
|
+
await page.emulateMediaType('screen');
|
131
|
+
|
132
|
+
// Set HTML content from HTML source
|
133
|
+
await page.setContent(html_source, {
|
134
|
+
waitUntil: 'domcontentloaded'
|
135
|
+
});
|
136
|
+
for (let i = 0; i < css_templates.length; i++) {
|
137
|
+
try {
|
138
|
+
await page.addStyleTag({
|
139
|
+
content: css_templates[i]
|
140
|
+
});
|
141
|
+
} catch (e) {
|
142
|
+
console.log(`Error applying template for [${target_file}]: ${e}`);
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
try {
|
147
|
+
const pdf_gen = await page.pdf({
|
148
|
+
path: target_file,
|
149
|
+
printBackground: true,
|
150
|
+
format: 'A4',
|
151
|
+
displayHeaderFooter: true,
|
152
|
+
headerTemplate: header,
|
153
|
+
footerTemplate: footer,
|
154
|
+
margin: {
|
155
|
+
top: "90px",
|
156
|
+
right: "30px",
|
157
|
+
bottom: "60px",
|
158
|
+
left: "30px"
|
159
|
+
},
|
160
|
+
timeout: 0
|
161
|
+
});
|
162
|
+
let currdate = new Date;
|
163
|
+
let datetime = currdate.toISOString();
|
164
|
+
if (verbose) console.log(`[${datetime}] PDF generation success: ${target_file}`);
|
165
|
+
|
166
|
+
pdf_size = pdf_gen.byteLength;
|
167
|
+
} catch (err) {
|
168
|
+
console.log(`Error generating PDF ${target_file} - ${err}`);
|
169
|
+
}
|
170
|
+
await page.close();
|
171
|
+
return pdf_size;
|
172
|
+
};
|
173
173
|
})();
|