1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | 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); } }
|
20 |
|
21 | 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); }); }; }
|
22 |
|
23 | const fs = require('fs');
|
24 |
|
25 | const PdfTransformer = require('./PdfTransformer');
|
26 |
|
27 | const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
|
28 |
|
29 | let pdfTransformer = null;
|
30 |
|
31 | beforeAll(() => {
|
32 | pdfTransformer = new PdfTransformer();
|
33 | });
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function getPdfFiles() {
|
40 | const result = [];
|
41 | const files = fs.readdirSync(__dirname + '/../test/data');
|
42 | files.forEach(function (file) {
|
43 | if (file.endsWith('.pdf')) {
|
44 | let contents = fs.readFileSync(__dirname + '/../test/data/' + file);
|
45 | result.push([file, contents]);
|
46 | }
|
47 | });
|
48 | return result;
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function saveCiceroMarkAsPdf(ciceroMarkDom, fileName) {
|
59 | fs.mkdirSync('./output', {
|
60 | recursive: true
|
61 | });
|
62 | const promise = new Promise(resolve => {
|
63 | const outputStream = fs.createWriteStream("./output/".concat(fileName, ".pdf"));
|
64 | outputStream.on('finish', () => {
|
65 | resolve(true);
|
66 | });
|
67 | const options = {
|
68 | info: {
|
69 | title: 'Smart Legal Contract',
|
70 | author: 'Dan',
|
71 | subject: 'Test PDF rendering',
|
72 | keywords: 'accord project, markdown transform, pdf'
|
73 | },
|
74 | defaultStyle: {
|
75 | font: 'LiberationSerif'
|
76 | },
|
77 | tocHeading: 'Table of Contents',
|
78 | headerText: 'Contract ABCDEF',
|
79 | footerText: 'Copyright Acme Inc.',
|
80 | footerPageNumber: true,
|
81 | styles: {
|
82 | Link: {
|
83 | color: 'red',
|
84 | fontSize: 16
|
85 | }
|
86 | }
|
87 | };
|
88 | pdfTransformer.toPdf(ciceroMarkDom, options, outputStream);
|
89 | });
|
90 | return promise;
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function getMarkdownFiles() {
|
99 | const result = [];
|
100 | const files = fs.readdirSync(__dirname + '/../test/data');
|
101 | files.forEach(function (file) {
|
102 | if (file.endsWith('.md')) {
|
103 | let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
|
104 | result.push([file, contents]);
|
105 | }
|
106 | });
|
107 | return result;
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | function getJsonFiles() {
|
116 | const result = [];
|
117 | const files = fs.readdirSync(__dirname + '/../test/data');
|
118 | files.forEach(function (file) {
|
119 | if (file.endsWith('.json')) {
|
120 | let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
|
121 | result.push([file, contents]);
|
122 | }
|
123 | });
|
124 | return result;
|
125 | }
|
126 |
|
127 | describe('pdf import', () => {
|
128 | jest.setTimeout(20000);
|
129 | getPdfFiles().forEach((_ref, i) => {
|
130 | let [file, pdfContent] = _ref;
|
131 | it("converts ".concat(file, " to cicero mark"), _asyncToGenerator(function* () {
|
132 | const ciceroMarkDom = yield pdfTransformer.toCiceroMark(pdfContent, 'json');
|
133 |
|
134 |
|
135 |
|
136 | expect(ciceroMarkDom).toMatchSnapshot();
|
137 |
|
138 | return saveCiceroMarkAsPdf(ciceroMarkDom, file + '-import');
|
139 | }));
|
140 | });
|
141 | });
|
142 | describe('pdf generation', () => {
|
143 | getMarkdownFiles().forEach((_ref3, i) => {
|
144 | let [file, markdownContent] = _ref3;
|
145 | it("converts ".concat(file, " to pdf"), _asyncToGenerator(function* () {
|
146 | const ciceroMarkTransformer = new CiceroMarkTransformer();
|
147 | const ciceroMarkDom = ciceroMarkTransformer.fromMarkdownCicero(markdownContent, 'json');
|
148 | return saveCiceroMarkAsPdf(ciceroMarkDom, file);
|
149 | }));
|
150 | });
|
151 | getJsonFiles().forEach((_ref5, i) => {
|
152 | let [file, jsonContent] = _ref5;
|
153 | it("converts ".concat(file, " to pdf"), _asyncToGenerator(function* () {
|
154 | fs.mkdirSync('./output', {
|
155 | recursive: true
|
156 | });
|
157 | const promise = new Promise(resolve => {
|
158 | const outputStream = fs.createWriteStream("./output/".concat(file, ".pdf"));
|
159 | outputStream.on('finish', () => {
|
160 | resolve(true);
|
161 | });
|
162 | const options = {
|
163 | info: {
|
164 | title: 'Smart Legal Contract',
|
165 | author: 'Dan',
|
166 | subject: 'Test PDF rendering',
|
167 | keywords: 'accord project, markdown transform, pdf'
|
168 | },
|
169 | defaultStyle: {
|
170 | font: 'LiberationSerif'
|
171 | },
|
172 | tocHeading: 'Table of Contents',
|
173 | headerText: 'Contract ABCDEF',
|
174 | footerText: 'Copyright Acme Inc.',
|
175 | footerPageNumber: true,
|
176 | styles: {
|
177 | Link: {
|
178 | color: 'red',
|
179 | fontSize: 16
|
180 | }
|
181 | }
|
182 | };
|
183 | pdfTransformer.toPdf(JSON.parse(jsonContent), options, outputStream);
|
184 | });
|
185 | return promise;
|
186 | }));
|
187 | });
|
188 | }); |
\ | No newline at end of file |