title: Why is the quality on generated PDFs so low with this code?
body: <p>I have the following code. It's used to combine various image attachments (and pdfs) into one PDF. For some reason, when I take even a single PDF and put it through the code, the end result comes out looking very bad compared to the original PDF. In addition, I can select text in the source PDF, but in the generated one I cannot.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <pre><code>// PDF object
  $pdf = new Imagick();
  $max_resolution = array('x' =&gt; 100, 'y' =&gt; 100);
  
  foreach($attachment_ids as $attachment_id) {
      $attachment = DAO_Attachment::get($attachment_id);
      $file = Storage_Attachments::get($attachment);
      // Temporarily store our attachment
      $im = new Imagick();
      $im-&gt;readImageBlob($file);
      // We need to reset the iterator otherwise only one page will be rotated
      $im-&gt;resetIterator();
  
      // Get the resolution
      $resolution = $im-&gt;getImageResolution();
      if($resolution['x'] &gt; $max_resolution['x']) {
          $max_resolution['x'] = $resolution['x'];
      }
      if($resolution['y'] &gt; $max_resolution['y']) {
          $max_resolution['y'] = $resolution['y'];
      }
  
      $num_pages = $im-&gt;getNumberImages();
  
      $rotation = array_shift($rotations);
      $degrees = $rotation &gt; 0 ? 360 - $rotation : 0;
      $pages = array();
  
      if($degrees &gt; 0) {
          // Rotate each page
          for($i = 1; $i &lt;= $num_pages; $i++) {
              $im-&gt;nextImage();
              $im-&gt;rotateImage(new ImagickPixel(), $degrees);
          }
      }
  
      // We need to reset the iterator again so all of our pages will be added to the pdf
      $im-&gt;resetIterator();
  
      // If the image format isn't a pdf, convert it to a png
      if($im-&gt;getImageFormat !== 'pdf') {
          $im-&gt;setImageFormat('png');
          // Opacity
          if(method_exists($im, 'setImageOpacity'))
              $im-&gt;setImageOpacity(1.0);
      }
  
      $im-&gt;setImageCompression(imagick::COMPRESSION_LOSSLESSJPEG); 
      $im-&gt;setImageCompressionQuality(100);
      $im-&gt;stripImage();
  
      // Add the rotated attachment to the PDF
      $pdf-&gt;addImage($im);
  
      // Free
      $im-&gt;destroy();
  }
  
  // Create a composite
  $pdf-&gt;setImageFormat('pdf');
  
  // Compress output
  $pdf-&gt;setImageCompression(imagick::COMPRESSION_LOSSLESSJPEG); 
  $pdf-&gt;setImageCompressionQuality(100);
  $pdf-&gt;stripImage();
  
  // Set resolution
  $pdf-&gt;setImageResolution($max_resolution['x'], $max_resolution['y']);
  </code></pre>