Create test for pdf files

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

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.

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

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

        exec($cmd, $outputContents);

        return $outputContents;
    }