# PRO TIP - Validating and checking PDF content in synthetic

**URL:** <https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578>\
**Category:** Dynatrace Tips\
**Tags:** browser-monitors, tips-and-tricks, synthetic-monitoring\
**Created:** [September 19, 2022, 8:37pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578 "2022-09-19T20:37:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julius\_Loman](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/julius_loman/32/51_2.png) [@Julius\_Loman](https://community.dynatrace.com/u/Julius_Loman)\
**Post date:** [September 19, 2022, 8:37pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/1 "2022-09-19T20:37:17Z")

</div>

Browser monitors allow you to handle quite complex scenarios when it comes to testing your application,. Sometimes you need to check in your test case if your application is producing PDF and validate the content. Luckily, there is an easy solution for that by utilizing Mozilla’s PDF.js library for working with PDF files in JavaScript.

Browser monitors allow you to use [JavaScript clickpath events](https://www.dynatrace.com/support/help/shortlink/id-brower-clickpath-events#javascript). These events are simply pieces of JavaScript code to be executed in the browser running your browser monitors. Also the code is able to talk back to the synthetic engine using the [JavaScript Event API](https://www.dynatrace.com/support/help/shortlink/id-brower-clickpath-events#javascript-event-api).

In the following “almost self-explaining” 😁 code example below we will:

1. Start an asynchronous execution and tell the synthetic engine to wait for the results.
2. Load the [PDF.js](https://mozilla.github.io/pdf.js/) library in the browser.
3. Get the link to the PDF from the currently opened application page.
4. Load the PDF by PDF.js library.
5. Open the first page.
6. And finally validate if the page contains expected content.
7. We mark the step as failed when something goes bad of course 😀.

```javascript
api.startAsyncSyntheticEvent();

// Insert PDFJS Library into browser window
var pdfjsTag = document.createElement('script');
pdfjsTag.setAttribute('src', '//mozilla.github.io/pdf.js/build/pdf.mjs');
pdfjsTag.setAttribute('type', 'module');
pdfjsTag.setAttribute('id', 'pdfjslib');
document.head.appendChild(pdfjsTag);

// Wait until the PDFJS library is loaded
pdfjsTag.addEventListener('load', function() {
    api.info("PDFJS - Library loaded");
    // Get link to PDF document from the page
    var url = document.getElementById('pdflink').getAttribute('href');
    api.info("PDFJS - Downloading pdf document from " + url);
    var pdfjsLib = window['pdfjs-dist/build/pdf'];
    pdfjsLib.GlobalWorkerOptions.workersrc='//mozilla.github.io/pdf.js/build/pdf.worker.mjs';
    var loadingTask = pdfjsLib.getDocument(url);
    loadingTask.promise.then(function(pdf) {
        api.info('PDFJS - PDF file loaded');
        // Fetch the first page
        var pageNumber = 1;
        pdf.getPage(pageNumber).then(function(page) {
            api.info('PDFJS - Loaded page ' + pageNumber);
            page.getTextContent().then(function(textContent) {
                var textItems = textContent.items;
                var finalString = "";

                // Concatenate the string of the item to the final string
                for (var i = 0; i < textItems.length; i++) {
                    var item = textItems[i];
                    finalString += item.str + " ";
                }
                api.info('PDFJS - Page text:' + finalString);

                // Check if text is present in the PDF page
                if (finalString.includes("Hello")) {
                    api.finish();
                } else {
                    api.fail("Validation error - Text not present in PDF");
                }
            });
        });
    }, function(reason) {
        // PDF loading error
        console.error("Could not open PDF", reason);
        api.fail("Could not open PDF (" + reason + ")");
    });
});

```

If you run the test locally, you can see the information messages easily in your browser, which is useful for further troubleshooting and fine-tuning your test scenario:

```auto
Event 4
------------------------------------------------------------------------------------
2022-09-19 10:32:56 INFO PDFJS - Library loaded
2022-09-19 10:32:56 INFO PDFJS - Downloading pdf document from https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/examples/learning/helloworld.pdf
2022-09-19 10:32:56 INFO PDFJS - PDF file loaded
2022-09-19 10:32:56 INFO PDFJS - Loaded page 1
2022-09-19 10:32:56 INFO PDFJS - Page text:Hello, world!

```

Happy PDF validation!

---

<div class="post-metadata">

**Author:** ![dannemca](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/dannemca/32/7215_2.png) [@dannemca](https://community.dynatrace.com/u/dannemca)\
**Post date:** [September 19, 2022, 8:45pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/2 "2022-09-19T20:45:53Z")

</div>

Wow, that’s gold!!! Thank you for sharing this!!!

---

<div class="post-metadata">

**Author:** ![AntonioSousa](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/antoniosousa/32/50_2.png) [@AntonioSousa](https://community.dynatrace.com/u/AntonioSousa)\
**Post date:** [September 19, 2022, 10:48pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/3 "2022-09-19T22:48:54Z")

</div>

This is 5 stars! Thanks a lot Julius for sharing!

---

<div class="post-metadata">

**Author:** ![mizso74](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mizso74/32/41729_2.png) [@mizso74](https://community.dynatrace.com/u/mizso74)\
**Post date:** [September 20, 2022, 8:22pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/4 "2022-09-20T20:22:26Z")

</div>

Amazing! Really nice solution! Thank you for sharing! Lot more similar please… 😉

---

<div class="post-metadata">

**Author:** ![Babar\_Qayyum](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/babar_qayyum/32/13620_2.png) [@Babar\_Qayyum](https://community.dynatrace.com/u/Babar_Qayyum)\
**Post date:** [October 23, 2022, 3:53pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/5 "2022-10-23T15:53:21Z")

</div>

Great Job&nbsp;[@Julius\_Loman](https://community.dynatrace.com/user/3364)

---

<div class="post-metadata">

**Author:** ![xu\_guo](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/xu_guo/32/20268_2.png) [@xu\_guo](https://community.dynatrace.com/u/xu_guo)\
**Post date:** [December 1, 2023, 3:56am UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/6 "2023-12-01T03:56:53Z")

</div>

Hi&nbsp;[@Julius\_Loman](https://community.dynatrace.com/user/3364)&nbsp;,

Thank you for sharing.

We use this code to validate the content in the PDF. However, the current&nbsp;PDFJS Libraries below are not available. The pages return the 404 code.

```markup
//mozilla.github.io/pdf.js/build/pdf.js

//mozilla.github.io/pdf.js/build/pdf.worker.js

```

We find the other available PDFJS Libraries below. Can you please update them? Thank you!

```markup
pdfjsTag.setAttribute('src', '//mozilla.github.io/pdf.js/build/pdf.mjs');
pdfjsTag.setAttribute('type', 'module');

```

```markup
pdfjsLib.GlobalWorkerOptions.workersrc='//mozilla.github.io/pdf.js/build/pdf.worker.mjs';

```

---

<div class="post-metadata">

**Author:** ![Julius\_Loman](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/julius_loman/32/51_2.png) [@Julius\_Loman](https://community.dynatrace.com/u/Julius_Loman)\
**Post date:** [December 2, 2023, 1:13pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/7 "2023-12-02T13:13:24Z")

</div>

Sure, corrected.

---

<div class="post-metadata">

**Author:** ![ayyanar\_chinna1](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@ayyanar\_chinna1](https://community.dynatrace.com/u/ayyanar_chinna1)\
**Post date:** [May 13, 2024, 6:54pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/8 "2024-05-13T18:54:45Z")

</div>

Hi&nbsp;[@Julius\_Loman](https://community.dynatrace.com/user/3364)

i have used this script in my browser clickpath monitor, its failing and throwing error “1601 - JS execution took too long”. could you help me get this fix

 ![ayyanar_chinna1_0-1715626422528.png](https://global.discourse-cdn.com/dynatrace/original/3X/7/4/74527f1fc83f27472a383aa82eedba4771cffe42.png)

---

<div class="post-metadata">

**Author:** ![Julius\_Loman](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/julius_loman/32/51_2.png) [@Julius\_Loman](https://community.dynatrace.com/u/Julius_Loman)\
**Post date:** [May 13, 2024, 9:10pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/9 "2024-05-13T21:10:41Z")

</div>

Have you tried running the test locally?

---

<div class="post-metadata">

**Author:** ![ayyanar\_chinna1](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@ayyanar\_chinna1](https://community.dynatrace.com/u/ayyanar_chinna1)\
**Post date:** [May 13, 2024, 9:30pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/10 "2024-05-13T21:30:37Z")

</div>

I have directly applied to my monitor updated with pdf link

// Get link to PDF document from the page  
var url = document.querySelector(“#\\36 030 \> td:nth-child(5) \> div \> a”).getAttribute(“href”);

---

<div class="post-metadata">

**Author:** ![prasad\_arugonda](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@prasad\_arugonda](https://community.dynatrace.com/u/prasad_arugonda)\
**Post date:** [May 17, 2024, 12:46pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/11 "2024-05-17T12:46:44Z")

</div>

[@Julius\_Loman](https://community.dynatrace.com/user/3364)FYI… When I tested the script you provided, I encountered the following error:

Uncaught TypeError: Cannot read properties of null (reading ‘getAttribute’)

This issue was also captured during synthetic execution.

---

<div class="post-metadata">

**Author:** ![Jennifer](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@Jennifer](https://community.dynatrace.com/u/Jennifer)\
**Post date:** [July 11, 2024, 6:31pm UTC](https://community.dynatrace.com/t/pro-tip-validating-and-checking-pdf-content-in-synthetic/36578/12 "2024-07-11T18:31:45Z")

</div>

Hi Julius,

How would you handle a pdf element that is retrieved from an “onclick” function.&nbsp; It seems the element can be found by id, but does not retrieve the correct URL for downloading the pdf.

Code example:

\<a href=“javascript:void(0);” onclick=“SubmitForm(‘rdPage.aspx?rdReport=Rpt\_Submission.SubReports.SubmissionUpdate&inpMonth=07&inpYear=2024&inpReportID=0002355&LinkHref=True’,‘\_blank’,‘false’,‘’,null,null);” id=“actEdit\_Row1”\>\<span class=“line ThemeBold black rowFontLarge”\>TDL-\*\*\*\*\*l)\</span\>\</a\>

Any help would be greatly appreciated!
