-
Notifications
You must be signed in to change notification settings - Fork 21
/
pdf_generate_module.js
57 lines (44 loc) · 1.67 KB
/
pdf_generate_module.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
// https://github.com/maiers/pdfkit-commonmark
import './commonmark/commonmark.js';
const commonmark = window.commonmark;
import './pdfkit-commonmark/commonmark-pdfkit-renderer.js';
import './pdfkit/pdfkit.standalone.js';
import './pdfkit/blob-stream.js'
console.log("pdf commonmark: ", commonmark);
console.log("pdf window.commonmark: ", window.commonmark);
console.log("pdf window.commonmarkParser: ", window.commonmarkParser);
console.log("CommonmarkPDFKitRenderer: ", CommonmarkPDFKitRenderer);
console.log("window.CommonmarkPDFKitRenderer: ", window.CommonmarkPDFKitRenderer);
window.text_to_pdf = async (text) => {
console.log("pdf_generate_module: in text_to_pdf. text: ", text);
// TODO: add a mark to each page to indicate it was likely (partially) generated with AI?
return new Promise((resolve, reject) => {
try{
// get parser instance
const reader = new commonmark.Parser();
//const reader = new window.commonmarkParser();
// parse input
const parsed = reader.parse(text);
// get pdf renderer instance
const writer = new CommonmarkPDFKitRenderer();
// create pdf document
const doc = new PDFDocument();
var stream = doc.pipe(blobStream());
// write pdf to some file in the current directory
console.log("pdf: writer: ", writer);
// render parsed markdown to pdf
writer.render(doc, parsed);
// end the document
doc.end();
console.log("pdf doc after end: ", doc);
stream.on('finish', function() {
console.log("PDF generation stream finished");
resolve(stream.toBlobURL('application/pdf'));
});
}
catch(err){
console.error("pdf_generate_module: text_to_pdf: caught error: ", err);
reject();
}
})
}