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:
- Load a Markdown file. You can load Markdown from a file or stream. In the following example, we create Markdown from scratch.
- Use the
convertMarkdown(
sourcePath
) method to convert Markdown to HTML document. - Create a new PdfSaveOptions object.
- 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 parametersdocument
,options
, andsavePath
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 simple Markdown example
2String code = "### Hello, World! \n\n" +
3 "[visit applications](https://products.aspose.app/html/applications)";
4
5// Create a Markdown file
6FileHelper.writeAllText($o("document.md"), code);
7
8// Convert Markdown to HTML
9HTMLDocument document = Converter.convertMarkdown($o("document.md"));
10
11// Convert the HTML document to PDF file format
12Converter.convertHTML(document, new PdfSaveOptions(), $o("document-output.pdf"));
You can download the complete examples and data files from GitHub.
Save Options – PdfSaveOptions Class
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, media type, etc.
Metod | Description |
---|---|
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. |
setEncryption | This 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:
- Load a Markdown file.
- Convert Markdown to HTML using one of the
convertMarkdown()
methods. - 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()
andVerticalResolution()
methods to set horizontal and vertical resolutions for output PDF file. - Use the
setBackgroundColor()
method to set the color that fills the background.
- Use the
- 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// Convert Markdown to HTML
2HTMLDocument document = Converter.convertMarkdown($i("nature.md"));
3
4// Initialize PdfSaveOptions. Set up the resolutions, JpegQuality and change the background color to AliceBlue
5PdfSaveOptions options = new PdfSaveOptions();
6options.setHorizontalResolution(Resolution.to_Resolution(200));
7options.setVerticalResolution(Resolution.to_Resolution(200));
8options.setBackgroundColor(Color.getAliceBlue());
9options.setJpegQuality(100);
10
11// Convert the HTML document to PDF file format
12Converter.convertHTML(document, options, $o("nature-output.pdf"));
Conclusion
Markdown is a simple and effective markup language, but its limitations in formatting and optimization for printing highlight the need to convert Markdown to more universal formats such as PDF. Aspose.HTML for Java provides a robust solution for converting Markdown to PDF using an intermediate Markdown to HTML processing step. With static methods on the Converter
class and configurable PdfSaveOptions
, developers have full control over conversion options such as page settings, image quality, background colors, resolutions, etc.
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!