Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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:
sourcePath) to convert the Markdown file to an HTMLDocument.PdfSaveOptions object with the default settings.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.
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.
| Method | Description |
|---|---|
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.
The following Java example uses PdfSaveOptions to convert Markdown to PDF with custom internal-image resolution, background color, and JPEG quality:
convertMarkdown() methods.PdfSaveOptions object and configure it:setHorizontalResolution() and setVerticalResolution() to set the resolution for internal images used during filter processing.setBackgroundColor() to set the page background color.setJpegQuality() to set the JPEG compression quality when JPEG compression is used.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.
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.
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.
Use the free online Markdown to PDF Converter for quick manual conversion without writing Java code.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.