Convert Markdown files to PDF with Nutrient Java SDK.

Markdown is a supported import format. The SDK enables you to:

  • Load Markdown from a file path or stream
  • Convert Markdown to PDF
  • Render Markdown as images
  • Process Markdown with standard SDK document APIs
Download sample

Preparing the project

Define a package and create a class for this conversion flow:

package io.nutrient.Sample;

Import the required classes from Nutrient Java SDK:

import io.nutrient.sdk.Document;
import io.nutrient.sdk.exceptions.NutrientException;
public class MarkdownToPDF {

Create a main method and declare NutrientException:

public static void main(String[] args) throws NutrientException {

Loading and converting the Markdown document

Load the Markdown file with a try-with-resources(opens in a new tab) statement so resources close correctly:

try (Document document = Document.open("input.md")) {

Document.open detects the format and parses the Markdown content. Then export it as PDF:

document.exportAsPdf("output.pdf");
}
}
}

exportAsPdf renders Markdown structure into formatted PDF output, including headings, lists, tables, and code blocks.

Error handling

The SDK throws NutrientException when an operation fails. Handle this exception in your app for custom logging, retries, or fallback logic.

Conclusion

You now have a complete Markdown-to-PDF conversion flow in Java.