# Compare PDF Files in AIQ Web Tests

There are actions in the Test Designer that allows you to compare two PDF files. The report will detail all the changes, based on the rules that you specify, on a page by page basis between the two files.

For example, you can validate that all the elements on every page from the template PDF file appear on the same page in the dynamic PDF file with the same formatting.

These two PDF files in the comparison are:

- Template PDF file that contains the baseline information and formatting
- Dynamic PDF file that has been downloaded as part of a Test Designer script
 
You can compare certain elements in the two PDF files based on rules that you create. See [Rules Based Comparison](#Rules) for more information.

You can compare the complete contents of the two PDF files to each other. See [Comparing PDFs](#Comparin) for more information.

This feature requires to open the port 9013 in the controller to be accessed from test nodes and end-user computers. See [AIQ Required Ports for information on required ports.](https://docs.appvance.net/Content/AIQ/Getting_Started/AIQ_Required_Ports.htm)

## <a name="Rules"></a>Rules Based Comparison

You can create the rules by which the PDFs will be compared. Since you specify the rules during the comparison, you can create custom rules that can be used in very specific situations or rules that will be used for all of your comparisons.

Prerequisite: You must be familiar with writing regular expressions (regex) in order to author custom rules.

Considerations:

- Rules can be applied by page basis or globally for all pages.
 
- You can check for the umber of pages which means that you can verify that both PDF files contain same number of pages.
- You can choose whether or not to ignore blank pages,
- You can choose to report on blank pages. For example, if a blank page is found in a PDF file you can log a warning which will be highlighted in the report.
- You can verify the position of certain elements to see if they have the same XY coordinates in each document.
- You can verify font and font size of elements.
 
## Sample rules.txt File

This is an example of a rules file used for comparing two PDF files. This comparison checks for the existence of specified content as well as how it is formatted (font / bold / size).

<div class="codeSnippet" id="bkmrk-copy-%7B%C2%A0%C2%A0%C2%A0%C2%A0%22rules%22%3A-%7B"><div class="codeSnippetBody">```
{<br></br>    "rules": {<br></br>        "policy": {<br></br>            "format": "^([A-Z]{2}[0-9]{8})",<br></br>            "fontFamily": "Arial",<br></br>            "Bold": "no",<br></br>            "Size": 12<br></br>        },"number": {<br></br>            "format": "^([0-9]{4}[A-Z]{3})$",<br></br>            "fontFamily": "Arial",<br></br>            "Bold": "no",<br></br>            "Size": 12<br></br>        },<br></br>        "plate": {<br></br>            "format": "(\\s[0-9]{7})",<br></br>            "fontFamily": "Tahoma",<br></br>            "Bold": "yes",<br></br>            "Size": 12<br></br>        },<br></br>        "created": {<br></br>            "format": "^([0-9]{2}\\s(de)\\s[a-zA-Z]{3,15}\\s(de)\\s[0-9]{4})",<br></br>            "fontFamily": "Helvetica",<br></br>            "Bold": "no",<br></br>            "Size": 11<br></br>        },<br></br>        "name": {<br></br>            "format": "^([a-zA-Z]{3,5}\\s[a-zA-Z]{3,5})$",<br></br>            "fontFamily": "Tahoma",<br></br>            "Bold": "no",<br></br>            "Size": 10<br></br>        }        <br></br>    }<br></br>}
```

 </div> </div>###  Syntax of the Rules

Rules are created using regular expressions (regex). For example, the `format` attribute in the `policy` section of above sample code, checks for a policy number within the PDF with the following parameters:

- `^` indicates that policy element should begin with what follows
- `[A-Z]{2}` indicates two uppercase characters
- `[0-9]{8}` indicates eight numerical characters
 
The `fontFamily`, `bold`, and `size` attributes specify how the policy number is expected to appear in the PDF file.

If the comparison identifies a string that matches the format but does not match these expected display attributes that difference will be noted in the report.

### Checking the Rules

You can compare the two PDF files just on the basis of the rules that you have created. Here is a sample of a script for that function.

### Sample checkRules.js File

<div class="codeSnippet" id="bkmrk-copy-var-%24basefilena"><div class="codeSnippetBody">```
var $basefilename = <span style="color:#df5000;">"{mds}/OriginalFile.pdf"</span>;<br></br>var $comparisonfilename = <span style="color:#df5000;">"{mds}/NewFile.pdf"</span>;<br></br>var $rulesfilename = <span style="color:#df5000;">"{mds}/rules.txt"</span>;<br></br><br></br><br></br>var response = <span style="color:#795da3;">assertPDFRules</span><span style="color:#000000;">(</span>$basefilename<span style="color:#000000;">,</span>$comparisonfilename<span style="color:#000000;">,</span>$rulesfilename<span style="color:#000000;">)</span>;<br></br>var responseObj = JSON<span style="color:#000000;">.</span><span style="color:#795da3;">parse</span><span style="color:#000000;">(</span>response<span style="color:#000000;">)</span>;<br></br>var summaryRuleDiffs = responseObj<span style="color:#000000;">.</span>content<span style="color:#000000;">.</span>summaryRuleDiffs;<br></br><span style="color:#a71d5d;font-weight:bold;">for</span><span style="color:#000000;">(</span>var idx = <span style="color:#008080;">0</span>; idx <span style="color:#000000;"><</span> summaryRuleDiffs<span style="color:#000000;">.</span>length; idx<span style="color:#000000;">++)</span> <span style="color:#000000;">{</span><br></br>    var matches = summaryRuleDiffs<span style="color:#000000;">[</span>idx<span style="color:#000000;">].</span>matches;<br></br>  <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"-------------------------------------------------------------------------------------------------------"</span><span style="color:#000000;">)</span>;<br></br>  <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"Page: "</span> <span style="color:#000000;">+</span> summaryRuleDiffs<span style="color:#000000;">[</span>idx<span style="color:#000000;">].</span>pageId<span style="color:#000000;">)</span>; <br></br>  <span style="color:#a71d5d;font-weight:bold;">for</span><span style="color:#000000;">(</span>var matchId = <span style="color:#008080;">0</span>; matchId <span style="color:#000000;"><</span> matches<span style="color:#000000;">.</span>length; matchId<span style="color:#000000;">++)</span> <span style="color:#000000;">{</span><br></br>      <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"     Rule Name: "</span> <span style="color:#000000;">+</span> matches<span style="color:#000000;">[</span>matchId<span style="color:#000000;">].</span>name<span style="color:#000000;">)</span>; <br></br>    var rules = matches<span style="color:#000000;">[</span>matchId<span style="color:#000000;">].</span>rules;<br></br>    <span style="color:#a71d5d;font-weight:bold;">for</span><span style="color:#000000;">(</span>var ruleId = <span style="color:#008080;">0</span>; ruleId <span style="color:#000000;"><</span> rules<span style="color:#000000;">.</span>length; ruleId<span style="color:#000000;">++)</span> <span style="color:#000000;">{</span><br></br>      <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"          Found: "</span> <span style="color:#000000;">+</span> rules<span style="color:#000000;">[</span>ruleId<span style="color:#000000;">].</span>found<span style="color:#000000;">)</span>; <br></br>      <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"          Match: "</span> <span style="color:#000000;">+</span>  rules<span style="color:#000000;">[</span>ruleId<span style="color:#000000;">].</span>match<span style="color:#000000;">)</span>; <br></br>      <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"          Font Expected: "</span> <span style="color:#000000;">+</span> rules<span style="color:#000000;">[</span>ruleId<span style="color:#000000;">].</span>fontExpected<span style="color:#000000;">)</span>; <br></br>      <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"          Font Found: "</span> <span style="color:#000000;">+</span>  rules<span style="color:#000000;">[</span>ruleId<span style="color:#000000;">].</span>foundFont<span style="color:#000000;">)</span>; <br></br>      <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"\r\n"</span><span style="color:#000000;">)</span>;<br></br>    <span style="color:#000000;">}</span><br></br>    <br></br>  <span style="color:#000000;">}</span>  <br></br><span style="color:#000000;">}</span>
```

 </div> </div>- `$basefilename` is the name and location of the base PDF file for the comparison. In the sample code this file is `OriginalFile.pdf`.
- `$comparisonfilename` is the name and location of the comparison PDF file. In the sample code this file is `NewFile.pdf`.
- `$rulesfilename` is the name and location of the rules file. In the sample code, this file is `rules.txt`.
- `assertPDFRules` is the action that invokes the comparison function
 
### Performing the Rules Check

From Services Workbench, load the `checkRules.js` file, and click **Play Script**. After the script has been run, the response tab is populated with the details of what rules were chcked and what was found, with any differences noted.

#### **Sample Output on the Response Tab** 

```
   Rule Name: policy
```

```
      Found: AB12345678
```

```
      Match: AB12345678
```

```
      Font Expected: Arial
```

```
      Font Found: Helvetica
```

This is just the output for the `policy` element of the rules file.

## <a name="Comparin"></a>Comparing PDFs

You can also compare the complete content of two PDF files. Here is a sample of a script for that function.

### Sample compareJson.js File

<div class="codeSnippet" id="bkmrk-copy-var-%24basefilena-1"><div class="codeSnippetBody">```
var $basefilename = <span style="color:#df5000;">"{mds}/OriginalFile.pdf"</span>;<br></br>var $comparisonfilename = <span style="color:#df5000;">"{mds}/NewFile.pdf"</span>;<br></br><br></br>var response = <span style="color:#795da3;">comparePDF</span><span style="color:#000000;">(</span>$basefilename<span style="color:#000000;">,</span>$comparisonfilename<span style="color:#000000;">)</span>;<br></br><br></br>var objResponse = JSON<span style="color:#000000;">.</span><span style="color:#795da3;">parse</span><span style="color:#000000;">(</span>response<span style="color:#000000;">)</span>;<br></br><br></br>var response = objResponse<span style="color:#000000;">.</span>content;<br></br><span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"-------------------------------------------------------------------------------------------------------"</span><span style="color:#000000;">)</span>;<br></br><span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"Base Pdf: "</span> <span style="color:#000000;">+</span> response<span style="color:#000000;">.</span>template <span style="color:#000000;">+</span> <span style="color:#df5000;">"                    Pages Count: "</span> <span style="color:#000000;">+</span> response<span style="color:#000000;">.</span>templatePageCount<span style="color:#000000;">)</span>;<br></br><span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"Comparison Pdf: "</span> <span style="color:#000000;">+</span> response<span style="color:#000000;">.</span>pdf <span style="color:#000000;">+</span> <span style="color:#df5000;">"                Pages Count: "</span> <span style="color:#000000;">+</span> response<span style="color:#000000;">.</span>pageCount<span style="color:#000000;">)</span>;<br></br><span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"-------------------------------------------------------------------------------------------------------"</span><span style="color:#000000;">)</span>;<br></br><span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"Differences per page:"</span><span style="color:#000000;">)</span>;<br></br>var pages = response<span style="color:#000000;">.</span>pages;<br></br><span style="color:#a71d5d;font-weight:bold;">for</span><span style="color:#000000;">(</span>var idx = <span style="color:#008080;">0</span>; idx <span style="color:#000000;"><</span> pages<span style="color:#000000;">.</span>length; idx<span style="color:#000000;">++)</span> <span style="color:#000000;">{</span><br></br>      <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"        Page: "</span> <span style="color:#000000;">+</span>  pages<span style="color:#000000;">[</span>idx<span style="color:#000000;">].</span>pageId <span style="color:#000000;">+</span> <span style="color:#df5000;">": "</span> <span style="color:#000000;">+</span> pages<span style="color:#000000;">[</span>idx<span style="color:#000000;">].</span>differences <span style="color:#000000;">+</span> <span style="color:#df5000;">" differences."</span><span style="color:#000000;">)</span>;   <br></br><span style="color:#000000;">}</span><br></br><span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"-------------------------------------------------------------------------------------------------------"</span><span style="color:#000000;">)</span>;<br></br>var differences = response<span style="color:#000000;">.</span>dynamicPdf;<br></br> <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"PDF: "</span> <span style="color:#000000;">+</span> response<span style="color:#000000;">.</span>pdf <span style="color:#000000;">+</span>  <span style="color:#df5000;">"\r\n "</span> <span style="color:#000000;">)</span>;<br></br><span style="color:#a71d5d;font-weight:bold;">for</span><span style="color:#000000;">(</span>var idx = <span style="color:#008080;">0</span>; idx <span style="color:#000000;"><</span> differences<span style="color:#000000;">.</span>length; idx<span style="color:#000000;">++)</span> <span style="color:#000000;">{</span>   <br></br>    <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"        PageId: "</span> <span style="color:#000000;">+</span> differences<span style="color:#000000;">[</span>idx<span style="color:#000000;">].</span>pageId  <span style="color:#000000;">+</span> <span style="color:#df5000;">", Font: "</span> <span style="color:#000000;">+</span> differences<span style="color:#000000;">[</span>idx<span style="color:#000000;">].</span>details<span style="color:#000000;">.</span>font <span style="color:#000000;">)</span>;<br></br>    <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"                Found: "</span> <span style="color:#000000;">+</span> differences<span style="color:#000000;">[</span>idx<span style="color:#000000;">].</span>details<span style="color:#000000;">.</span>pdf <span style="color:#000000;">+</span> <span style="color:#df5000;">"\r\n"</span><span style="color:#000000;">)</span>;<br></br>    <span style="color:#795da3;">log</span><span style="color:#000000;">(</span><span style="color:#df5000;">"-------------------------------------------------------------------------------------------------------"</span><span style="color:#000000;">)</span>;<br></br><span style="color:#000000;">}</span>
```

 </div> </div>- `$basefilename` is the name and location of the base PDF file for the comparison. In the sample code this file is `OriginalFile.pdf`.
- `$comparisonfilename` is the name and location of the comparison PDF file. In the sample code this file is `NewFile.pdf`.
- `$rulesfilename` is the name and location of the rules file. In the sample code, this file is `rules.txt`.
- `comparePDF` is the action that invokes the comparison function
 
### Performing the Comparison

From Services Workbench, load the `compareJason.js` file, click **Play Script**. After the script has been run, the response tab is populated with the details of what rules were chcked and what was found, with any differences noted.

#### **Sample Output on the Response Tab** 

```
----------------------------------------------------
```

```
Base PDF: OriginalFile.pdf    Page Count: 3
```

```
Comparison PDF: NewFile.PDF   Page Count: 3
```

```
----------------------------------------------------
```

```
Differences per page:
```

```
   Page: 3: 1 differences
```

```
----------------------------------------------------
```

```
PDF: NewFile.PDF 
```

```
 
```

```
    PageId: 3, Font: Helvetica
```

```
            Found: $99.99
```

```
 
```

 Explanation:

- Header contains a summary of the comparison, which includes the file names and the number of pages.
- Differences per page are noted. In this case only one difference was found and it was found on page 3.
- Report shows the change in the comparison file. In this case it was the text "$99.99" in Helvetica.
 
```
 
```

### HTML Comparison Report

The comparison report lists the details of all differences to both text within the two PDF files.

Details include:

- Text attributes: Font size, formatting
- Location information: X/Y coordinates, height/width, page number
- Image information: Report will show both images
 
 ![](https://docs.appvance.net/uploads/images/gallery/2026-07/scaled-1680-/v9l5RjkEIzGMIWnW-0ca65fdb4fe7.png)

<div id="bkmrk-last-updated%3A-octobe">---

Last updated: <span class="mc-variable General.NewDateTimeVariable variable">October 14, 2023</span>

If you have any feedback on this topic, please contact us at <documentation@appvance.ai>

</div>