A JavaFX-based desktop application that converts HTML files to PDF format, preserving directory structure and styling.
- Convert individual HTML files or entire directories to PDF
- Preserves original directory structure in output
- Supports CSS styling and embedded resources
- Cross-platform: macOS, Windows, Linux
- Progress tracking for batch conversions
- JDK 21 (required for building and running)
- Gradle (included via wrapper)
- Docker (optional, for cross-platform builds and testing)
-
Clone the repository:
git clone <repository-url> cd html-to-pdf
-
Build the project:
./gradlew build
-
Run the application:
./gradlew run
- JavaFX 21: UI framework
- OpenHTMLtoPDF: HTML to PDF conversion engine
- JSoup: HTML parsing and cleaning
- Apache PDFBox: PDF generation backend
Build a native installer for your current operating system:
# macOS: Creates .dmg
# Windows: Creates .exe
# Linux: Creates .deb
./gradlew jpackage
Output location: build/jpackage/
The project supports building app-images (without installers) for different platforms:
./gradlew jpackage
Creates: build/jpackage/HTMLtoPDF.app/
./gradlew jpackage -PtargetOs=windows
Creates: build/jpackage/HTMLtoPDF/
(app-image only, no .exe)
Note: Creating Windows .exe
installers requires running on Windows due to WiX Toolset dependencies.
./gradlew jpackage -PtargetOs=linux
Creates: build/jpackage/HTMLtoPDF/
(app-image only on non-Linux, .deb on Linux)
The included Dockerfile allows you to test the built application in a containerized Linux environment with a virtual display.
-
Build the jpackage app-image:
./gradlew jpackage
-
Build the Docker image:
docker build -t htmltopdf-test .
-
Run the application in Docker:
docker run --rm htmltopdf-test
To view the application GUI running in Docker:
-
Run with VNC port exposed:
docker run -p 5900:5900 htmltopdf-test
-
Connect with a VNC client:
- Host:
localhost:5900
- You'll see the JavaFX application running
- Host:
- Launch the application
- Browse for HTML file or directory:
- Click "Browse File" to select a single HTML file
- Click "Browse Directory" to select a folder containing HTML files
- Browse for output directory:
- Choose where to save the generated PDFs
- Click "Convert":
- Progress bar shows conversion status
- PDFs are created with
_pdf
suffix in the output directory - Directory structure is preserved for batch conversions
"Unsupported class file major version 67"
- Ensure you're using JDK 21. Check with:
java -version
- Set
JAVA_HOME
to JDK 21 location
jpackage fails with platform-specific options
- The build script automatically adjusts options based on the target OS
- Cross-platform builds create app-images only (no installers)
Empty PDFs generated
- Check that HTML files are well-formed (valid markup)
- Ensure all
<link>
and<meta>
tags are properly closed - CSS files should be accessible relative to the HTML file
Missing fonts or styling
- External CSS files must be in the correct relative path
- Font files should be embedded or available on the system
Application won't start in container
- Verify the jpackage build completed successfully
- Check Docker logs:
docker logs <container-id>
- Ensure the virtual display is running (Xvfb)
- Add to
build.gradle.kts
dependencies section - Rebuild:
./gradlew build --refresh-dependencies
- FXML files are in
src/main/resources/com/ursineenterprises/utilities/htmltopdf/
- Controllers are in
src/main/java/com/ursineenterprises/utilities/htmltopdf/
- After changes, rebuild and run:
./gradlew run
Run with debug logging:
./gradlew run --debug
Or attach a debugger from your IDE to the run
task.
See LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
Built with: JavaFX 21 | Gradle | OpenHTMLtoPDF | PDFBox