Markdown Converter – Java

Markdown is a web page archive format that allows embedding multiple resources, such as images, CSS styles, audio, and video files, into a single file, typically with a .mht file extension. Markdown format allows a web page to be saved and sent as a single file, preserving its appearance and interactivity. It can be opened by most web browsers. But sometimes you want to Convert Markdown to get the advantages of other formats.

Aspose.HTML for Java provides a wide range of Markdown conversions to popular formats, such as PDF, XPS, JPG, PNG, BMP, TIFF, and GIF.

This section provides information on the list of supported Markdown conversions and how to perform them using convertMarkdown() methods of the Converter class. It can be different scenarios, but any Markdown conversion can be made with a few required steps:

  1. Load an existing Markdown file. You can load Markdown from a file or stream.
  2. Create a Save Options object. You may customize the rendering process to get the desired result.
  3. Use one of the convertMarkdown() methods to convert Markown to HTML and pass the required parameters to it.
  4. Call one of the convertHTML() methods of the Converter class to convert the HTML to the required output format.

Note: Any conversion from Markdown to other formats goes through the Markdown to HTML conversion stage.

Let’s consider the following code snippet that shows how to convert Markdown to PDF using our Java library:

 1    // Prepare a path to a source Markdown file
 2    String sourcePath = Path.combine(getDataDir(), "nature.md");
 3
 4    // Prepare a path for converted PDF file saving 
 5    String savePath = Path.combine(getOutputDir(), "nature-output.pdf");
 6
 7    // Convert Markdown to HTML
 8    HTMLDocument document = Converter.convertMarkdown(sourcePath);
 9    try {
10        // Initialize PdfSaveOptions
11        PdfSaveOptions options = new PdfSaveOptions();
12
13        // Convert the HTML document to PDF file format
14        com.aspose.html.converters.Converter.convertHTML(document, options, savePath);
15    }
16    finally { if (document != null) document.dispose(); }

Popular Markdown Conversions

You can download the complete examples and data files from GitHub.

Aspose.HTML provides a free online Markdown Converter that enables you to convert Markdown files to various popular formats. This tool can easily convert Markdown to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF. The conversion process is simple and fast. You don’t need any additional software. The best part is that it’s completely free!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.