Turn markdown files to a PDF or HTML document with the help of this java library. You can also use CSS styles to format your documents.
First, you have to assemble a Document object. Then you use a Generator to generate you document.
There are currently two generators in the library:
PdfGenerator
HTMLGenerator
Both have a generate(Document document)
method which returns an Output
object.
Here is an example how to use is as a library:
ContentFactory contentFactory = ContentFactory.getInstance();
Document document = Document
.builder()
.markdownContents(Arrays.asList(
contentFactory.create("# Sample header \n sample content "),
contentFactory.create(new URL("https://floppylab.com/resources/markdown2document/sample.md"))
))
.styles(Arrays.asList(
contentFactory.create("body { font-family: sans-serif; color: #555; /* some comment*/ }"),
new Link("https://floppylab.com/resources/markdown2document/sample.css")
)).build();
PdfGenerator pdfGenerator = new PdfGenerator();
Output output = pdfGenerator.generate(document);
output.toFile("sample.pdf");
HtmlGenerator htmlGenerator = new HtmlGenerator();
output = htmlGenerator.generate(document);
output.toFile("sample.html");
A document can consist of:
- a list of markdown contents -
List<Content> markdownContents
- a list of style inputs -
List<Input> styleInputs
- base uri for relative links (images, links, etc) -
String baseUri
Input is an abstract class and there are two classes that extend it:
Content
Link
A Content
object can be constructed directly from:
String
- as a text
But also can be constructed by ContentFactory
from:
String
- as a textURL
- url where the content can be foundPath
- path to file with optionalCharset
InputStream
- stream with content
A Link
object can be constructed from:
String
- link
An Output object contains the contents of the generated document, and it has the following methods:
toString
- returns the content as aString
toOutputStream
- returns the content as anOutputStream
toFile(String name)
- writes the content into a file with the given name (FileOutputStream
is used)
java -jar markdown2document-1.0.0.jar -h
java -jar markdown2document-1.0.0.jar -mc "# title","*italic text*" -pf "sample.pdf"
You can use it as a command line tool with the following options:
-
markdown contents
- short:
mc
- long:
markdown-contents
- multiple values
- short:
-
markdown urls
- short:
mu
- long:
markdown-urls
- multiple values
- short:
-
markdown file paths
- short:
mp
- long:
markdown-paths
- multiple values
- short:
-
style links
- short:
sl
- long:
style-links
- multiple values
- short:
-
style contents
- short:
sc
- long:
style-contents
- multiple values
- short:
-
style urls
- short:
su
- long:
style-urls
- multiple values
- short:
-
style paths
- short:
sp
- long:
style-paths
- multiple values
- short:
- base uri
- short:
bu
- long:
base-uri
- short:
-
to pdf file with filename
- short:
pf
- long:
to-pdf
- short:
-
to html file with filename
- short:
hf
- long:
to-html
- short:
-
to html string
- short:
hs
- long:
to-html-string
- short:
- help
- short:
h
- long:
help
- short:
This library is based on a few other libraries so credit goes to the contributors of these projects.
The library uses CommonMark to parse the markdown input contents.
Learn more about it here:
commonmark-java is a Java library for parsing and rendering Markdown text according to the CommonMark specification by Atlassian.
Check the project on GitHub.
openhtmltopdf is an HTML to PDF library for the JVM.
Check the project on GitHub.
Please see CONTRIBUTING.md file for details.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our project(s) you are using.
Our address is: Gedőci utca 25., 3100 Salgótarján, Hungary.
We publish all received postcards on our website.
The MIT License (MIT). Please see LICENSE file for more information.