Skip to content

Commit e67615e

Browse files
Rename openpdf-html config file til openpdf.conf. (#1360)
Remove GenShinGothic-Normal.ttf in order to reduce overall jar file size. Add some tests for openpdf-html.
1 parent f4ac413 commit e67615e

File tree

12 files changed

+177
-180
lines changed

12 files changed

+177
-180
lines changed

openpdf-html/src/main/resources/resources/conf/xhtmlrenderer.conf renamed to openpdf-html/src/main/resources/resources/conf/openpdf.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Configuration file for XHTMLRenderer core
1+
# Configuration file for openpdf-html
22
#
33
# Follows formatting specified in JavaDoc for java.util.Properties
44
# key = value
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.openpdf.pdf;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
import java.io.OutputStream;
8+
import java.net.URL;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
public class HtmlAndImageToPdf {
13+
14+
@Test
15+
void generatePdfWithImage() throws Exception {
16+
// Load image from resources
17+
URL imageUrl = getClass().getClassLoader().getResource("norway.png");
18+
assertNotNull(imageUrl, "Image resource 'norway.png' not found");
19+
String imageUri = imageUrl.toExternalForm();
20+
21+
// HTML content
22+
String html = """
23+
<html>
24+
<head>
25+
<style>
26+
@page {
27+
size: A4;
28+
margin: 2cm;
29+
}
30+
body {
31+
font-family: Arial, sans-serif;
32+
font-size: 12pt;
33+
color: #333;
34+
}
35+
h1 {
36+
color: navy;
37+
}
38+
img.flag {
39+
margin-top: 20px;
40+
width: 80px;
41+
height: auto;
42+
border: 1px solid #ccc;
43+
}
44+
</style>
45+
</head>
46+
<body>
47+
<h1>Hello, World!</h1>
48+
<p>This PDF includes the Norwegian flag:</p>
49+
<img class="flag" src="%s" alt="Norwegian Flag"/>
50+
</body>
51+
</html>
52+
""".formatted(imageUri);
53+
54+
// Save PDF to target/test-output/
55+
File outputDir = new File("target/test-output");
56+
outputDir.mkdirs();
57+
File pdfFile = new File(outputDir, "norway-flag.pdf");
58+
59+
try (OutputStream outputStream = new FileOutputStream(pdfFile)) {
60+
ITextRenderer renderer = new ITextRenderer();
61+
renderer.setDocumentFromString(html);
62+
renderer.layout();
63+
renderer.createPDF(outputStream);
64+
}
65+
66+
System.out.println("PDF created at: " + pdfFile.getAbsolutePath());
67+
68+
assertTrue(pdfFile.exists(), "PDF file should exist");
69+
assertTrue(pdfFile.length() > 1000, "PDF file should not be empty");
70+
}
71+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package org.openpdf.pdf;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.io.TempDir;
5+
6+
import java.io.File;
7+
import java.io.FileOutputStream;
8+
import java.io.OutputStream;
9+
import java.nio.file.Path;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
public class HtmlToPdfTest {
14+
15+
@Test
16+
void generateHelloWorldPdf(@TempDir Path tempDir) throws Exception {
17+
String html = """
18+
<html>
19+
<head>
20+
<style>
21+
@page {
22+
size: A4;
23+
margin: 2cm;
24+
}
25+
body {
26+
font-family: Arial, sans-serif;
27+
line-height: 1.5;
28+
font-size: 12pt;
29+
color: #333;
30+
}
31+
h1 {
32+
color: navy;
33+
border-bottom: 1px solid #ccc;
34+
padding-bottom: 5px;
35+
}
36+
table {
37+
width: 100%;
38+
border-collapse: collapse;
39+
margin-top: 20px;
40+
}
41+
th, td {
42+
border: 1px solid #aaa;
43+
padding: 8px;
44+
text-align: left;
45+
}
46+
th {
47+
background-color: #f0f0f0;
48+
}
49+
footer {
50+
font-size: 10pt;
51+
text-align: center;
52+
margin-top: 50px;
53+
color: #777;
54+
}
55+
</style>
56+
</head>
57+
<body>
58+
<h1>Hello, World!</h1>
59+
<p>This PDF was generated using <b>openpdf-html</b>, a modern HTML to PDF library built on OpenPDF and Flying Saucer.</p>
60+
<p>OpenPDF-html is possibly the best HTML-to-PDF library in the world.</p>
61+
<p>Here is a table:</p>
62+
<table>
63+
<thead>
64+
<tr>
65+
<th>Item</th>
66+
<th>Quantity</th>
67+
<th>Price</th>
68+
</tr>
69+
</thead>
70+
<tbody>
71+
<tr>
72+
<td>Apples</td>
73+
<td>3</td>
74+
<td>€2.40</td>
75+
</tr>
76+
<tr>
77+
<td>Bananas</td>
78+
<td>5</td>
79+
<td>€3.00</td>
80+
</tr>
81+
<tr>
82+
<td>Oranges</td>
83+
<td>2</td>
84+
<td>€1.60</td>
85+
</tr>
86+
</tbody>
87+
</table>
88+
<footer>Page rendered with ♥ by OpenPDF-html.</footer>
89+
</body>
90+
</html>
91+
""";
92+
93+
File pdfFile = tempDir.resolve("hello.pdf").toFile();
94+
95+
try (OutputStream outputStream = new FileOutputStream(pdfFile)) {
96+
ITextRenderer renderer = new ITextRenderer();
97+
renderer.setDocumentFromString(html);
98+
renderer.layout();
99+
renderer.createPDF(outputStream);
100+
}
101+
102+
assertTrue(pdfFile.exists(), "PDF file should exist");
103+
assertTrue(pdfFile.length() > 1000, "PDF file should not be empty");
104+
}
105+
}
1.18 KB
Loading

openpdf/src/test/java/com/lowagie/text/pdf/FontDetailsTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ void convertToBytesBaseFontNullShouldThrowNpe() {
1515
assertThatNullPointerException().isThrownBy(() -> new FontDetails(null, null, null));
1616
}
1717

18-
@Test
19-
void convertToBytesShouldExerciseSomeCode() throws IOException {
20-
String filename = "src/test/resources/fonts/jp/GenShinGothic-Normal.ttf";
21-
BaseFont baseFont = BaseFont.createFont(filename, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
22-
FontDetails fontDetails = new FontDetails(null, null, baseFont);
23-
TextRenderingOptions options = new TextRenderingOptions();
24-
byte[] bytes = fontDetails.convertToBytes("hällö wörld", options);
25-
assertThat(bytes).hasSize(22);
26-
assertThat(fontDetails.isSubset()).isTrue();
27-
}
28-
2918
@Test
3019
void convertToBytesAwesomeShouldExerciseSomeCode() throws IOException {
3120
String fileName = "src/test/resources/fonts/font-awesome/fa-v4compatibility.ttf";

openpdf/src/test/java/com/lowagie/text/pdf/PdfFormFlatteningTest.java

Lines changed: 0 additions & 168 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)