HTML to PDF Conversion with the Java API
Aspose.HTML for Java
converts HTML files, strings, and webpages to PDF with
Converter.convertHTML()
. Load the source into HTMLDocument, create PdfSaveOptions, and specify the destination path. The renderer processes CSS, fonts, images, and page layout before writing the PDF.
Generate a PDF from an HTML File
The example loads document.html, creates the default PDF save options, and writes the rendered document to output.pdf.
Convert HTML to PDF in Java
import com.aspose.html.HTMLDocument;
import com.aspose.html.converters.Converter;
import com.aspose.html.saving.PdfSaveOptions;
// Load the HTML document
try (HTMLDocument document = new HTMLDocument("document.html")) {
// Create the PDF save options
PdfSaveOptions options = new PdfSaveOptions();
// Convert HTML to PDF
Converter.convertHTML(document, options, "output.pdf");
}
Test HTML to PDF Conversion Online
Upload an HTML file or enter a webpage URL to review the PDF output before implementing the conversion in a Java application.
HTML to PDF API Workflow
- Add Aspose.HTML for Java to the project.
- Load the HTML source with
HTMLDocument. - Create a
PdfSaveOptionsinstance. - Pass the document, options, and output path to
Converter.convertHTML().
Configure PDF Pages and Protection
PdfSaveOptions controls page size and margins, background color, internal image resolution, JPEG quality, CSS processing, tagged PDF output, form-field behavior, and encryption. Page dimensions, print styles, and available fonts can change pagination, so test the output with the resources used in production.
See
Convert HTML to PDF in Java
and the
PdfSaveOptions API Reference
.
Install Aspose.HTML for Java
Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the
Aspose.HTML for Java releases
page, then reload or rebuild the project so the dependency is resolved.
Add the repository and dependency to the Maven project:
Maven – pom.xml
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html</artifactId>
<version>ASPOSE_HTML_VERSION</version>
</dependency>
</dependencies>Add the repository and dependency to the Gradle project:
Gradle – build.gradle
repositories {
maven {
url = uri('https://releases.aspose.com/java/repo/')
}
}
dependencies {
implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .
HTML to PDF Conversion FAQ
1. Can Java convert an HTML string or webpage URL to PDF?
Yes. Converter.convertHTML() provides overloads for supported HTML sources, including content with a base URI, while HTMLDocument can load files and URLs before conversion.
2. How are relative CSS, images, and fonts resolved during conversion?
Relative resources are resolved against the document URL or the base URI supplied with the HTML content. Use a correct base location and ensure the application can access the referenced resources.
3. Can I set PDF page size, margins, and encryption?
Yes. Configure page setup and encryption through PdfSaveOptions before calling Converter.convertHTML().
4. Can I convert HTML to PDF online for free?
Yes. Use the free online HTML to PDF converter for a manual conversion. Use Aspose.HTML for Java for automated or server-side workflows.
Related Java Conversions
Choose another output format for the same HTML source.