Convert Markdown to PDF in Java

Markdown is a lightweight markup language, and while Markdown is an excellent tool for writing and structuring content, it has limited formatting options, is not optimized for printing, etc. Converting Markdown to PDF allows you to take advantage of the PDF format. PDF comes with many benefits, and Markdown to PDF conversion can be used for sharing, formatting, archiving, or printing web pages. In this article, you will find information about Markdown to PDF conversion scenarios and learn how to use PdfSaveOptions.

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

Markdown to PDF

The static methods of the Converter class are primarily used as the easiest way to convert a Markdown file into various formats. Let’s walk through the step-by-step instructions for a simple Markdown to PDF conversion scenario:

  1. Load a Markdown file. You can load Markdown from a file or stream. In the following example we create Markdown from scratch.
  2. Use the convertMarkdown(sourcePath) method to convert Markdown to HTML document.
  3. Create a new PdfSaveOptions object.
  4. Use the сonvertHTML(document, options, savePath) method of the Converter class to save an intermediate HTML document as a PDF file. The method takes as parameters document, options, and savePath and performs the conversion.

Please review the following Java code snippet, which shows the Markdown to PDF conversion process with step-by-step instructions:

 1    // Prepare a path to a source Markdown file
 2    String sourcePath = Path.combine(getOutputDir(), "");
 4    // Prepare a simple Markdown example
 5    String code = StringExtensions.concat("### Hello, World!", 
 6                "\r\n", 
 7                "[visit applications](");
 8    // Create a Markdown file
 9, code);            
11    // Convert Markdown to HTML 
12    HTMLDocument document = Converter.convertMarkdown(sourcePath);
14    // Prepare a path for converted PDF file saving
15    String savePath = Path.combine(getOutputDir(), "document-output.pdf");
17    try {
18        // Convert the HTML document to PDF file format
19        com.aspose.html.converters.Converter.convertHTML(document, new PdfSaveOptions(), savePath);
20    }
21    finally { if (document != null) document.dispose(); }    

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

Save Options

Aspose.HTML for Java allows converting Markdown to PDF using default or custom save options. PdfSaveOptions allows you to customize the rendering process. You can specify the page size, margins, file permissions, CSS media-type, etc.

setJpegQuality(value)Specifies the quality of JPEG compression for images. The default value is 95.
getCss()Gets a CssOptions object which is used for configuration of CSS properties processing.
setBackgroundColor(value)Sets the color that will fill the background of every page. By default, this property is Transparent.
setPageSetup(value)This method sets a page setup object and uses it for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for internal images, in pixels per inch. By default this property is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
setEncryptionThis method gets or sets encryption details. If it is not set, then no encryption will be performed.

For further information on how to customize the conversion process with PdfSaveOptions, you can refer to the Fine-Tuning Converters article.

Convert Markdown to PDF in Java using PdfSaveOptions

With Aspose.HTML for Java, you can convert files programmatically with full control over a wide range of conversion parameters. To convert Markdown to PDF with PdfSaveOptions specifying, you should follow a few steps:

  1. Load a Markdown file.
  2. Convert Markdown to HTML using one of the convertMarkdown() methods.
  3. Create a new PdfSaveOptions object and specify the required properties. In the following example, we apply a custom resolutions and background color for the resulting PDF document:
    • Use the setHorizontalResolution() and VerticalResolution() methods to set horizontal and vertical resolutions for output PDF file.
    • Use the setBackgroundColor() method to set the color that fills the background.
  4. Call the сonvertHTML(document, options, savePath) method of the Converter class.

The following Java example shows how to use PdfSaveOptions and create a PDF file with custom resolutions and background color:

 1    // Prepare a path to a source Markdown file
 2    String sourcePath = Path.combine(getDataDir(), "");
 4    // Prepare a path for converted PDF file saving 
 5    String savePath = Path.combine(getOutputDir(), "nature-output.pdf");
 7    // Convert Markdown to HTML
 8    HTMLDocument document = Converter.convertMarkdown(sourcePath);
 9    try {
10        // Initialize PdfSaveOptions. Set up the resolutions and change the background color to AliceBlue
11        PdfSaveOptions options = new PdfSaveOptions();
12        options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
13        options.setVerticalResolution(new Resolution(200, UnitType.AUTO));;
14        com.aspose.html.drawing.Color.getAliceBlue().CloneTo(options.getBackgroundColor());       
16        // Convert the HTML document to PDF file format
17        com.aspose.html.converters.Converter.convertHTML(document, options, savePath);
18    }
19    finally { if (document != null) document.dispose(); }    

Aspose.HTML offers a free online Markdown to PDF Converter that converts Markdown to PDF with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Text “Banner Markdown to PDF Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.