Skip to content

Fix #167: Added README and improved styling for HTML-to-PDF example #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion itext-java-html-pdf/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
<classpathentry kind="var" path="M2_REPO/com/itextpdf/styled-xml-parser/7.1.9/styled-xml-parser-7.1.9.jar"/>
<classpathentry kind="var" path="M2_REPO/com/itextpdf/svg/7.1.9/svg-7.1.9.jar"/>
<classpathentry kind="var" path="M2_REPO/com/itextpdf/html2pdf/2.1.6/html2pdf-2.1.6.jar"/>
</classpath>
</classpath>
27 changes: 27 additions & 0 deletions itext-java-html-pdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# HTML to PDF with iText in Java

This example demonstrates how to convert HTML content to PDF using the **iText 7** library in Java.

## Features:

- Converts a **hardcoded HTML string** into PDF (`string-to-pdf.pdf`)
- Converts a **styled external HTML file** (`index.html + style.css`) into PDF (`index-to-pdf.pdf`)
- Uses `HtmlConverter` from `iText pdfHTML`

## Folder Contents

| File : Purpose |

| `App.java` | Java code for conversion |
| `index.html` | HTML template for PDF |
| `style.css` | Styling applied to the PDF |
| `java.png` | Image used inside the HTML (optional) |

## How to Run

1. Ensure you have **Java 8+** and **iText 7 libraries** (`html2pdf` JARs)
2. Compile and run the project:

```bash
javac -cp "itext7-core.jar" com/hmkcode/App.java
java -cp ".;itext7-core.jar" com.hmkcode.App
77 changes: 63 additions & 14 deletions itext-java-html-pdf/style.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,70 @@
/* ...existing code... */
body {
background: #f7f9fb;
font-family: 'Segoe UI', Arial, sans-serif;
margin: 0;
padding: 0;
}

h1 {
color:#ccc;
color: #2d3e50;
text-align: center;
margin-top: 32px;
margin-bottom: 8px;
letter-spacing: 1px;
}
table tr td{
text-align:center;
border:1px solid gray;
padding:4px;

p {
text-align: center;
font-size: 1.1em;
margin-bottom: 24px;
}
table tr th{
background-color:#84C7FD;
color:#fff;
width: 100px;

img {
display: block;
margin: 0 auto 24px auto;
max-width: 120px;
}
.itext{
color:#84C7FD;
font-weight:bold;

table {
margin: 0 auto;
border-collapse: collapse;
background: #fff;
box-shadow: 0 2px 12px rgba(0,0,0,0.07);
border-radius: 8px;
overflow: hidden;
min-width: 350px;
}
.description{
color:gray;

table tr th,
table tr td {
text-align: left;
border: none;
padding: 12px 20px;
}

table tr th {
background-color: #84C7FD;
color: #fff;
width: 120px;
font-weight: 600;
letter-spacing: 0.5px;
}

table tr:not(:last-child) td,
table tr:not(:last-child) th {
border-bottom: 1px solid #e3e3e3;
}

.itext {
color: #2196f3;
font-weight: bold;
font-size: 1.15em;
}

.description {
color: #888;
font-style: italic;
margin-left: 8px;
}
/* ...existing code... */