Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/97b4f6ee58eff17fc00ad11387ea0f3e to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/97b4f6ee58eff17fc00ad11387ea0f3e to your computer and use it in GitHub Desktop.
This Gist contains examples Aspose.HTML Converting Between Formats

Convert HTML, MHTML, SVG, EPUB, and Markdown to PDF, Images, etc. in Java

This gist repository provides ready-to-use Java code snippets from the Converting Between Formats chapter of the Aspose.HTML for Java documentation. The examples demonstrate how to convert HTML, SVG, EPUB, MHTML, and Markdown documents into multiple formats, including PDF, DOCX, XPS, and popular image types. Additionally, it includes examples for fine-tuning converters, giving developers precise control over rendering and output options.

Topics Covered

  1. Convert HTML, SVG, EPUB, MHTML, and Markdown documents into multiple output formats including PDF, DOCX, and images.
  2. Control every step of the conversion with options for page setup, resolution, background color, and resource management.
  3. Use the unified Converter API to handle different input formats with the same consistent and reliable workflow.

What's Included?

  • HTML Converter – Convert HTML to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, GIF, MD, and MHTML formats.
  • SVG Converter – Convert SVG to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
  • EPUB Converter – Convert EPUB to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
  • MHTML Converter – Convert MHTML to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
  • Markdown Converter – Convert MD to HTML, PDF, XPS, DOCX, and image file formats.
  • Fine-Tuning Converters – Alternative rendering approaches that allow greater control over conversion settings, including page size, CSS media, background color, resolution, permissions, and stream handling.

Quick Start

  1. Install the latest version of Aspose.HTML for Java.
  2. Configure the Aspose repository and add the Aspose.HTML for Java dependency as described in the installation guide.
  3. Obtain a free trial package and apply a temporary license for full functionality.
  4. Explore the gists in this repository and copy the code examples relevant to your task.
  5. Adjust file paths, options, and input data according to your environment before running the samples.

About Aspose.HTML for Java

Aspose.HTML for Java is a feature-rich library that enables developers to parse, edit, and convert HTML and related file formats such as SVG, MHTML, XHTML, Markdown, and EPUB. It includes built-in support for CSS, JavaScript, DOM traversal, XPath queries, rendering options, and a variety of export formats, making it a powerful tool for Java applications that require HTML processing.

Contribute

If you have questions, issues, or suggestions about these examples, please visit the Aspose.HTML Support Forum for help or feedback.

Documentation and Resources

Requirements

  • Java SE 8 (or higher).
  • Supported operating systems: Windows, macOS, Linux.
  • Java development environment (IntelliJ IDEA, Eclipse, or similar) and a build tool (Maven or Gradle).
  • Aspose.HTML for Java library.
