Skip to content

Commit eba3640

Browse files
authored
Merge pull request #22 from Visual-Regression-Tracker/pdf_compare
It does not save png images to file when comparing pdf files.
2 parents a0568a9 + c45c0f6 commit eba3640

File tree

3 files changed

+64
-45
lines changed

3 files changed

+64
-45
lines changed

standalone-from-folder-maven/README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# Compare image and pdf files from a folder
2-
This can be used to compare existing png images in a folder. If there are pdf files in the folder, they will be converted to png files and then will be compared in VRT.
2+
This can be used to compare existing PNG and PDF files in a folder. Each page of PDF file is converted to ```BufferedImage``` and then compared in VRT.
33

4-
#### Use it as a standalone executable jar file with option to pass parameters from command line
5-
- Ensure in the main method, you are using the only line ```option1_RunWithParameters```
4+
#### Use it as a standalone executable jar file passing parameters from command line
65
- Make a fat jar by giving below maven command
76

87
```mvn clean compile assembly:single```
9-
- Then you can use run the jar file in command line as
8+
- Then run the jar file in command line as
109

11-
```java -jar vrt-standalone-client-1.0.1.jar http://localhost:4200 CDJ3HHD5MY45G0PK5M3PBB6NBGC9 c936bfa4-50b3-40c4-b177-94efbeabfbcf null main```
12-
- You can go through help
10+
```java -jar vrt-standalone-client-1.0.1.jar http://localhost:4200 CDJ3HHD5MY45G0PK5M3PBB6NBGC9 c936bfa4-50b3-40c4-b177-94efbeabfbcf null main <folder_name>```
11+
- You can see help by running without parameters like below.
1312

1413
```java -jar vrt-standalone-client-1.0.1.jar```
1514

standalone-from-folder-maven/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<dependency>
5050
<groupId>org.apache.pdfbox</groupId>
5151
<artifactId>pdfbox</artifactId>
52-
<version>2.0.25</version>
52+
<version>2.0.27</version>
5353
</dependency>
5454
<dependency>
5555
<groupId>com.github.Visual-Regression-Tracker</groupId>

standalone-from-folder-maven/src/main/java/CompareFromFile.java

+58-38
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import io.visual_regression_tracker.sdk_java.response.BuildResponse;
33
import org.apache.commons.io.FilenameUtils;
44
import org.apache.pdfbox.pdmodel.PDDocument;
5-
import org.apache.pdfbox.rendering.ImageType;
65
import org.apache.pdfbox.rendering.PDFRenderer;
76

