Skip to content

Commit b30539b

Browse files
dselmanjeromesimeon
authored andcommitted
(feat) convert ciceromark to pdf
Signed-off-by: Dan Selman <[email protected]>
1 parent bd728dd commit b30539b

15 files changed

+567
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
.DS_Store
1616

17+
output
18+
1719
# Downloaded model files
1820
# **/models/@*.cto
1921

packages/markdown-pdf/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
"@accordproject/markdown-common": "0.11.3",
8585
"easy-pdf-parser": "0.0.4",
8686
"jsdom": "^15.2.1",
87-
"type-of": "^2.0.1"
87+
"type-of": "^2.0.1",
88+
"pdfmake" : "0.1.66"
8889
},
8990
"license-check-config": {
9091
"src": [

packages/markdown-pdf/src/PdfTransformer.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,46 @@
1717
const parsePdf = require('easy-pdf-parser').parsePdf;
1818
const extractPlainText = require('easy-pdf-parser').extractPlainText;
1919
const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
20+
const PdfPrinter = require('pdfmake');
21+
const ToPdfMakeVisitor = require('./ToPdfMakeVisitor');
22+
const fonts = {
23+
Courier: {
24+
normal: 'Courier',
25+
bold: 'Courier-Bold',
26+
italics: 'Courier-Oblique',
27+
bolditalics: 'Courier-BoldOblique'
28+
},
29+
Helvetica: {
30+
normal: 'Helvetica',
31+
bold: 'Helvetica-Bold',
32+
italics: 'Helvetica-Oblique',
33+
bolditalics: 'Helvetica-BoldOblique'
34+
},
35+
Times: {
36+
normal: 'Times-Roman',
37+
bold: 'Times-Bold',
38+
italics: 'Times-Italic',
39+
bolditalics: 'Times-BoldItalic'
40+
},
41+
Symbol: {
42+
normal: 'Symbol'
43+
},
44+
ZapfDingbats: {
45+
normal: 'ZapfDingbats'
46+
},
47+
LiberationSerif: {
48+
normal: `${__dirname}/fonts/LiberationSerif-Regular.ttf`,
49+
bold: `${__dirname}/fonts/LiberationSerif-Bold.ttf`,
50+
italics: `${__dirname}/fonts/LiberationSerif-Italic.ttf`,
51+
bolditalics: `${__dirname}/fonts/LiberationSerif-BoldItalic.ttf`
52+
},
53+
Roboto: {
54+
normal: `${__dirname}/fonts/Roboto-Regular.ttf`,
55+
bold: `${__dirname}/fonts/Roboto-Medium.ttf`,
56+
italics: `${__dirname}/fonts/Roboto-Italic.ttf`,
57+
bolditalics: `${__dirname}/fonts/Roboto-MediumItalic.ttf`
58+
}
59+
};
2060

2161
/**
2262
* Converts a PDF to CiceroMark DOM
@@ -44,6 +84,40 @@ class PdfTransformer {
4484
return this.ciceroMarkTransformer.fromMarkdown(plainText, format);
4585

4686
}
87+
88+
/**
89+
* Converts a CiceroMark DOM to a PDF Buffer
90+
* @param {*} input - CiceroMark DOM
91+
* @param {*} outputStream - the output stream
92+
*/
93+
async toPdf(input, outputStream) {
94+
95+
const printer = new PdfPrinter(fonts);
96+
97+
if(!input.getType) {
98+
input = this.ciceroMarkTransformer.getSerializer().fromJSON(input);
99+
}
100+
101+
const parameters = {};
102+
parameters.result = '';
103+
parameters.first = true;
104+
parameters.indent = 0;
105+
const visitor = new ToPdfMakeVisitor(this.options);
106+
input.accept(visitor, parameters);
107+
console.log(JSON.stringify(parameters.result, null, 2));
108+
109+
// let docDefinition = {
110+
// content: [
111+
// 'First paragraph',
112+
// 'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
113+
// ]
114+
// };
115+
116+
// parameters.result
117+
const pdfDoc = printer.createPdfKitDocument(parameters.result);
118+
pdfDoc.pipe(outputStream);
119+
pdfDoc.end();
120+
}
47121
}
48122

49123
module.exports = PdfTransformer;

packages/markdown-pdf/src/PdfTransformer.test.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const fs = require('fs');
2020
const PdfTransformer = require('./PdfTransformer');
21+
const CiceroMarkTransformer = require('@accordproject/markdown-cicero').CiceroMarkTransformer;
2122

2223
let pdfTransformer = null;
2324

@@ -27,7 +28,7 @@ beforeAll(() => {
2728
});
2829

2930
/**
30-
* Get the name and contents of all markdown test files
31+
* Get the name and contents of all pdf test files
3132
* @returns {*} an array of name/contents tuples
3233
*/
3334
function getPdfFiles() {
@@ -44,12 +45,51 @@ function getPdfFiles() {
4445
return result;
4546
}
4647

47-
describe.only('pdf import', () => {
48+
/**
49+
* Get the name and contents of all markdown test files
50+
* @returns {*} an array of name/contents tuples
51+
*/
52+
function getMarkdownFiles() {
53+
const result = [];
54+
const files = fs.readdirSync(__dirname + '/../test/data');
55+
56+
files.forEach(function(file) {
57+
if(file.endsWith('.md')) {
58+
let contents = fs.readFileSync(__dirname + '/../test/data/' + file, 'utf8');
59+
result.push([file, contents]);
60+
}
61+
});
62+
63+
return result;
64+
}
65+
66+
describe('pdf import', () => {
4867
getPdfFiles().forEach(([file, pdfContent], i) => {
4968
it(`converts ${file} to cicero mark`, async () => {
5069
const ciceroMarkDom = await pdfTransformer.toCiceroMark(pdfContent, 'json');
5170
//console.log(JSON.stringify(ciceroMarkDom, null, 4));
5271
expect(ciceroMarkDom).toMatchSnapshot(); // (1)
5372
});
5473
});
74+
});
75+
76+
describe('pdf generation', () => {
77+
getMarkdownFiles().forEach(([file, markdownContent], i) => {
78+
it(`converts ${file} to pdf`, async () => {
79+
const ciceroMarkTransformer = new CiceroMarkTransformer();
80+
const ciceroMarkDom = ciceroMarkTransformer.fromMarkdown(markdownContent, 'json');
81+
console.log(JSON.stringify(ciceroMarkDom, null, 2));
82+
fs.mkdirSync('./output', { recursive: true });
83+
84+
const promise = new Promise( (resolve) => {
85+
const outputStream = fs.createWriteStream(`./output/${file}.pdf`);
86+
outputStream.on('finish', () => {
87+
resolve(true);
88+
});
89+
pdfTransformer.toPdf(ciceroMarkDom, outputStream );
90+
});
91+
92+
return promise;
93+
});
94+
});
5595
});

0 commit comments

Comments
 (0)