# Create test for pdf files

**URL:** <https://discourse.cakephp.org/t/create-test-for-pdf-files/12020>\
**Category:** Testing\
**Tags:** help\
**Created:** [May 15, 2024, 12:16pm UTC](https://discourse.cakephp.org/t/create-test-for-pdf-files/12020 "2024-05-15T12:16:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arodu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/arodu/32/2253_2.png) [@arodu](https://discourse.cakephp.org/u/arodu)\
**Post date:** [May 15, 2024, 12:16pm UTC](https://discourse.cakephp.org/t/create-test-for-pdf-files/12020/1 "2024-05-15T12:16:47Z")

</div>

What is the best way to create a test from a pdf file?

I thought about using a predefined file, and then comparing it against the generated file, but it is not working for me.

Although the content of the 2 files is the same, I suppose that internally something changes and they are not exactly the same

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [May 15, 2024, 12:30pm UTC](https://discourse.cakephp.org/t/create-test-for-pdf-files/12020/2 "2024-05-15T12:30:07Z")

</div>

You probably didn’t write the PDF creation logic yourself, therefore you shouldn’t test it since you depend on your library to do what it says it can do.

I’d rather unit test the specific peaces which are used to build your content inside your PDF instead of testing the whole PDF generation.

And if you really need to test the PDF generation I’d be fine to test, that the file is being generated and that the mimetype of the file is correct.

---

<div class="post-metadata">

**Author:** ![jmcd73](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jmcd73/32/481_2.png) [@jmcd73](https://discourse.cakephp.org/u/jmcd73)\
**Post date:** [May 17, 2024, 3:54am UTC](https://discourse.cakephp.org/t/create-test-for-pdf-files/12020/4 "2024-05-17T03:54:43Z")

</div>

You could use `pdftotext` to convert the generated PDF to text to make sure what you are entering is appearing in the file.

```auto
public function getContents($outputFile)
    {
        $cmd = 'pdftotext ' . $outputFile . ' -';

        exec($cmd, $outputContents);

        return $outputContents;
    }

```
