Created
June 4, 2014 03:29
-
-
Save adamh/2f19adf1a51e88b2467c to your computer and use it in GitHub Desktop.
markdownj (markdown to html to pdf)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"org.markdownj" % "markdownj" % "0.3.0-1.0.2b4", | |
"org.ccil.cowan.tagsoup" % "tagsoup" % "1.2.1", | |
"org.xhtmlrenderer" % "core-renderer" % "R8" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.xhtmlrenderer.pdf.ITextRenderer | |
import org.ccil.cowan.tagsoup.Parser | |
import org.apache.xalan.xsltc.trax.SAX2DOM | |
import org.xml.sax.InputSource | |
import com.petebevin.markdown.MarkdownProcessor | |
import org.xml.sax | |
import java.io.{ByteArrayOutputStream, FileOutputStream, File, ByteArrayInputStream} | |
object PdfUtil { | |
def markdown2pdf(input:String) = { | |
val mp = new MarkdownProcessor() | |
val html = s"<html><body>${mp.markdown(input)}</body></html>" | |
// Convert from html ro w3c doc | |
val parser = new Parser() | |
val sax2dom = new SAX2DOM() | |
parser.setContentHandler(sax2dom) | |
parser.setFeature(Parser.namespacesFeature, false) | |
parser.parse(new sax.InputSource(new ByteArrayInputStream(html.getBytes()))) | |
// Use document to create pdf | |
val renderer = new ITextRenderer() | |
renderer.setDocument(sax2dom.getDOM().asInstanceOf[org.w3c.dom.Document], null) | |
renderer.layout() | |
val b = new ByteArrayOutputStream() | |
renderer.createPDF(b) | |
b | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment