Skip to content

Instantly share code, notes, and snippets.

@awerlang
Created March 6, 2017 17:29
Show Gist options
  • Save awerlang/87b397430779d1142d32c7491931937e to your computer and use it in GitHub Desktop.
Save awerlang/87b397430779d1142d32c7491931937e to your computer and use it in GitHub Desktop.
markdown to pdf
// from: https://github.com/devongovett/pdfkit/issues/29#issuecomment-103121102
/**
* Parses an input String with the help of the "marked"-package and writes the corresponding elements to the supplied document
* @param doc - the PDFDocument to write to
* @param input - the input String
* @returns {object} - the document for chaining
*/
PDFDocument.parseMarkdown = function (doc, input){
// Array of markdown parts as objects
var markdown = marked.lexer(input);
// loop through the Array, going over each markdown-part individually
markdown.forEach(function(part, index){
if(part.type === 'paragraph'){
doc.font('Normal').fontSize(8).text(part.text, {lineGap: 1.3}).moveDown();
}else if(part.type === 'heading'){
var fontsize = 8;
// depth defines if it's a h1, h2, h3 etc.
switch(part.depth){
case 1: fontsize = 12; break;
case 2: fontsize = 10; break;
default: fontsize = 8;
}
doc.fontSize(4).moveDown().font('Heading2').fontSize(fontsize).text(part.text).fontSize(8).moveDown();
}else if(part.type === 'text' && markdown[index-1].type === 'list_item_start'){
doc.font('Normal').fontSize(8).list([part.text], {lineGap: 1.3});
}else if(part.type === 'list_end'){
doc.fontSize(8).moveDown();
}
});
return doc;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment