Search code examples
pdfimagemagickghostscriptpostscript

RMagick convert ps to pdf


When I convert a PS file to PDF, it works fine on the local machine, but on the production server, it adds margin to the page, as if I selected Scale to Fit instead of Scale: 100% from the Mac OSX Preview print settings. How do i prevent this? Currently, I'm doing:

Magick::ImageList::new('cool.ps').write('cool.pdf') # only works on local machine

UPDATE: I solved this! See my answer below.


Solution

  • Solution

    system("gs -dSAFER -dBATCH -dNOPAUSE -sPAPERSIZE=letter -sDEVICE=pdfwrite \
            -sOutputFile='cool.pdf' 'cool.ps'")
    
    • -dSAFER (readonly) prevents the input file from being changed.
    • -dBATCH quits Ghostscript automatically.
    • -dNOPAUSE continues automatically after each page conversion.

    I realized that you can also use the command ps2pdf (which seems nicer) instead of gs, but the above worked fine, so I kept it because I have other more important things to do, and I cannot easily deploy to and test on production.

    How I came about this solution

    First, I figured out that I could use ghostscript to convert PostScript (PS) files to PDF.

    So, I tried using Ghostscript but got the same result. (In hindsight, I learned this is because ImageMagic delegates PS & PDF formats to Ghostscript anyhow.)

    After further examination of the production-server generated PDF, I realized that actually the page size of the PDF generated by the production server (vs. that of the PDF generated by my local machine) was slightly larger (In hindsight, I think it was a4.), thus, causing the addition of margin, haha. :)

    I thought to myself, why is the production server using a different page size? ...

    Aha! The Ghostscript configuration files (Actually, I soon thereafter learned that they're called Ghostscript initialization files.) on the production server must be specifying a different default Ghostscript page size.

    Alright, who's the nincompoop that decided (when installing Ghostscript on the production server) to override the default paper size to a4?

    Fortunately, since I don't have production access, I could set it explicitly with -sPAPERSIZE=legal, but I wasn't done investigating things...

    On my local machine, I did a man gs and then searched for "init" by typing /init and then enter. Then, n & N to cycle through the results. I saw gs_init.ps, so I quit q and did:

    sudo find / -name gs_init.ps
    

    I got:

    /opt/local/share/ghostscript/8.71/Resource/Init/gs_init.ps
    /opt/local/var/macports/software/ghostscript/8.71_3/opt/local/share/ghostscript/8.71/Resource/Init/gs_init.ps
    

    I catted the second result (since I remembered installing ImageMagick with MacPorts, although if I had to do it again, I'd probably install ImageMagick from source or with HomeBrew instead). I searched for PAPERSIZE and found:

    % Optionally choose a default paper size other than U.S. letter.
    % The default page size for many devices is set at compile time to 
    % letter, but this can be changed to A4 although this is rarely done.  
    % Some devices such as bbox have a different default page size,
    % and should not be set to A4 or letter.
    % When ghostscript is used in countries that use the international
    % standard page size A4 rather than US letter, the page size of 
    % devices that default to letter or A4 can be changed by setting
    % DEFAULTPAPERSIZE.
    % /DEFAULTPAPERSIZE (a4) def