title: Ghostscript resize PDF not working with ImageMagick PDF
body: <p>I'm trying to write a small program for Linux to resize PDFs and adjust margins. My plan was to use Ghostscript as a back-end. This Terminal command successfully resizes most PDFs:</p>
  
  <pre><code>gs -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dFIXEDMEDIA -dPDFFitPage \
   -dDEVICEWIDTHPOINTS=300 -dDEVICEHEIGHTPOINTS=400 -sOutputFile=out.pdf file.pdf
  </code></pre>
  
  <p>The <code>-dPDFFitPage</code> option scales pages to fit the new size, adding whitespace as padding if the image aspect ratio doesn't match the specified dimensions. Removing <code>-dPDFFitPage</code> changes the page size without scaling - pages will be cropped if too large, or whitespace added if too small.</p>
  
  <p>However, the command doesn't work with PDFs created by ImageMagick's "convert" program. The PDF is scaled but no whitespace is added so only one dimension will be correct in the output file. Without the <code>-dPDFFitPage</code> option oversize images are cropped as expected, but nothing appears to happen if the image is smaller than the new page size (i.e. no whitespace is added).</p>
  
  <p>It appears that the problem lies with the fact that the PDF is empty apart from the image. How can I get Ghostscript to adjust the page size and fill the empty part of the page with white if necessary?</p>
  
  <p><strong>Edit: Example files</strong></p>
  
  <p>To see the <em>problem</em>, try with these <a href="https://drive.google.com/folderview?id=0By292-yIKOWPfnRabGlJZTk2R1JYajNkMDBlN0IzN1dOelFwczlfVFhkaExTNjd4b1N1LTg&amp;usp=sharing" rel="nofollow">example files</a> (there are also example Ghostscript output PDFs).</p>
  
  <p>Alternatively, use ImageMagick <em>(or any image editor)</em> to create a suitable example image yourself:</p>
  
  <pre><code>convert -size 500x500 xc:skyblue -fill black -draw "circle 250,250 0,250" image.png
  </code></pre>
  
  <p>Now, use ImageMagick <em>(<strong>NOT</strong> any other program)</em> to convert it to a PDF:</p>
  
  <pre><code>convert image.png file.pdf
  </code></pre>
  
  <p>Now try this with the Ghostscript code. See what happens when you try it:</p>
  
  <ul>
  <li>with and without <code>-dPDFFitPage</code></li>
  <li>with the width and height smaller than the original, and with them larger</li>
  </ul>
  
  <p>To see how it is <em>supposed</em> to work, try using any other tool to convert the example image to a PDF. You could (for example) use LibreOffice or LaTeX, or take the PDF you just made <em>(the one that didn't work)</em> and "Print" it to create another PDF <em>(which for some reason will work)</em>. Make sure the image fills the entire page of the PDF (there should be no whitespace/border in the PDF you use to test with Ghostscript, but the output PDF created by Ghostscript should have some whitespace.)</p>