Aspose.HTML for Java - Converting Between Formats
// Merge HTML to PDF using Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code
String code1 = "<br><span style='color: green'>Hello, World!!</span>";
String code2 = "<br><span style='color: blue'>Hello, World!!</span>";
String code3 = "<br><span style='color: red'>Hello, World!!</span>";
// Create three HTML documents to merge later
HTMLDocument document1 = new HTMLDocument(code1, ".");
HTMLDocument document2 = new HTMLDocument(code2, ".");
HTMLDocument document3 = new HTMLDocument(code3, ".");
// Create an instance of HTML Renderer
HtmlRenderer renderer = new HtmlRenderer();
// Create an instance of the PdfDevice class
PdfDevice device = new PdfDevice("output.pdf");
// Merge all HTML documents to PDF
renderer.render(device, new HTMLDocument[]{document1, document2, document3});
// Convert EPUB to JPG in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-jpg/
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setHorizontalResolution(Resolution.to_Resolution(400));
options.setVerticalResolution(Resolution.to_Resolution(400));
options.setBackgroundColor(Color.getAliceBlue());
options.getPageSetup().setAnyPage(new Page(new Size(800, 500), new Margin(30, 20, 10, 10)));
// Convert EPUB to JPG
Converter.convertEPUB(fileInputStream, options, "input-options.jpg");
// Convert EPUB to JPEG using Aspose.HTML for Java
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-jpg/
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Call the сonvertEPUB() method to convert EPUB to JPG
Converter.convertEPUB(fileInputStream, options, "input-output.jpg");
// Convert EPUB to JPG using Java
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-jpg/
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Invoke the convertEPUB() method to convert EPUB to JPG
Converter.convertEPUB(fileInputStream, new ImageSaveOptions(ImageFormat.Jpeg), "convert-in-two-lines.jpg");
// Convert EPUB to PDF in Java
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-pdf/
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of the PdfSaveOptions class
PdfSaveOptions options = new PdfSaveOptions();
// Call the convertEPUB() method to convert EPUB to PDF
Converter.convertEPUB(fileInputStream, options, "output-epub-to-pdf.pdf");
// Convert EPUB to PDF using Java
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-pdf/
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Invoke the convertEPUB() method to convert EPUB to PDF
Converter.convertEPUB(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");
// Convert EPUB to PDF in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-pdf/
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions();
options.setBackgroundColor(Color.getAliceBlue());
options.getPageSetup().setAnyPage(new Page());
options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(1000), Length.fromPixels(1000)));
// Call the convertEPUB() method to convert EPUB to PDF
Converter.convertEPUB(fileInputStream, options, "input-options.pdf");
// Convert EPUB to PNG in Java
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-png/
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Call the сonvertEPUB() method to convert EPUB to PNG
Converter.convertEPUB(fileInputStream, options, "input-output.png");
// Convert EPUB to PNG using Java
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-png/
// Open an existing EPUB file for reading
FileInputStream inputStream = new FileInputStream("input.epub");
// Convert EPUB to PNG
Converter.convertEPUB(inputStream, new ImageSaveOptions(), "convert-with-single-line.png");
// Convert EPUB to PNG in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-epub-to-png/
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions();
options.setBackgroundColor(Color.getAliceBlue());
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setVerticalResolution(Resolution.to_Resolution(400));
options.setHorizontalResolution(Resolution.to_Resolution(400));
// Call the convertEPUB() method to convert EPUB to PNG
Converter.convertEPUB(fileInputStream, options, "input-options.png");
// Populate an HTML template with structured JSON data using Java
// Learn more: https://docs.aspose.com/html/java/html-template/
// Prepare a JSON data-source and save it to a file
String data =
"{\n" +
" 'FirstName': 'John',\n" +
" 'LastName': 'Smith',\n" +
" 'Address': {\n" +
" 'City': 'Dallas',\n" +
" 'Street': 'Austin rd.',\n" +
" 'Number': '200'\n" +
" }\n" +
"}";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("data-source.json")) {
fileWriter.write(data);
}
// Prepare an HTML Template and save it to a file
String template =
"<table border=1>\n" +
" <tr>\n" +
" <th>Person</th>\n" +
" <th>Address</th>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>{{FirstName}} {{LastName}}</td>\n" +
" <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>\n" +
" </tr>\n" +
"</table>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("template.html")) {
fileWriter.write(template);
}
// Convert Template to HTML
com.aspose.html.converters.Converter.convertTemplate(
"template.html",
new com.aspose.html.converters.TemplateData("data-source.json"),
new com.aspose.html.loading.TemplateLoadOptions(), "document.html"
);
// Convert HTML to BMP using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-image/
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("bmp.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("bmp.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Convert HTML to BMP
Converter.convertHTML(document, options, "bmp-output.bmp");
// Convert HTML to DOCX using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-docx/
// Initialize an HTML document from a file
HTMLDocument document = new HTMLDocument("canvas.html");
// Initialize DocSaveOptions
DocSaveOptions options = new DocSaveOptions();
// Convert HTML to DOCX
Converter.convertHTML(document, options, "canvas-output.docx");
// Convert HTML to DOCX in Java with custom page size and margins
// Learn more: https://docs.aspose.com/html/java/convert-html-to-docx/
// Initialize an HTML document from a file
HTMLDocument document = new HTMLDocument("canvas.html");
// Initialize DocSaveOptions. Set up the pag size 600x400 pixels and margins
DocSaveOptions options = new DocSaveOptions();
options.getPageSetup().setAnyPage(new Page(new Size(600, 400), new Margin(10, 10, 10, 10)));
// Convert HTML to DOCX
Converter.convertHTML(document, options, "canvas-output-options.docx");
// Convert HTML to DOCX in one line using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-docx/
// Invoke the convertHTML() method to convert HTML to DOCX
Converter.convertHTML("<h1>Convert HTML to DOCX!</h1>", ".", new DocSaveOptions(), "convert-with-single-line.docx");
// Convert HTML to GIF using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-image/
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("gif.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("gif.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Convert HTML to GIF
Converter.convertHTML(document, options, "gif-output.gif");
// Convert HTML to JPG using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-image/
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("jpg.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("jpg.html");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Convert HTML to JPG
Converter.convertHTML(document, options, "jpg-output.jpg");
// Convert HTML to JPG in Java with with custom background, resolution, and page size settings
// Learn more: https://docs.aspose.com/html/java/convert-html-to-image/
// Prepare HTML code and save it to a file
String code = "<h1> Image SaveOptions </h1> " +
"<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of " +
"conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>";
FileHelper.writeAllText("save-options.html", code);
// Initialize an HTML Document from the HTML file
HTMLDocument document = new HTMLDocument("save-options.html");
// Initialize an ImageSaveOptions object and customize save options
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
options.setBackgroundColor(Color.getAntiqueWhite());
options.getPageSetup().setAnyPage(new Page(new Size(500, 250), new Margin(40, 40, 20, 20)));
// Convert HTML to JPG
Converter.convertHTML(document, options, "save-options-output.jpg");
// Convert HTML to JPG in one line using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-image/
// Invoke the convertHTML() method to convert HTML code to image
Converter.convertHTML(
"<h1>Convert HTML to JPG!</h1>",
".",
new ImageSaveOptions(ImageFormat.Jpeg),
"convert-with-single-line.jpg"
);
// Convert HTML to Markdown in Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-markdown/
// Prepare HTML code and save it to a file
String code = "<h1>Convert HTML to Markdown Using Java</h1>" +
"<h2>How to Convert HTML to MD in Java</h2>" +
"<p>The Aspose.HTML for Java library allows you to convert HTML to Markdown.</p>";
FileHelper.writeAllText("conversion.html", code);
// Call ConvertHTML() method to convert HTML to Markdown
Converter.convertHTML("conversion.html", new MarkdownSaveOptions(), "conversion.md");
// Convert HTML to Markdown in Java using Git syntax
// Learn more: https://docs.aspose.com/html/java/convert-html-to-markdown/
// Prepare HTML code and save it to a file
String code = "<h1>Header 1</h1>" +
"<h2>Header 2</h2>" +
"<p>Hello, World!!</p>";
FileHelper.writeAllText("document.html", code);
// Call convertHTML() method to convert HTML to Markdown
Converter.convertHTML("document.html", MarkdownSaveOptions.getGit(), "output-git.md");
// Convert selective HTML tags to Markdown using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-markdown/
// Prepare HTML code and save it to the file
String code = "<h1>Header 1</h1>" +
"<h2>Header 2</h2>" +
"<p>Hello, World!!</p>" +
"<a href='aspose.com'>aspose</a>";
FileHelper.writeAllText("options.html", code);
// Create an instance of SaveOptions and set up the rule:
// - only <a> and <p> elements will be converted to Markdown
MarkdownSaveOptions options = new MarkdownSaveOptions();
options.setFeatures(MarkdownFeatures.Link | MarkdownFeatures.AutomaticParagraph);
// Call the convertHTML() method to convert HTML to Markdown
Converter.convertHTML("options.html", options, "options-output.md");
// Convert HTML to MHTML in Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-mhtml/
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("drawing.html");
// Initialize MHTMLSaveOptions
MHTMLSaveOptions options = new MHTMLSaveOptions();
// Convert HTML to MHTML
Converter.convertHTML(document, options, "drawing-output.mht");
// Convert HTML to MHTML with custom settings using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-mhtml/
// Prepare HTML code with a link to another file and save it to the file as "document.html"
String code = "<span>Hello, World!!</span> " +
"<a href='document2.html'>click</a>";
FileHelper.writeAllText("document.html", code);
// Prepare HTML code and save it to the file as "document2.html"
code = "<span>Hello, World!!</span>";
FileHelper.writeAllText("document2.html", code);
// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
MHTMLSaveOptions options = new MHTMLSaveOptions();
options.getResourceHandlingOptions().setMaxHandlingDepth(1);
// Convert HTML to MHTML
Converter.convertHTML("document.html", options, "output-options.mht");
// Convert HTML to MHTML in one line using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-mhtml/
// Invoke the convertHTML() method to convert HTML code to MHTML file
Converter.convertHTML("<h1>Hello, Word!</h1>", ".", new MHTMLSaveOptions(), "convert-with-single-line.mht");
// Convert HTML to PDF using Java
// Learn more: https://docs.aspose.com/html/java/html-to-pdf-converter/
// Prepare HTML code and save it to a file
String code = "<span>Hello, World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("link-to-incoming-document.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("link-to-incoming-document.html");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert HTML to PDF
Converter.convertHTML(document, options, "document-output.pdf");
// Convert HTML to PDF in one line using Java
// Learn more: https://docs.aspose.com/html/java/html-to-pdf-converter/
// Invoke the convertHTML() method to convert the HTML to PDF
Converter.convertHTML("<h1>Convert HTML to PDF!</h1>", ".", new PdfSaveOptions(), "convert-with-single-line.pdf");
// Convert HTML to PDF in Java with custom page settings
// Learn more: https://docs.aspose.com/html/java/html-to-pdf-converter/
// Initialize an HTML document from a file
HTMLDocument document = new HTMLDocument("drawing.html");
// Initialize PdfSaveOptions. Set up the page-size 500x300 pixels, margins,
// resolutions and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions();
options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
options.setBackgroundColor(Color.getAliceBlue());
options.setJpegQuality(100);
options.getPageSetup().setAnyPage(new Page(new Size(500, 300), new Margin(20, 10, 10, 10)));
// Convert HTML to PDF
Converter.convertHTML(document, options, "drawing-options.pdf");
// Convert HTML to PDF with custom settings using Java
// Learn more: https://docs.aspose.com/html/java/html-to-pdf-converter/
// Prepare HTML code and save it to a file
String code = "<h1> PdfSaveOptions Class</h1> " +
"<p>Using PdfSaveOptions Class, you can programmatically " +
"apply a wide range of conversion parameters " +
"such as BackgroundColor, HorizontalResolution, VerticalResolution, PageSetup, etc.</p>";
FileHelper.writeAllText("save-options.html", code);
// Initialize an HTML Document from the HTML file
HTMLDocument document = new HTMLDocument("save-options.html");
// Set up the page-size, margins and change the background color to AntiqueWhite
PdfSaveOptions options = new PdfSaveOptions();
options.setBackgroundColor(Color.getAntiqueWhite());
options.getPageSetup().setAnyPage(
new Page(
new Size(Length.fromInches(4.9f), Length.fromInches(3.5f)),
new Margin(30, 20, 10, 10)
)
);
// Convert HTML to PDF
Converter.convertHTML(document, options, "save-options-output.pdf");
// Convert HTML to PNG using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-image/
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("png.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("png.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Convert HTML to PNG
Converter.convertHTML(document, options, "png-output.png");
// Convert HTML to PNG in Java
// Learn more: https://docs.aspose.com/html/java/html-converter/
// Initialize an HTML document from a file
HTMLDocument document = new HTMLDocument("nature.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Convert HTML to PNG
Converter.convertHTML(document, options, "nature-output.png");
// Convert HTML to TIFF using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-image/
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("tiff.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("tiff.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Convert HTML to TIFF
Converter.convertHTML(document, options, "tiff-output.bmp");
// Convert HTML to XPS using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-xps/
// Prepare HTML code and save it to a file
String code = "<span>Hello, World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("document.html");
// Initialize XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions();
// Convert HTML to XPS
Converter.convertHTML(document, options, "document-output.xps");
// Convert HTML to XPS using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-xps/
// Invoke the convertHTML() method to convert HTML to XPS
Converter.convertHTML("<h1>Convert HTML to XPS!</h1>", ".", new XpsSaveOptions(), "convert-with-single-line.xps");
// Convert HTML to XPS with custom settings using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-xps/
// Prepare HTML code and save it to a file
String code = "<h1> XpsSaveOptions Class</h1>\r\n" +
"<p>Using XpsSaveOptions Class, you can programmatically " +
"apply a wide range of conversion parameters " +
"such as BackgroundColor, PageSetup, etc.</p>";
FileHelper.writeAllText("save-options.html", code);
// Initialize an HTML document from the html file
HTMLDocument document = new HTMLDocument("save-options.html");
// Set up the page-size, margins and change the background color to AntiqueWhite
XpsSaveOptions options = new XpsSaveOptions();
options.setBackgroundColor(Color.getAntiqueWhite());
options.getPageSetup().setAnyPage(new Page(new Size(Length.fromInches(4.9f), Length.fromInches(3.5f)), new Margin(30, 20, 10, 10)));
// Convert HTML to XPS
Converter.convertHTML(document, options, "save-options-output.xps");
// Convert inline HTML elements to Markdown using Java
// Learn more: https://docs.aspose.com/html/java/convert-html-to-markdown/
// Prepare HTML code and save it to a file
String code = "text<div markdown='inline'><code>text</code></div>";
FileHelper.writeAllText("inline.html", code);
// Call convertHTML() method to convert HTML to Markdown
Converter.convertHTML("inline.html", new MarkdownSaveOptions(), "inline-html.md");
// Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>
// Convert Markdown to HTML using Java
// Learn more: https://docs.aspose.com/html/java/convert-markdown-to-html/
// Convert Markdown to HTML
Converter.convertMarkdown("nature.md", "nature-output.html");
// Convert Markdown to PDF using Java
// Learn more: https://docs.aspose.com/html/java/convert-markdown-to-html/
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("nature.md");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert the HTML document to PDF file format
Converter.convertHTML(document, options, "nature-output.pdf");
// Convert Markdown to HTML in Java
// Prepare a simple Markdown example
String code = "### Hello, World! \n" +
"[visit applications](https://products.aspose.app/html/family)";
// Create a Markdown file
FileHelper.writeAllText("document.md", code);
// Convert Markdown to HTML
Converter.convertMarkdown("document.md", "document-output.html");
// Convert Markdown to JPG in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-markdown-to-image/
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("nature.md");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setHorizontalResolution(Resolution.to_Resolution(200));
options.setVerticalResolution(Resolution.to_Resolution(200));
options.setBackgroundColor(Color.getAliceBlue());
options.getPageSetup().setAnyPage(new Page(new Size(600, 950), new Margin(30, 20, 10, 10)));
// Convert HTML to JPG
Converter.convertHTML(document, options, "nature-options.jpg");
// Convert Markdown to PDF using Aspose.HTML for Java
// Learn more: https://docs.aspose.com/html/java/convert-markdown-to-pdf/
// Prepare a simple Markdown example
String code = "### Hello, World! \n\n" +
"[visit applications](https://products.aspose.app/html/applications)";
// Create a Markdown file
FileHelper.writeAllText("document.md", code);
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("document.md");
// Convert the HTML document to PDF file format
Converter.convertHTML(document, new PdfSaveOptions(), "document-output.pdf");
// Convert Markdown to PDF in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-markdown-to-pdf/
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("nature.md");
// Initialize PdfSaveOptions. Set up the resolutions, JpegQuality and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions();
options.setHorizontalResolution(Resolution.to_Resolution(200));
options.setVerticalResolution(Resolution.to_Resolution(200));
options.setBackgroundColor(Color.getAliceBlue());
options.setJpegQuality(100);
// Convert the HTML document to PDF file format
Converter.convertHTML(document, options, "nature-output.pdf");
// Convert Markdown to PNG using Java
// Learn more: https://docs.aspose.com/html/java/convert-markdown-to-image/
// Prepare a simple Markdown example
String code = "### Hello, World\n\n" +
"[visit applications](https://products.aspose.app/html/family)";
// Create a Markdown file
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.md")) {
fileWriter.write(code);
}
// Convert Markdown to HTML document
HTMLDocument document = Converter.convertMarkdown("document.md");
// Convert HTML document to PNG image file format
Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Png), "output_md.png");
// Convert MHTML to JPG in Java
// Learn more: https://docs.aspose.com/html/java/convert-mhtml-to-jpg/
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Call the convertMHTML() method to convert MHTML to JPG
Converter.convertMHTML(fileInputStream, options, "sample-output.jpg");
// Convert MHTML to JPG in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-mhtml-to-jpg/
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize the ImageSaveOptions with a custom page-size and background color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
options.setBackgroundColor(Color.getGreen());
options.getPageSetup().setAnyPage(new Page());
options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(1000), Length.fromPixels(500)));
// Call the convertMHTML() method to convert MHTML to JPG
Converter.convertMHTML(fileInputStream, options, "sample-options.jpg");
// Convert MHTML to JPG using Java
// Learn more: https://docs.aspose.com/html/java/convert-mhtml-to-jpg/
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Invoke the convertMHTML() method to convert MHTML to JPG
Converter.convertMHTML(fileInputStream, new ImageSaveOptions(ImageFormat.Jpeg), "convert-by-few-lines.jpg");
// Convert MHTML to PDF using Java
// Learn more: https://docs.aspose.com/html/java/convert-mhtml-to-pdf/
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of the PdfSaveOptions class
PdfSaveOptions options = new PdfSaveOptions();
// Call the convertMHTML() method to convert MHTML to PDF
Converter.convertMHTML(fileInputStream, options, "sample-output.pdf");
// Convert MHTML to PDF in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-mhtml-to-pdf/
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions();
options.setBackgroundColor(Color.getAliceBlue());
options.getPageSetup().setAnyPage(new Page());
options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(3000), Length.fromPixels(1000)));
// Call the convertMHTML() method to convert MHTML to PDF
Converter.convertMHTML(fileInputStream, options, "sample-options.pdf");
// Convert MHTML to PDF using Java
// Learn more: https://docs.aspose.com/html/java/convert-mhtml-to-pdf/
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Invoke the convertMHTML() method to convert MHTML to PDF
Converter.convertMHTML(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");
// Convert MHTML to PNG using Java
// Learn more: https://docs.aspose.com/html/java/convert-mhtml-to-png/
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Call the convertMHTML() method to convert MHTML to PNG
Converter.convertMHTML(fileInputStream, options, "sample-output.png");
// Convert MHTML to PNG in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-mhtml-to-png/
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize the ImageSaveOptions with a custom page-size and background-color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
PageSetup pageSetup = new PageSetup();
Page anyPage = new Page();
anyPage.setSize(
new Size(
Length.fromPixels(3000),
Length.fromPixels(1000)
)
);
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(Color.getGreen());
// Call the convertMHTML() method to convert MHTML to PNG
Converter.convertMHTML(fileInputStream, options, "sample-options.png");
// Convert SVG to GIF in Java
// Learn more: https://docs.aspose.com/html/java/svg-converter/
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the SVG file
SVGDocument document = new SVGDocument("document.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Convert SVG to GIF
Converter.convertSVG(document, options, "output.gif");
// Convert SVG to JPG using Java
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-jpg/
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the SVG file
SVGDocument document = new SVGDocument("document.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Convert SVG to JPG
Converter.convertSVG(document, options, "output.jpg");
// Convert SVG to JPG in a few lines using Java
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-jpg/
// Prepare SVG code
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
// Invoke the convertSVG() method to convert SVG to image
Converter.convertSVG(code, ".", new ImageSaveOptions(ImageFormat.Jpeg), "output.jpg");
// Convert SVG to JPG in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-jpg/
// Prepare SVG code and save it to a file
String code =
"<svg width='450' height='450' xmlns='http://www.w3.org/2000/svg'>" +
" <g fill='RoyalBlue'>" +
" <rect x='100' y='100' rx='25' ry='25' width='200' height='56' />" +
" <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(90 200 128)' />" +
" <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(-45 200 128)' />" +
" <rect x='100' y='100' rx='25' ry='25' width='200' height='56' transform ='rotate(45 200 128)' />" +
" </g>" +
" <circle cx='200' cy='128' r='28' stroke='pink' stroke-width='50' stroke-dasharray='3 13' fill='Orange' />" +
" <circle cx='200' cy='128' r='5' />" +
"</svg>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("flower.svg")) {
fileWriter.write(code);
}
// Initialize ImageSaveOptions and set up smoothing mode, page size, and background color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
PageSetup pageSetup = new PageSetup();
options.setSmoothingMode(SmoothingMode.HighQuality);
Page anyPage = new Page();
anyPage.setSize(new Size(Length.fromPixels(200), Length.fromPixels(200)));
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(Color.getAliceBlue());
// Call the convertSVG() method to convert the "flower.svg" file to a JPEG image
Converter.convertSVG("flower.svg", options, "flower.jpg");
// Convert SVG to PDF using Java
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-pdf/
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the svg file
SVGDocument document = new SVGDocument("document.svg");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert SVG to PDF
Converter.convertSVG(document, options, "output.pdf");
// Convert SVG to PDF in a few lines using Java
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-pdf/
// Prepare SVG code
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
// Call the convertSVG() method to convert SVG to PDF
Converter.convertSVG(code, ".", new PdfSaveOptions(), "output.pdf");
// Convert SVG to PDF in Java with custom page settings
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-pdf/
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Set A5 as a page-size and change the background color to green
PdfSaveOptions options = new PdfSaveOptions();
PageSetup pageSetup = new PageSetup();
Page anyPage = new Page();
anyPage.setSize(new Size(Length.fromInches(8.3f), Length.fromInches(5.8f)));
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(Color.getGreen());
// Convert SVG to PDF
Converter.convertSVG("document.svg", options, "output.pdf");
// Convert SVG to PNG using Java
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-png/
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the SVG file
SVGDocument document = new SVGDocument("document.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Convert SVG to PNG
Converter.convertSVG(document, options, "output.png");
// Convert SVG to PNG in one line using Java
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-png/
// Invoke the convertSVG() method for SVG to PNG conversion
Converter.convertSVG("shapes.svg", new ImageSaveOptions(ImageFormat.Png), "convert-with-single-line.png");
// Convert SVG to PNG in Java with custom settings
// Learn more: https://docs.aspose.com/html/java/convert-svg-to-png/
// Create an instance of the ImageSaveOptions class. Set up the SmoothingMode, resolutions, and background color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
options.setHorizontalResolution(Resolution.to_Resolution(200));
options.setVerticalResolution(Resolution.to_Resolution(200));
options.setBackgroundColor(Color.getAliceBlue());
options.setSmoothingMode(SmoothingMode.HighQuality);
// Initialize an SVG document from a file
final SVGDocument document = new SVGDocument("flower1.svg");
// Convert SVG to PNG
Converter.convertSVG(document, options, "flower-options.png");
// Render HTML to PDF and adjust to the widest page with Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code
String code = " <style>\n" +
" div {\n" +
" page - break -after:always;\n" +
" }\n" +
" </style >\n" +
" <div style = 'border: 1px solid red; width: 400px' > First Page</div >\n" +
" <div style = 'border: 1px solid red; width: 600px' > Second Page</div >\n";
// Initialize an HTML document from HTML code
HTMLDocument document = new HTMLDocument(code, ".");
// Create an instance of the PdfRenderingOptions class and set a custom page-size
PdfRenderingOptions options = new PdfRenderingOptions();
options.getPageSetup().setAnyPage(new Page(new Size(500, 200)));
// Enable auto-adjusting for the page size
options.getPageSetup().setAdjustToWidestPage(true);
// Create an instance of the PdfDevice class and specify options and output file
PdfDevice device = new PdfDevice(options, "output.pdf");
// Render HTML to PDF
document.renderTo(device);
// Render HTML to PDF using Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code
String code = "<span>Hello, World!!</span>";
// Initialize an HTML document from HTML code
HTMLDocument document = new HTMLDocument(code, ".");
// Create an instance of the PdfDevice class and specify the output file to render
PdfDevice device = new PdfDevice("output.pdf");
// Render HTML to PDF
document.renderTo(device);
// Render HTML to PDF with password protection using Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code
String code = "<div>Hello, World!!</div>";
// Initialize an HTML document from the HTML code
HTMLDocument document = new HTMLDocument(code, ".");
// Create the instance of the PdfRenderingOptions class
PdfRenderingOptions options = new PdfRenderingOptions();
// Set file permissions
options.setEncryption(
new PdfEncryptionInfo(
"user_pwd",
"owner_pwd",
PdfPermissions.PrintDocument,
PdfEncryptionAlgorithm.RC4_128
)
);
// Create a PDF Device and specify options and output file
PdfDevice device = new PdfDevice(options, "output.pdf");
// Render HTML to PDF
document.renderTo(device);
// Render HTML to PDF in Java with custom page size
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code
String code = "<span>Hello, World!!</span>";
// Initialize a HTML document from the HTML code
HTMLDocument document = new HTMLDocument(code, ".");
// Create an instance of PdfRenderingOptions and set a custom page-size
PdfRenderingOptions options = new PdfRenderingOptions();
PageSetup pageSetup = new PageSetup();
Page anyPage = new Page();
anyPage.setSize(
new Size(
Length.fromInches(5),
Length.fromInches(2)
)
);
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
// Create a PDF Device and specify options and output file
PdfDevice device = new PdfDevice(options, "output.pdf");
// Render HTML to PDF
document.renderTo(device);
// Render HTML to PDF with custom resolution using Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code and save it to a file
String code = "< style >\n" +
" p\n" +
" {\n" +
" background:\n" +
" blue;\n" +
" }\n" +
" @media(min - resolution:300dpi)\n" +
" {\n" +
" p\n" +
" {\n" +
" /* high resolution screen color */\n" +
" background:\n" +
" green\n" +
" }\n" +
" }\n" +
" </style >\n" +
" <p > Hello World !! </p >\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Create an instance of the HTMLDocument class
HTMLDocument document = new HTMLDocument("document.html");
// Create options for low-resolution screens
PdfRenderingOptions options = new PdfRenderingOptions();
options.setHorizontalResolution(Resolution.to_Resolution(50d));
options.setVerticalResolution(Resolution.to_Resolution(50d));
// Create an instance of the PdfDevice
PdfDevice device = new PdfDevice(options, "output_resolution_50.pdf");
// Render HTML to PDF
document.renderTo(device);
// Create options for high-resolution screens
options = new PdfRenderingOptions();
options.setHorizontalResolution(Resolution.to_Resolution(300d));
options.setVerticalResolution(Resolution.to_Resolution(300d));
// Create an instance of PDF device
device = new PdfDevice(options, "output_resolution_300.pdf");
// Render HTML to PDF
document.renderTo(device);
// Render HTML to PDF with timeout settings using Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code
String code = "< script >\n" +
" var count = 0;\n" +
" setInterval(function()\n" +
" {\n" +
" var element = document.createElement('div');\n" +
" var message = (++count) + '. ' + 'Hello, World!!';\n" +
" var text = document.createTextNode(message);\n" +
" element.appendChild(text);\n" +
" document.body.appendChild(element);\n" +
" },1000);\n" +
"</script >\n";
// Initialize an HTML document based on prepared HTML code
HTMLDocument document = new HTMLDocument(code, ".");
// Create an instance of HTML Renderer
HtmlRenderer renderer = new HtmlRenderer();
// Create an instance of the PdfDevice class
PdfDevice device = new PdfDevice("output.pdf");
// Render HTML to PDF
renderer.render(device, 5, document);
// Render HTML to PDF with custom background color using Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code and save it to a file
String code = "<p>Hello, World!!</p>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Create an instance of the HTMLDocument class
HTMLDocument document = new HTMLDocument("document.html");
// Initialize options with 'cyan' as a background-color
PdfRenderingOptions options = new PdfRenderingOptions();
options.setBackgroundColor(Color.getCyan());
// Create an instance of the PdfDevice class
PdfDevice device = new PdfDevice(options, "output.pdf");
// Render HTML to PDF
document.renderTo(device);
// Render HTML to JPG with custom resolution and antialiasing settings with Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code
String code = "<div>Hello, World!!</div>";
// Initialize an instance of the HTMLDocument class based on prepared code
HTMLDocument document = new HTMLDocument(code, ".");
// Create an instance of the ImageRenderingOptions class
ImageRenderingOptions options = new ImageRenderingOptions();
options.setFormat(ImageFormat.Jpeg);
// Disable smoothing mode
options.setSmoothingMode(SmoothingMode.None);
// Set the image resolution as 75 dpi
options.setVerticalResolution(Resolution.fromDotsPerInch(75));
options.setHorizontalResolution(Resolution.fromDotsPerInch(75));
// Create an instance of the ImageDevice class
ImageDevice device = new ImageDevice(options, "output.jpg");
// Render HTML to Image
document.renderTo(device);
// Render HTML to PDF with custom MediaType settings with Java
// Learn more: https://docs.aspose.com/html/java/fine-tuning-converters/
// Prepare HTML code
String code = "<span>Hello, World!!</span>";
// Initialize an HTML document from the HTML code
HTMLDocument document = new HTMLDocument(code, ".");
// Create an instance of the PdfRenderingOptions class
PdfRenderingOptions options = new PdfRenderingOptions();
// Set the 'screen' media-type
options.getCss().setMediaType(MediaType.Screen);
// Create a PDF Device and specify options and output file
PdfDevice device = new PdfDevice(options, "output.pdf");
// Render HTML to PDF
document.renderTo(device);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment