1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
17 |
|
18 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
19 |
|
20 | const parsePdf = require('easy-pdf-parser').parsePdf;
|
21 |
|
22 | const extractPlainText = require('easy-pdf-parser').extractPlainText;
|
23 |
|
24 | const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
|
25 |
|
26 | const PdfPrinter = require('pdfmake');
|
27 |
|
28 | const ToPdfMakeVisitor = require('./ToPdfMakeVisitor');
|
29 |
|
30 | const fonts = {
|
31 | Courier: {
|
32 | normal: 'Courier',
|
33 | bold: 'Courier-Bold',
|
34 | italics: 'Courier-Oblique',
|
35 | bolditalics: 'Courier-BoldOblique'
|
36 | },
|
37 | Helvetica: {
|
38 | normal: 'Helvetica',
|
39 | bold: 'Helvetica-Bold',
|
40 | italics: 'Helvetica-Oblique',
|
41 | bolditalics: 'Helvetica-BoldOblique'
|
42 | },
|
43 | Times: {
|
44 | normal: 'Times-Roman',
|
45 | bold: 'Times-Bold',
|
46 | italics: 'Times-Italic',
|
47 | bolditalics: 'Times-BoldItalic'
|
48 | },
|
49 | Symbol: {
|
50 | normal: 'Symbol'
|
51 | },
|
52 | ZapfDingbats: {
|
53 | normal: 'ZapfDingbats'
|
54 | },
|
55 | LiberationSerif: {
|
56 | normal: "".concat(__dirname, "/fonts/LiberationSerif-Regular.ttf"),
|
57 | bold: "".concat(__dirname, "/fonts/LiberationSerif-Bold.ttf"),
|
58 | italics: "".concat(__dirname, "/fonts/LiberationSerif-Italic.ttf"),
|
59 | bolditalics: "".concat(__dirname, "/fonts/LiberationSerif-BoldItalic.ttf")
|
60 | },
|
61 | LiberationSans: {
|
62 | normal: "".concat(__dirname, "/fonts/LiberationSans-Regular.ttf"),
|
63 | bold: "".concat(__dirname, "/fonts/LiberationSans-Bold.ttf"),
|
64 | italics: "".concat(__dirname, "/fonts/LiberationSans-Italic.ttf"),
|
65 | bolditalics: "".concat(__dirname, "/fonts/LiberationSans-BoldItalic.ttf")
|
66 | },
|
67 | LiberationMono: {
|
68 | normal: "".concat(__dirname, "/fonts/LiberationMono-Regular.ttf"),
|
69 | bold: "".concat(__dirname, "/fonts/LiberationMono-Bold.ttf"),
|
70 | italics: "".concat(__dirname, "/fonts/LiberationMono-Italic.ttf"),
|
71 | bolditalics: "".concat(__dirname, "/fonts/LiberationMono-BoldItalic.ttf")
|
72 | }
|
73 | };
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | class PdfTransformer {
|
79 | |
80 |
|
81 |
|
82 |
|
83 | constructor() {
|
84 | let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
85 | this.options = options;
|
86 | this.ciceroMarkTransformer = new CiceroMarkTransformer();
|
87 | }
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | toCiceroMark(input) {
|
98 | var _arguments = arguments,
|
99 | _this = this;
|
100 |
|
101 | return _asyncToGenerator(function* () {
|
102 | let format = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : 'concerto';
|
103 | const plainText = yield parsePdf(input).then(extractPlainText);
|
104 | return _this.ciceroMarkTransformer.fromMarkdown(plainText, format);
|
105 | })();
|
106 | }
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | toPdf(input, options, outputStream) {
|
116 | var _this2 = this;
|
117 |
|
118 | return _asyncToGenerator(function* () {
|
119 | const printer = new PdfPrinter(fonts);
|
120 |
|
121 | if (!input.getType) {
|
122 | input = _this2.ciceroMarkTransformer.getSerializer().fromJSON(input);
|
123 | }
|
124 |
|
125 | const parameters = {};
|
126 | parameters.result = '';
|
127 | parameters.first = true;
|
128 | parameters.indent = 0;
|
129 | const visitor = new ToPdfMakeVisitor(_this2.options);
|
130 | input.accept(visitor, parameters);
|
131 | const dd = parameters.result;
|
132 |
|
133 | dd.defaultStyle = {
|
134 | fontSize: 12,
|
135 | font: 'LiberationSerif',
|
136 | lineHeight: 1.5
|
137 | };
|
138 | dd.pageSize = 'LETTER';
|
139 | dd.pageOrientation = 'portrait', dd.pageMargins = [80, 80, 80, 80];
|
140 |
|
141 | Object.assign(dd, options);
|
142 |
|
143 | if (options.tocHeading) {
|
144 | dd.content = [{
|
145 | toc: {
|
146 | title: {
|
147 | text: options.tocHeading,
|
148 | style: 'toc'
|
149 | }
|
150 | }
|
151 | }].concat([{
|
152 | text: '',
|
153 | pageBreak: 'after'
|
154 | }].concat(dd.content));
|
155 | }
|
156 |
|
157 | if (options.headerText) {
|
158 | dd.header = {
|
159 | text: options.headerText,
|
160 | style: 'Header'
|
161 | };
|
162 | }
|
163 |
|
164 | if (options.footerText || options.footerPageNumber) {
|
165 | dd.footer = function (currentPage, pageCount) {
|
166 | const footer = [{
|
167 | text: options.footerText ? options.footerText : '',
|
168 | style: 'Footer'
|
169 | }];
|
170 |
|
171 | if (options.footerPageNumber) {
|
172 | footer.push({
|
173 | text: currentPage.toString() + ' / ' + pageCount,
|
174 | style: 'PageNumber'
|
175 | });
|
176 | }
|
177 |
|
178 | return footer;
|
179 | };
|
180 | }
|
181 |
|
182 | const defaultStyles = {
|
183 | Footer: {
|
184 | alignment: 'left',
|
185 | margin: [10, 10, 0, 0]
|
186 | },
|
187 | PageNumber: {
|
188 | alignment: 'center',
|
189 | margin: [0, 0, 0, 0]
|
190 | },
|
191 | Header: {
|
192 | alignment: 'right',
|
193 | margin: [0, 10, 10, 0]
|
194 | },
|
195 | heading_one: {
|
196 | fontSize: 30,
|
197 | bold: true,
|
198 | alignment: 'center'
|
199 | },
|
200 | heading_two: {
|
201 | fontSize: 28,
|
202 | bold: true
|
203 | },
|
204 | heading_three: {
|
205 | fontSize: 26,
|
206 | bold: true
|
207 | },
|
208 | heading_four: {
|
209 | fontSize: 24,
|
210 | bold: true
|
211 | },
|
212 | heading_five: {
|
213 | fontSize: 22,
|
214 | bold: true
|
215 | },
|
216 | heading_six: {
|
217 | fontSize: 20,
|
218 | bold: true
|
219 | },
|
220 | Code: {
|
221 | font: 'LiberationMono'
|
222 | },
|
223 | CodeBlock: {
|
224 | font: 'LiberationMono'
|
225 | },
|
226 | HtmlInline: {
|
227 | font: 'LiberationMono'
|
228 | },
|
229 | HtmlBlock: {
|
230 | font: 'LiberationMono'
|
231 | },
|
232 | Paragraph: {
|
233 | alignment: 'justify'
|
234 | },
|
235 | toc: {
|
236 | fontSize: 30,
|
237 | bold: true,
|
238 | alignment: 'center'
|
239 | },
|
240 | Link: {
|
241 | color: 'blue'
|
242 | },
|
243 | BlockQuote: {
|
244 | margin: [20, 0]
|
245 | }
|
246 | };
|
247 |
|
248 | dd.styles = defaultStyles;
|
249 |
|
250 | if (options.styles) {
|
251 | Object.assign(dd.styles, options.styles);
|
252 | }
|
253 |
|
254 | const pdfDoc = printer.createPdfKitDocument(dd);
|
255 | pdfDoc.pipe(outputStream);
|
256 | pdfDoc.end();
|
257 | })();
|
258 | }
|
259 |
|
260 | }
|
261 |
|
262 | module.exports = PdfTransformer; |
\ | No newline at end of file |