Skip to content

Commit

Permalink
verify the contents of generated PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Sep 21, 2023
1 parent ce1cdb3 commit 1f5882c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
7 changes: 7 additions & 0 deletions flying-saucer-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<itext.version>2.1.7</itext.version>
<slf4j.version>2.0.9</slf4j.version>
<assertj.version>3.24.2</assertj.version>
<pdftest.version>1.8.1</pdftest.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -58,6 +59,12 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>pdf-test</artifactId>
<version>${pdftest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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();

Expand All @@ -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() {
Expand Down

0 comments on commit 1f5882c

Please sign in to comment.