title: Converting PDF to PNG for Tesseract to process
body: <p>I'm having an issue at the moment with Imagemagick and Tesseract.</p>
  
  <p>I'm working on a command-line classifier for documents in PHP. The idea is that it takes in PDF documents and uses the <a href="http://pipeline.thephpleague.com/" rel="nofollow noreferrer">League Pipeline package</a> to pass it through numerous steps. The steps I've identified as necessary are as follows:</p>
  
  <ol>
  <li>Convert PDF to a PNG file</li>
  <li>Extract text from PNG file</li>
  <li>Run text through a machine learning library to classify it</li>
  </ol>
  
  <p>The main command for that looks like this:</p>
  
  <pre><code>&lt;?php
  
  namespace Matthewbdaly\LetterClassifier\Commands;
  
  use Symfony\Component\Console\Command\Command;
  use Symfony\Component\Console\Input\InputInterface;
  use Symfony\Component\Console\Output\OutputInterface;
  use Symfony\Component\Console\Input\InputArgument;
  use League\Pipeline\Pipeline;
  use Matthewbdaly\LetterClassifier\Stages\ConvertPdfToPng;
  use Matthewbdaly\LetterClassifier\Stages\ReadFile;
  
  class Processor extends Command
  {
      protected function configure()
      {
          $this-&gt;setName('process')
              -&gt;setDescription('Processes a file')
              -&gt;setHelp('This command processes a file')
              -&gt;addArgument('file', InputArgument::REQUIRED, 'File to process');
      }
  
      protected function execute(InputInterface $input, OutputInterface $output)
      {
          $file = $input-&gt;getArgument('file');
          $pipeline = (new Pipeline)
              -&gt;pipe(new ConvertPdfToPng)
              -&gt;pipe(new ReadFile);
          $pipeline-&gt;process($file);
      }
  }
  </code></pre>
  
  <p>As you can see, it accepts a filename as the first argument, then defines a pipeline for the required steps, before passing the file to the pipeline.</p>
  
  <p>The step for converting the PDF looks like this:</p>
  
  <pre><code>&lt;?php
  
  namespace Matthewbdaly\LetterClassifier\Stages;
  
  use Imagick;
  
  class ConvertPdfToPng
  {
      public function __invoke($file)
      {
          $tmp = tmpfile();
          $uri = stream_get_meta_data($tmp)['uri'];
          $img = new Imagick($file);
          $img-&gt;setResolution(300, 300);
          $img-&gt;setImageDepth(8);
          $img-&gt;setImageFormat('png');
          $img-&gt;writeImage($uri);
          return $tmp;
      }
  }
  </code></pre>
  
  <p>It writes a PNG version of the PDF as a temporary file. The generated file looks OK, at least to my eye, but it can't be read correctly by Tesseract. Here's the second step where Tesseract should process the file:</p>
  
  <pre><code>&lt;?php
  
  namespace Matthewbdaly\LetterClassifier\Stages;
  
  use thiagoalessio\TesseractOCR\TesseractOCR;
  
  class ReadFile
  {
  
      public function __invoke($file)
      {
          $uri = stream_get_meta_data($file)['uri'];
          $ocr = new TesseractOCR($uri);
          $output = $ocr-&gt;lang('eng')-&gt;run();
          eval(\Psy\Sh());
      }
  }
  </code></pre>
  
  <p>The output from Psysh looks like this:</p>
  
  <pre><code>=&gt; """
     Am sum\n
     \n
     mm“ m mun SuHrkw-l\n
     n m 51mm\n
     \n
     mm\n
     \n
     um um\n
     \n
     ms Murine\n
     1 Elm: 51mm\n
     Emnuumn\n
     \n
     a mu\n
     \n
     m Mm 2m-\n
     Dav st-n-m.\n
     \n
     P‘Eualanﬂ ma lumnﬂarvlmamrmy ”Hay ”mum-m-\n
     we we “mum-m n: "mum,“ m mun\n
     \n
     vm [harem\n
     \n
     Am smrm
     """
  </code></pre>
  
  <p>This is not the content of the letter I'm trying to classify - the text is getting mangled. If I run the following commands from the shell, they work as expected to convert and write the letter's text to the output file:</p>
  
  <pre><code>convert -density 300 Quote.pdf output.png
  tesseract output.png output
  </code></pre>
  
  <p>And if I hardcode the path to the file in the Tesseract stage to point at the <code>output.png</code> generated using the <code>convert</code> command, that works. So I'm fairly confident the issue is with the step to generate the PNG file. I'm not that experienced with using Imagemagick, so I'm unsure why the file can't be processed, but it seems like there's a setting of some kind that I'm missing.</p>
  
  <p>Can anyone suggest what the problem might be?</p>