Convert Markdown to PDF in Java

Markdown is a lightweight markup language for writing and structuring content, but it is not optimized for printing or fixed-layout distribution. Converting Markdown to PDF creates a portable document suitable for sharing, archiving, and printing. This article explains basic and customized Markdown to PDF conversion scenarios and shows how to use PdfSaveOptions.

To convert Markdown to PDF in Java, call Converter.convertMarkdown() to create an HTMLDocument, configure PdfSaveOptions, and pass both to Converter.convertHTML(). Markdown to PDF conversion uses HTML as an intermediate format.

Markdown to PDF

The static methods of the Converter class provide a straightforward way to convert Markdown files into other formats. Follow these steps to convert Markdown to PDF:

  1. Prepare the source Markdown file.
  2. Call convertMarkdown(sourcePath) to convert the Markdown file to an HTMLDocument.
  3. Create a PdfSaveOptions object with the default settings.
  4. Call convertHTML(document, options, savePath) to save the intermediate HTML document as a PDF file.

The following Java code writes a Markdown source, obtains an intermediate HTMLDocument, and converts it to PDF:

 1// Convert Markdown to PDF using Aspose.HTML for Java
 2
 3// Prepare a simple Markdown example
 4String code = "### Hello, World! \n\n" +
 5        "[visit applications](https://products.aspose.app/html/applications)";
 6
 7// Create a Markdown file
 8FileHelper.writeAllText("document.md", code);
 9
10// Convert Markdown to HTML
11HTMLDocument document = Converter.convertMarkdown("document.md");
12
13// Convert the HTML document to PDF file format
14Converter.convertHTML(document, new PdfSaveOptions(), "document-output.pdf");

The example converts the generated document.md file and saves the result as document-output.pdf.

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

Save Options – PdfSaveOptions Class

Aspose.HTML for Java supports Markdown to PDF conversion with default or custom save options. The PdfSaveOptions class lets you configure page setup, CSS processing, background color, image settings, and PDF encryption.

MethodDescription
setJpegQuality(value)Sets the JPEG compression quality when JPEG compression is used. The default value is 95.
getCss()Returns a CssOptions object used to configure CSS property processing.
setBackgroundColor(value)Sets the background color of every page. The default value is Transparent.
getPageSetup()Returns the page setup object used to configure the output page size and margins.
setHorizontalResolution(value)Sets the horizontal resolution for internal images used during filter processing. The default value is 300 dpi.
setVerticalResolution(value)Sets the vertical resolution for internal images used during filter processing. The default value is 300 dpi.
setEncryption(value)Sets PDF encryption details. If this value is not set, the output PDF is not encrypted.

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

Convert Markdown to PDF Using PdfSaveOptions

The following Java example uses PdfSaveOptions to convert Markdown to PDF with custom internal-image resolution, background color, and JPEG quality:

  1. Load a Markdown file.
  2. Convert Markdown to HTML using one of the convertMarkdown() methods.
  3. Create a PdfSaveOptions object and configure it:
    • Call setHorizontalResolution() and setVerticalResolution() to set the resolution for internal images used during filter processing.
    • Call setBackgroundColor() to set the page background color.
    • Call setJpegQuality() to set the JPEG compression quality when JPEG compression is used.
  4. Call convertHTML(document, options, savePath) to convert the intermediate HTML document to PDF.
 1// Convert Markdown to PDF in Java with custom settings
 2
 3// Convert Markdown to HTML
 4HTMLDocument document = Converter.convertMarkdown("nature.md");
 5
 6// Initialize PdfSaveOptions. Set up the resolutions, JpegQuality and change the background color to AliceBlue
 7PdfSaveOptions options = new PdfSaveOptions();
 8options.setHorizontalResolution(Resolution.to_Resolution(200));
 9options.setVerticalResolution(Resolution.to_Resolution(200));
10options.setBackgroundColor(Color.getAliceBlue());
11options.setJpegQuality(100);
12
13// Convert the HTML document to PDF file format
14Converter.convertHTML(document, options, "nature-output.pdf");

The example saves nature-output.pdf with 200 dpi internal-image resolution, an Alice Blue page background, and JPEG quality set to 100.

Related Markdown Conversion Guides

Other Platforms

FAQ

Can I convert Markdown to PDF for free?

Yes. Use the free online Markdown to PDF Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.

Do I need to save an intermediate HTML file?

No. Call Converter.convertMarkdown(sourcePath) to obtain an HTMLDocument in memory, then pass that document directly to Converter.convertHTML() with PdfSaveOptions and the PDF output path. The examples on this page use this workflow.

Try Online Markdown to PDF Conversion

Use the free online Markdown to PDF Converter for quick manual conversion without writing Java code.

Free Online Markdown to PDF Converter