Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Configuration file for XHTMLRenderer core
# Configuration file for openpdf-html
#
# Follows formatting specified in JavaDoc for java.util.Properties
# key = value
Expand Down
71 changes: 71 additions & 0 deletions openpdf-html/src/test/java/org/openpdf/pdf/HtmlAndImageToPdf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.openpdf.pdf;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;

import static org.junit.jupiter.api.Assertions.*;

public class HtmlAndImageToPdf {

Check notice on line 12 in openpdf-html/src/test/java/org/openpdf/pdf/HtmlAndImageToPdf.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

openpdf-html/src/test/java/org/openpdf/pdf/HtmlAndImageToPdf.java#L12

The class name 'HtmlAndImageToPdf' doesn't match '^Test.*$|^[A-Z][a-zA-Z0-9]*Test(s|Case)?$'

@Test
void generatePdfWithImage() throws Exception {
// Load image from resources
URL imageUrl = getClass().getClassLoader().getResource("norway.png");
assertNotNull(imageUrl, "Image resource 'norway.png' not found");
String imageUri = imageUrl.toExternalForm();

// HTML content
String html = """
<html>
<head>
<style>
@page {
size: A4;
margin: 2cm;
}
body {
font-family: Arial, sans-serif;
font-size: 12pt;
color: #333;
}
h1 {
color: navy;
}
img.flag {
margin-top: 20px;
width: 80px;
height: auto;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
<p>This PDF includes the Norwegian flag:</p>
<img class="flag" src="%s" alt="Norwegian Flag"/>
</body>
</html>
""".formatted(imageUri);

// Save PDF to target/test-output/
File outputDir = new File("target/test-output");
outputDir.mkdirs();
File pdfFile = new File(outputDir, "norway-flag.pdf");

try (OutputStream outputStream = new FileOutputStream(pdfFile)) {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(outputStream);
}

System.out.println("PDF created at: " + pdfFile.getAbsolutePath());

assertTrue(pdfFile.exists(), "PDF file should exist");
assertTrue(pdfFile.length() > 1000, "PDF file should not be empty");
}
}
105 changes: 105 additions & 0 deletions openpdf-html/src/test/java/org/openpdf/pdf/HtmlToPdfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.openpdf.pdf;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.*;

public class HtmlToPdfTest {

@Test
void generateHelloWorldPdf(@TempDir Path tempDir) throws Exception {
String html = """
<html>
<head>
<style>
@page {
size: A4;
margin: 2cm;
}
body {
font-family: Arial, sans-serif;
line-height: 1.5;
font-size: 12pt;
color: #333;
}
h1 {
color: navy;
border-bottom: 1px solid #ccc;
padding-bottom: 5px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #aaa;
padding: 8px;
text-align: left;
}
th {
background-color: #f0f0f0;
}
footer {
font-size: 10pt;
text-align: center;
margin-top: 50px;
color: #777;
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
<p>This PDF was generated using <b>openpdf-html</b>, a modern HTML to PDF library built on OpenPDF and Flying Saucer.</p>
<p>OpenPDF-html is possibly the best HTML-to-PDF library in the world.</p>
<p>Here is a table:</p>
<table>
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Apples</td>
<td>3</td>
<td>€2.40</td>
</tr>
<tr>
<td>Bananas</td>
<td>5</td>
<td>€3.00</td>
</tr>
<tr>
<td>Oranges</td>
<td>2</td>
<td>€1.60</td>
</tr>
</tbody>
</table>
<footer>Page rendered with ♥ by OpenPDF-html.</footer>
</body>
</html>
""";

File pdfFile = tempDir.resolve("hello.pdf").toFile();

try (OutputStream outputStream = new FileOutputStream(pdfFile)) {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(outputStream);
}

assertTrue(pdfFile.exists(), "PDF file should exist");
assertTrue(pdfFile.length() > 1000, "PDF file should not be empty");
}
}
Binary file added openpdf-html/src/test/resources/norway.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions openpdf/src/test/java/com/lowagie/text/pdf/FontDetailsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ void convertToBytesBaseFontNullShouldThrowNpe() {
assertThatNullPointerException().isThrownBy(() -> new FontDetails(null, null, null));
}

@Test
void convertToBytesShouldExerciseSomeCode() throws IOException {
String filename = "src/test/resources/fonts/jp/GenShinGothic-Normal.ttf";
BaseFont baseFont = BaseFont.createFont(filename, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontDetails fontDetails = new FontDetails(null, null, baseFont);
TextRenderingOptions options = new TextRenderingOptions();
byte[] bytes = fontDetails.convertToBytes("hällö wörld", options);
assertThat(bytes).hasSize(22);
assertThat(fontDetails.isSubset()).isTrue();
}

@Test
void convertToBytesAwesomeShouldExerciseSomeCode() throws IOException {
String fileName = "src/test/resources/fonts/font-awesome/fa-v4compatibility.ttf";
Expand Down
168 changes: 0 additions & 168 deletions openpdf/src/test/java/com/lowagie/text/pdf/PdfFormFlatteningTest.java

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading