-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
converter.js
144 lines (132 loc) · 4.41 KB
/
converter.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
/* global Opal */
const { PDFDocument } = require('pdf-lib')
const util = require('util')
const fsExtra = require('fs-extra')
const path = require('path')
const mkdirs = util.promisify(fsExtra.mkdirs)
const Browser = require('./browser.js')
const { addOutline } = require('./outline.js')
const { addMetadata } = require('./metadata')
const browserInstance = new Browser()
function registerTemplateConverter (processor, templates) {
class TemplateConverter {
constructor () {
this.baseConverter = processor.Html5Converter.create()
this.templates = templates
}
convert (node, transform, opts) {
const template = this.templates[transform || node.node_name]
if (template) {
return template(node, this.baseConverter)
}
return this.baseConverter.convert(node, transform, opts)
}
}
processor.ConverterFactory.register(new TemplateConverter(), ['html5'])
}
async function convert (processor, inputFile, options, timings, watch, preview) {
const tempFile = getTemporaryHtmlFile(inputFile.path, options)
let workingDir
if (options.to_dir) {
await mkdirs(options.to_dir)
workingDir = options.to_dir
} else {
workingDir = path.dirname(inputFile.path)
}
const inputFilenameWithoutExt = path.basename(inputFile.path, path.extname(inputFile.path))
let outputFile = path.join(workingDir, inputFilenameWithoutExt + '.pdf')
let outputToStdout = false
if (options.to_file) {
if (options.to_file !== Opal.gvars.stdout) {
await mkdirs(path.dirname(options.to_file))
if (options.to_dir) {
outputFile = path.join(options.to_dir, options.to_file)
} else {
outputFile = options.to_file
}
} else {
outputToStdout = true
}
}
const instanceOptions = Object.assign({}, options, { to_file: tempFile })
let doc
if (timings) {
const timings = processor.Timings.$new()
instanceOptions.timings = timings
}
if ( inputFile.contents ) {
// data from stdin
doc = processor.convert(inputFile.contents, instanceOptions)
} else {
doc = processor.convertFile(inputFile.path, instanceOptions)
}
if (timings) {
timings.$print_report(Opal.gvars.stderr, inputFile.contents ? '-' : inputFile.path)
}
const puppeteerConfig = {
headless: !preview,
args: ['--no-sandbox', '--allow-file-access-from-files']
}
if (preview) {
Object.assign(puppeteerConfig, { defaultViewport: null })
}
try {
const page = await browserInstance.goto(`file://${tempFile}`, preview)
if (!preview) {
const pdfOptions = {
printBackground: true,
preferCSSPageSize: true
}
const pdfWidth = doc.getAttributes()['pdf-width']
if (pdfWidth) {
pdfOptions.width = pdfWidth
}
const pdfHeight = doc.getAttributes()['pdf-height']
if (pdfHeight) {
pdfOptions.height = pdfHeight
}
const format = doc.getAttributes()['pdf-format']
if (format) {
pdfOptions.format = format // Paper format. If set, takes priority over width or height options. Defaults to 'Letter'.
}
let pdf = await page.pdf(pdfOptions)
// Outline is not yet implemented in Chromium, so we add it manually here.
// https://bugs.chromium.org/p/chromium/issues/detail?id=840455
let pdfDoc = await PDFDocument.load(pdf)
pdfDoc = await addOutline(pdfDoc, doc)
pdfDoc = await addMetadata(pdfDoc, doc)
pdf = await pdfDoc.save()
if (outputToStdout) {
console.log(pdf.toString())
} else {
fsExtra.writeFileSync(outputFile, pdf)
}
}
} catch (err) {
console.log('Unable to generate the PDF - Error: ' + err.toString())
} finally {
if (watch || preview) {
if (!watch) {
console.log('Preview mode entered, needs to be manually terminated using Ctrl+C!')
await new Promise(resolve => {})
}
} else {
await browserInstance.close()
}
}
}
function getTemporaryHtmlFile (inputFile) {
const workingDir = path.dirname(inputFile)
const inputFilenameWithoutExt = path.basename(inputFile, path.extname(inputFile))
let tempFile
if (path.isAbsolute(workingDir)) {
tempFile = path.join(workingDir, `${inputFilenameWithoutExt}.html`)
} else {
tempFile = path.normalize(path.join(process.cwd(), workingDir, `${inputFilenameWithoutExt}.html`))
}
return tempFile
}
module.exports = {
convert: convert,
registerTemplateConverter: registerTemplateConverter
}