diff --git a/flying-saucer-examples/pom.xml b/flying-saucer-examples/pom.xml
index a036de30e..d5d13ab01 100644
--- a/flying-saucer-examples/pom.xml
+++ b/flying-saucer-examples/pom.xml
@@ -28,6 +28,7 @@
2.1.7
2.0.9
3.24.2
+ 1.8.1
@@ -58,6 +59,12 @@
${assertj.version}
test
+
+ com.codeborne
+ pdf-test
+ ${pdftest.version}
+ test
+
org.slf4j
slf4j-simple
diff --git a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderTest.java b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderTest.java
index 63276d7bf..fda1f282a 100644
--- a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderTest.java
+++ b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderTest.java
@@ -1,5 +1,6 @@
package org.xhtmlrenderer.pdf;
+import com.codeborne.pdftest.PDF;
import com.lowagie.text.DocumentException;
import org.junit.Test;
import org.slf4j.Logger;
@@ -14,9 +15,10 @@
import java.io.OutputStream;
import java.net.URL;
+import static com.codeborne.pdftest.assertj.Assertions.assertThat;
+import static java.lang.System.lineSeparator;
import static java.nio.file.Files.newOutputStream;
import static java.util.Objects.requireNonNull;
-import static org.assertj.core.api.Assertions.assertThat;
public class PDFRenderTest {
private static final Logger log = LoggerFactory.getLogger(PDFRenderTest.class);
@@ -25,33 +27,43 @@ public class PDFRenderTest {
public void testConvertSimpleHtmlToPdf() throws IOException, DocumentException {
URL source = requireNonNull(Thread.currentThread().getContextClassLoader().getResource("hello.html"));
File output = File.createTempFile("flying-saucer-" + getClass().getSimpleName(), ".hello.pdf");
- createPDF(source, output);
- log.info("Rendered {} to PDF: {}", source, output.toURI());
- assertThat(output).exists();
+ PDF pdf = generatePDF(source, output);
+ assertThat(pdf).containsExactText("Hello, world");
}
@Test
public void testConvertComplexHtmlToPdf() throws IOException, DocumentException {
URL source = requireNonNull(Thread.currentThread().getContextClassLoader().getResource("hamlet.xhtml"));
File output = File.createTempFile("flying-saucer-" + getClass().getSimpleName(), ".hamlet.pdf");
- createPDF(source, output);
- log.info("Rendered {} to PDF: {}", source, output.toURI());
- assertThat(output).exists();
+ PDF pdf = generatePDF(source, output);
+ assertThat(pdf).containsText(
+ "Previous Page", "Next Page",
+ "Hamlet",
+ "by William Shakespeare",
+ "Dramatis Personae",
+ "ACT I",
+ "Scene 5",
+ "ACT V",
+ "Dramatis Personae",
+ "Claudius, King of Denmark",
+ "THE END");
}
- private static void createPDF(URL url, File output) throws IOException, DocumentException {
+ private static PDF generatePDF(URL source, File output) throws IOException, DocumentException {
try (OutputStream os = newOutputStream(output.toPath())) {
ITextRenderer renderer = new ITextRenderer();
ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer.getOutputDevice());
callback.setSharedContext(renderer.getSharedContext());
renderer.getSharedContext().setUserAgentCallback(callback);
- Document doc = XMLResource.load(new InputSource(url.toString())).getDocument();
+ Document doc = XMLResource.load(new InputSource(source.toString())).getDocument();
- renderer.setDocument(doc, url.toString());
+ renderer.setDocument(doc, source.toString());
renderer.layout();
renderer.createPDF(os);
}
+ log.info("Rendered {}{} to PDF: {}", source, lineSeparator(), output.toURI());
+ return new PDF(output);
}
private static class ResourceLoaderUserAgent extends ITextUserAgent {
diff --git a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderToMultiplePagesTest.java b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderToMultiplePagesTest.java
index 2b7f55b86..32b78c427 100644
--- a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderToMultiplePagesTest.java
+++ b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderToMultiplePagesTest.java
@@ -1,12 +1,16 @@
package org.xhtmlrenderer.pdf;
+import com.codeborne.pdftest.PDF;
+import com.lowagie.text.DocumentException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
+import java.io.IOException;
import java.io.OutputStream;
+import static com.codeborne.pdftest.assertj.Assertions.assertThat;
import static java.nio.file.Files.newOutputStream;
/**
@@ -18,9 +22,15 @@ public class PDFRenderToMultiplePagesTest {
@Test
public void testGenerateSinglePdfFromMultipleInputDocuments() throws Exception {
File output = File.createTempFile("flying-saucer-" + getClass().getSimpleName(), ".pdf");
-
String[] inputs = createSimpleFakeDocuments();
+ generatePDF(inputs, output);
+ log.info("Sample file with {} documents rendered as PDF to {}", inputs.length, output.toURI());
+
+ PDF pdf = new PDF(output);
+ assertThat(pdf).containsText("Page1", "Page2", "Page3");
+ }
+ private static void generatePDF(String[] inputs, File output) throws IOException, DocumentException {
try (OutputStream os = newOutputStream(output.toPath())) {
ITextRenderer renderer = new ITextRenderer();
@@ -40,8 +50,6 @@ public void testGenerateSinglePdfFromMultipleInputDocuments() throws Exception {
// complete the PDF
renderer.finishPDF();
}
-
- log.info("Sample file with {} documents rendered as PDF to {}", inputs.length, output.toURI());
}
private static String[] createSimpleFakeDocuments() {