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// @md products/html/en/java/converting-between-formats/markdown-converter/convert-markdown-to-pdf/_index.md
 2
 3// Prepare a simple Markdown example
 4String code = "### Hello, World! \n" +
 5        "[visit applications](https://products.aspose.app/html/applications)";
 6
 7// Create a Markdown file
 8FileHelper.writeAllText($o("document.md"), code);
 9
10// Convert Markdown to HTML
11HTMLDocument document = Converter.convertMarkdown($o("document.md"));
12
13// Convert the HTML document to PDF file format
14Converter.convertHTML(document, new PdfSaveOptions(), $o("document-output.pdf"));
 1    // @java java/com/aspose/html/examples/Examples_Java_ConvertingBetweenFormats_ConvertMarkdownToPDF.java
 2    // Prepare a path to a source Markdown file
 3    String sourcePath = "document.md");
 4
 5    // Prepare a simple Markdown example
 6    String code = StringExtensions.concat("### Hello, World!", 
 7                "\r\n", 
 8                "[visit applications](https://products.aspose.app/html/applications)");
 9    // Create a Markdown file
10    File.writeAllText(sourcePath, code);            
11
12    // Convert Markdown to HTML 
13    HTMLDocument document = Converter.convertMarkdown(sourcePath);
14
15    // Prepare a path for converted PDF file saving
16    String savePath = "document-output.pdf");
17
18    
19        // Convert the HTML document to PDF file format
20        Converter.convertHTML(document, new PdfSaveOptions(), savePath);
21        

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.

MetodDescription
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// @md products/html/en/java/converting-between-formats/markdown-converter/convert-markdown-to-pdf/_index.md
 2// Convert Markdown to HTML
 3HTMLDocument document = Converter.convertMarkdown($i("nature.md"));
 4
 5// Initialize PdfSaveOptions. Set up the resolutions, JpegQuality and change the background color to AliceBlue
 6PdfSaveOptions options = new PdfSaveOptions();
 7options.setHorizontalResolution(Resolution.to_Resolution(200));
 8options.setVerticalResolution(Resolution.to_Resolution(200));
 9options.setBackgroundColor(Color.getAliceBlue());
10options.setJpegQuality(100);
11
12// Convert the HTML document to PDF file format
13Converter.convertHTML(document, options, $o("nature-output.pdf"));
 1    // @java java/com/aspose/html/examples/Examples_Java_ConvertingBetweenFormats_ConvertMarkdownToPDF_SpecifyPdfSaveOptions.java
 2    // Prepare a path to a source Markdown file
 3    String sourcePath = "nature.md");
 4
 5    // Prepare a path for converted PDF file saving 
 6    String savePath = "nature-output.pdf");
 7
 8    // Convert Markdown to HTML
 9    HTMLDocument document = Converter.convertMarkdown(sourcePath);
10    
11        // Initialize PdfSaveOptions. Set up the resolutions and change the background color to AliceBlue
12        PdfSaveOptions options = new PdfSaveOptions();
13        options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
14        options.setVerticalResolution(new Resolution(200, UnitType.AUTO));;
15        options.setBackgroundColor(Color.getAliceBlue())       
16
17        // Convert the HTML document to PDF file format
18        Converter.convertHTML(document, options, savePath);
19        

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.