Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Unit)test pdf generation

We implemented a magento module https://github.com/firegento/firegento-pdf/ and I plan to write tests for the module.

The problem is: The extension generates pdfs.

Is there any framework, or whatever to test pdfs? It would be totally fine if I can check for text in the pdf. I don't want to check the correct placement.

Andy ideas?

This looks promising but feels oversized. http://webcheatsheet.com/php/reading_clean_text_from_pdf.php

like image 883
Fabian Blechschmidt Avatar asked Feb 15 '23 19:02

Fabian Blechschmidt


1 Answers

I use PdfBox for a similar module, a Java based command line utility that extracts text from a PDF and optionally converts it to HTML: http://pdfbox.apache.org/commandline/#extractText

To use it within PHPUnit tests, I wrote a PHP interface for the relevant PdfBox methods: https://github.com/schmengler/PdfBox

Example

use SGH\PdfBox;

//$pdf = GENERATED_PDF;
$converter = new PdfBox;
$converter->setPathToPdfBox('/usr/bin/pdfbox-app-1.7.0.jar');
$text = $converter->textFromPdfStream($pdf);

Further reading: Unit Test Generated PDFs with PHPUnit and PDFBox

like image 169
Fabian Schmengler Avatar answered Feb 24 '23 01:02

Fabian Schmengler