title: Scale pdf to A4 pdf with ratio - width to sides & centered
body: <p>I have a pdf that is created using an image (so it has image's original dimensions)</p>
  
  <p>Now, I am trying to scale it in its ratio to fit A4 pdf (width touching to sides and centering vertically).</p>
  
  <p>This is the code I tried:</p>
  
  <pre><code>    $imagick = new \Imagick();
      $imagick-&gt;setResolution(576,576);
      $imagick-&gt;readImage($path);
      $imagick-&gt;resizeImage(2480,3508,\Imagick::FILTER_CUBIC,1);
      $imagick-&gt;setCompressionQuality(80);
      $imagick-&gt;setImageFormat('pdf');
      $imagick-&gt;writeImage($path);
  </code></pre>
  
  <p>But it's disturbing the image ratio to fit full page (100% width &amp; 100% height).</p>
  
  <p>For example, original image 717x558:
  <a href="https://i.stack.imgur.com/HfmmP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HfmmP.png" alt="enter image description here"></a></p>
  
  <p>Exported image,</p>
  
  <p><a href="https://i.stack.imgur.com/ZvSQC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZvSQC.png" alt="enter image description here"></a></p>
  
  <p>However, what I am trying to achieve is this:</p>
  
  <p><a href="https://i.stack.imgur.com/w3TsM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w3TsM.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/glRcI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/glRcI.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>** Update **</p>
  
  <pre><code>$imagick = new \Imagick();
  $imagick-&gt;readImage($path);
  $imagick-&gt;resizeImage(595,842,\Imagick::FILTER_CUBIC,1);
  $imagick-&gt;setBackgroundColor(new ImagickPixel('white')); 
  $imagick-&gt;setGravity ( Imagick::GRAVITY_CENTER );
  $imagick-&gt;extentImage ( 595 , 843 , 0 , 0 );
  $imagick-&gt;setImageFormat('pdf');
  $imagick-&gt;writeImage($path);
  </code></pre>
  
  <p>Using this, this is the output (it's full page but lost the ratio):</p>
  
  <p><a href="https://i.stack.imgur.com/cb3gl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cb3gl.png" alt="enter image description here"></a></p>