87
import javax.imageio.ImageIO;
98
import java.awt.image.BufferedImage;
9+
import java.io.ByteArrayOutputStream;
1010
import java.io.File;
1111
import java.io.FileInputStream;
1212
import java.io.IOException;
@@ -90,50 +90,35 @@ private static void runVRT(String apiUrl, String apiKey, String project, String
9090
System.out.println("ERROR: Ensure path " + filePath + " exists.");
9191
System.exit(1);
9292
}
93-
//If the folder has pdf files, they will be converted to png to be able to compare.
94-
File[] fileList = file.listFiles();
95-
for (File eachFile : fileList) {
96-
if (FilenameUtils.getExtension(eachFile.getName()).equalsIgnoreCase("pdf")) {
97-
PDDocument pdDocument = PDDocument.load(eachFile);
98-
PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
99-
for (int pageNumber = 0; pageNumber < pdDocument.getNumberOfPages(); ++pageNumber) {
100-
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(pageNumber, 300, ImageType.RGB);
101-
ImageIO.write(bufferedImage, "PNG", new File(eachFile.getPath().replace(".pdf", "_Page") + (pageNumber + 1) + ".png"));
102-
}
103-
pdDocument.close();
104-
}
105-
}
10693
visualRegressionTracker.start();
10794
//Use the default options builder or custom options builder.
10895
TestRunOptions.TestRunOptionsBuilder testRunOptionsBuilder = TestRunOptions.builder();
10996
//TestRunOptions.TestRunOptionsBuilder testRunOptionsBuilder = getTestRunOptionsBuilder();
110-
fileList = file.listFiles();
97+
File[] fileList = file.listFiles();
11198
for (File eachFile : fileList) {
112-
String[] imageExtensions = {"png"};
99+
String[] imageExtensions = {"png", "pdf"};
113100
if (Arrays.asList(imageExtensions).contains(FilenameUtils.getExtension(eachFile.getName()))) {
114-
FileInputStream fileInputStreamReader = new FileInputStream(eachFile);
115-
byte[] bytes = new byte[(int) eachFile.length()];
116-
fileInputStreamReader.read(bytes);
117-
String encodedBase64 = new String(Base64.getEncoder().encode(bytes));
118-
fileInputStreamReader.close();
119-
try {
120-
int countOfProcessed = results.get("imageProcessedCount") == null ? 1 : Integer.parseInt(results.get("imageProcessedCount").toString()) + 1;
121-
results.put("imageProcessedCount", countOfProcessed);
122-
TestRunResult testRunResult = visualRegressionTracker.track(eachFile.getName(), encodedBase64, testRunOptionsBuilder.build());
123-
String result = String.valueOf(testRunResult.getTestRunResponse().getStatus());
124-
if (!result.equals("OK") && !result.equals("autoApproved"))
125-
results.put("allPassed", false);
126-
int countOfResult = results.get(result) == null ? 1 : Integer.parseInt(results.get(result).toString()) + 1;
127-
results.put(result, countOfResult);
128-
Object imageVerifiedCount = results.get("imageVerifiedCount");
129-
int countOfFilesVerified = imageVerifiedCount == null ? 1 : Integer.parseInt(imageVerifiedCount.toString()) + 1;
130-
results.put("imageVerifiedCount", countOfFilesVerified);
131-
} catch (Exception ex) {
132-
if (!(ex.getMessage().contains("No baseline:") || ex.getMessage().contains("Difference found"))) {
133-
throw ex;
134-
} else {
135-
results.put("allPassed", false);
101+
//If the folder has pdf files, they will be converted to png to be able to compare.
102+
if (FilenameUtils.getExtension(eachFile.getName()).equalsIgnoreCase("pdf")) {
103+
List<BufferedImage> bufferedImageList = getPDFPagesAsImages(eachFile);
104+
for (int index = 0; index < bufferedImageList.size(); index++) {
105+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
106+
BufferedImage bufferedImage = bufferedImageList.get(index);
107+
ImageIO.write(bufferedImage, "PNG", byteArrayOutputStream);
108+
byte[] bytes = byteArrayOutputStream.toByteArray();
109+
String encodedBase64 = new String(Base64.getEncoder().encode(bytes));
110+
String screenshotName = eachFile.getName().replace(".pdf", "_Page") + (index + 1) + ".png";
111+
runComparison(visualRegressionTracker, results, testRunOptionsBuilder, screenshotName, encodedBase64);
112+
bufferedImage.getGraphics().dispose();
113+
bufferedImage.flush();
136114
}
115+
} else {
116+
FileInputStream fileInputStreamReader = new FileInputStream(eachFile);
117+
byte[] bytes = new byte[(int) eachFile.length()];
118+
fileInputStreamReader.read(bytes);
119+
String encodedBase64 = new String(Base64.getEncoder().encode(bytes));
120+
fileInputStreamReader.close();
121+
runComparison(visualRegressionTracker, results, testRunOptionsBuilder, eachFile.getName(), encodedBase64);
137122
}
138123
}
139124
}
@@ -153,6 +138,41 @@ private static void runVRT(String apiUrl, String apiKey, String project, String
153138
System.out.println(BOLD_TEXT + ANSI_GREEN + results + ANSI_RESET);
154139
}
155140

141+
private static void runComparison(VisualRegressionTracker visualRegressionTracker, Map<String, Object> results, TestRunOptions.TestRunOptionsBuilder testRunOptionsBuilder, String screenshotName, String encodedBase64) throws IOException, InterruptedException {
142+
try {
143+
int countOfProcessed = results.get("imageProcessedCount") == null ? 1 : Integer.parseInt(results.get("imageProcessedCount").toString()) + 1;
144+
results.put("imageProcessedCount", countOfProcessed);
145+
TestRunResult testRunResult = visualRegressionTracker.track(screenshotName, encodedBase64, testRunOptionsBuilder.build());
146+
String result = String.valueOf(testRunResult.getTestRunResponse().getStatus());
147+
if (!result.equals("OK") && !result.equals("autoApproved"))
148+
results.put("allPassed", false);
149+
int countOfResult = results.get(result) == null ? 1 : Integer.parseInt(results.get(result).toString()) + 1;
150+
results.put(result, countOfResult);
151+
Object imageVerifiedCount = results.get("imageVerifiedCount");
152+
int countOfFilesVerified = imageVerifiedCount == null ? 1 : Integer.parseInt(imageVerifiedCount.toString()) + 1;
153+
results.put("imageVerifiedCount", countOfFilesVerified);
154+
} catch (Exception ex) {
155+
if (!(ex.getMessage().contains("No baseline:") || ex.getMessage().contains("Difference found"))) {
156+
throw ex;
157+
} else {
158+
results.put("allPassed", false);
159+
}
160+
}
161+
}
162+
163+
private static List<BufferedImage> getPDFPagesAsImages(File eachFile) throws Exception {
164+
List<BufferedImage> bufferedImageList = new ArrayList<>();
165+
try (PDDocument pdDocument = PDDocument.load(eachFile)) {
166+
PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
167+
for (int pageNumber = 0; pageNumber < pdDocument.getNumberOfPages(); ++pageNumber) {
168+
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(pageNumber, 300);
169+
bufferedImageList.add(bufferedImage);
170+
//ImageIO.write(bufferedImage, "PNG", new File(eachFile.getPath().replace(".pdf", "_Page") + (pageNumber + 1) + ".png"));
171+
}
172+
}
173+
return bufferedImageList;
174+
}
175+
156176
private static TestRunOptions.TestRunOptionsBuilder getTestRunOptionsBuilder() {
157177
//Use only the options you need in below line.
158178
String maskData_x = "0";

0 commit comments

Comments
 (0)