[Tex/LaTex] pdfcrop does not work

ghostscriptpdfcrop

I have just updated to MiKTeX 2.9

I am using Emacs and I run
pdflatex --shell-escape -interaction=nonstopmode "\input" Instructions.tex

pdfcrop gives me the following error

PDFCROP 1.33, 2012/02/01 - Copyright (c) 2002-2012 by Heiko Oberdiek.
!!! Error: Cannot call ghostscript ("C:\Program Files   (x86)\gs\gs8.71\bin\gswin32c.exe")!

so I tried to call pdfcrop manually:

C:\>pdfcrop  --margins 10 --debug Instructions-autopp.pdf s.pdf
PDFCROP 1.33, 2012/02/01 - Copyright (c) 2002-2012 by Heiko Oberdiek.
* Restricted mode: disabled
* Option `pdfversion': auto
* OS name: MSWin32
* Arch name: MSWin32-x86-multi-thread
* System: win
* Not found (gswin32c): gswin32c.exe
* Not found (gswin32c): C:\Perl\site\bin\gswin32c.exe
* Not found (gswin32c): C:\Perl\bin\gswin32c.exe
* Not found (gswin32c): C:\GTK\bin\gswin32c.exe
* Not found (gswin32c): C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\gswin32c.exe
* Not found (gswin32c): C:\Program Files (x86)\OpenVPN\bin\gswin32c.exe
* Not found (gswin32c): C:\Program Files (x86)\ispell\gswin32c.exe
* Not found (gswin32c): C:\Program Files (x86)\ispell\gswin32c.exe
* Not found (gs): gs.exe
* Not found (gs): C:\Perl\site\bin\gs.exe
* Not found (gs): C:\Perl\bin\gs.exe
* Not found (gs): C:\GTK\bin\gs.exe
* Not found (gs): C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\gs.exe
* Not found (gs): C:\Program Files (x86)\OpenVPN\bin\gs.exe
* Not found (gs): C:\Program Files (x86)\ispell\gs.exe
* Not found (gs): C:\Program Files (x86)\ispell\gs.exe
* Search registry at `HKEY_LOCAL_MACHINE/SOFTWARE/'.
* Registry entry found: HKEY_LOCAL_MACHINE/SOFTWARE/GPL Ghostscript/
* Registry entry found: HKEY_LOCAL_MACHINE/SOFTWARE/GPL Ghostscript/8.71/
  GS_DLL = C:\Program Files (x86)\gs\gs8.71\bin\gsdll32.dll
  EXE found: C:\Program Files (x86)\gs\gs8.71\bin\gswin32c.exe
* Found (via registry): C:\Program Files (x86)\gs\gs8.71\bin\gswin32c.exe
* Autodetected ghostscript command: C:\Program Files (x86)\gs\gs8.71\bin\gswin32c.exe
* Input file: Instructions-autopp.pdf
* Output file: s.pdf
* Margins: 10 10 10 10
* PDF header: %PDF-1.4
* Using PDF minor version: 4
* Running ghostscript for BoundingBox calculation ...
* Ghostscript call: "C:\Program Files (x86)\gs\gs8.71\bin\gswin32c.exe" -sDEVICE=bbox -    dBATCH -dNOPA
USE -c save pop -f Instructions-autopp.pdf
* Cleanup
* Temporary files: tmp-pdfcrop-1412.tex
!!! Error: Cannot call ghostscript ("C:\Program Files (x86)\gs\gs8.71\bin\gswin32c.exe")!
* Cleanup
* Temporary files: tmp-pdfcrop-1412.tex

so it seems to find ghostscript but still there is an error. Ghostscript seems to be installed correctly. I can e.g. do the following:

C:\>"C:\Program Files (x86)\gs\gs8.71\bin\gswin32c.exe" -sDEVICE=bbox -dBATCH -d
NOPAUSE -c save pop -f Instructions-autopp.pdf
GPL Ghostscript 8.71 (2010-02-10)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
%%BoundingBox: 640 577 978 706
%%HiResBoundingBox: 640.053965 577.439982 977.374658 705.834189

What could I try?

UPDATE

Debug output:

C:\>perl pdfcrop.pl   --margins 10 --debug Instructions-autopp.pdf s.pdf
PDFCROP 1.37, 2012/10/16 - Copyright (c) 2002-2012 by Heiko Oberdiek.
* Restricted mode: disabled
* Option `pdfversion': auto
* Perl executable: C:\Perl64\bin\perl.exe
* Perl version: v5.10.1
* Perl product: ActivePerl, build 1007
* Pointer size: 8
* Pipe support: yes
* Fork support: no
* OS name: MSWin32
* Arch name: MSWin32-x64-multi-thread
* System: win
* Not found (gswin64c): gswin64c.exe
* Not found (gswin64c): C:\Perl64\site\bin\gswin64c.exe
* Not found (gswin64c): C:\Perl64\bin\gswin64c.exe
* Not found (gswin64c): C:\GTK\bin\gswin64c.exe
* Not found (gswin64c): C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\gswin64c.exe
* Not found (gswin64c): C:\Program Files (x86)\OpenVPN\bin\gswin64c.exe
* Not found (gswin64c): C:\Program Files (x86)\ispell\gswin64c.exe
* Not found (gswin64c): C:\Program Files (x86)\ispell\gswin64c.exe
* Not found (gswin32c): gswin32c.exe
* Not found (gswin32c): C:\Perl64\site\bin\gswin32c.exe
* Not found (gswin32c): C:\Perl64\bin\gswin32c.exe
* Not found (gswin32c): C:\GTK\bin\gswin32c.exe
* Not found (gswin32c): C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\gswin32c.exe
* Not found (gswin32c): C:\Program Files (x86)\OpenVPN\bin\gswin32c.exe
* Not found (gswin32c): C:\Program Files (x86)\ispell\gswin32c.exe
* Not found (gswin32c): C:\Program Files (x86)\ispell\gswin32c.exe
* Not found (gs): gs.exe
* Not found (gs): C:\Perl64\site\bin\gs.exe
* Not found (gs): C:\Perl64\bin\gs.exe
* Not found (gs): C:\GTK\bin\gs.exe
* Not found (gs): C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\gs.exe
* Not found (gs): C:\Program Files (x86)\OpenVPN\bin\gs.exe
* Not found (gs): C:\Program Files (x86)\ispell\gs.exe
* Not found (gs): C:\Program Files (x86)\ispell\gs.exe
* Search registry at `HKEY_LOCAL_MACHINE/SOFTWARE/'.
* Registry entry found: HKEY_LOCAL_MACHINE/SOFTWARE/GPL Ghostscript/
* Registry entry found: HKEY_LOCAL_MACHINE/SOFTWARE/GPL Ghostscript/9.06/
  GS_DLL = C:\Program Files\gs\gs9.06\bin\gsdll64.dll
  EXE found: C:\Program Files\gs\gs9.06\bin\gswin64c.exe
* Found (via registry): C:\Program Files\gs\gs9.06\bin\gswin64c.exe
* Autodetected ghostscript command: C:\Program Files\gs\gs9.06\bin\gswin64c.exe
* Input file: Instructions-autopp.pdf
* Output file: s.pdf
* Margins: 10 10 10 10
* PDF header: %PDF-1.4
* Using PDF minor version: 4
* Running ghostscript for BoundingBox calculation ...
* Ghostscript call: "C:\Program Files\gs\gs9.06\bin\gswin64c.exe" -sDEVICE=bbox -dBATCH     -dNOPAUSE -c
 save pop -f Instructions-autopp.pdf
* Cleanup
* Temporary files: tmp-pdfcrop-3284.tex
!!! Error: Cannot call ghostscript `"C:\Program Files\gs\gs9.06\bin\gswin64c.exe"' (Bad
file descriptor/Le chemin dÆaccÞs spÚcifiÚ est introuvable)!
* Cleanup
* Temporary files: tmp-pdfcrop-3284.tex 

Le chemin dÆaccÞs spÚcifiÚ est introuvable)! = The specified path can not be found.

UPDATE
The result of your script:

*** system() ***
GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
%%BoundingBox: 640 577 978 706
%%HiResBoundingBox: 640.053402 577.439982 977.375220 705.851978
* No error.

*** backticks ***
%%BoundingBox: 640 577 978 706
%%HiResBoundingBox: 640.053402 577.439982 977.375220 705.851978
* No error.
==========
GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
==========

*** pipe ***
[[GPL Ghostscript 9.06 (2012-08-08)]]
[[Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.]]
[[This software comes with NO WARRANTY: see the file PUBLIC for details.]]
[[Processing pages 1 through 1.]]
[[Page 1]]
%%BoundingBox: 640 577 978 706
%%HiResBoundingBox: 640.053402 577.439982 977.375220 705.851978
* No error.

*** with stderror redirection ***

*** system() ***
Can't spawn "cmd.exe": No such file or directory at test.pl line 26.
! Exit code: 255

*** backticks ***
! Execution error: Bad file descriptor/Le chemin dÆaccÞs spÚcifiÚ est introuvable
==========
Use of uninitialized value $result in print at test.pl line 33.
==========

*** pipe ***
! Error: Execution error: Bad file descriptor/Le chemin dÆaccÞs spÚcifiÚ est introuvable

*** with stderror and stdout redirection ***

*** system() ***
Can't spawn "cmd.exe": No such file or directory at test.pl line 26.
! Exit code: 255

*** backticks ***
! Execution error: Bad file descriptor/Le chemin dÆaccÞs spÚcifiÚ est introuvable
==========
Use of uninitialized value $result in print at test.pl line 33.
==========

*** pipe ***
! Error: Execution error: Bad file descriptor/Le chemin dÆaccÞs spÚcifiÚ est introuvable

The problem seems to be that cmd.exe can not be found.
I put c:\windows\system32 in system variable PATH and now everything works!

(I do not know why c:\windows\system32 was not present any more)

Many thanks for your help!

Best Answer

Perhaps there is a problem with redirection of standard/error output with your Perl version. Try updating. The current version is 5.16.1.1601.

Also I have written a test script that tries different execution methods with and without redirecting:

#!/usr/bin/env perl
use strict;
$^W=1;

use File::Spec::Functions qw(devnull);
my $null = devnull();

my $prg = '"C:\Program Files\gs\gs9.06\bin\gswin64c.exe"';
# $prg = '"C:\Program Files\gs\gs9.05\bin\gswin32c.exe"';

my $testfile = 'Instructions-autopp.pdf';
# $testfile = 'test.pdf';
my $cmdline =
  "$prg -sDEVICE=bbox -dBATCH -dNOPAUSE -c save pop -f $testfile";

sub analyze {
    if ($? == -1) { print "! Execution error: $!/$^E\n"; }
    elsif ($? & 127) { print sprintf "! Signal: %d\n", $? & 127; }
    elsif ($? != 0) { print sprintf "! Exit code: %d\n", $? >> 8; }
    else { print "* No error.\n"; }
}

sub test {
    print "\n*** system() ***\n";

    system($cmdline);
    analyze();

    print "\n*** backticks ***\n";
    my $result = `$cmdline`;
    analyze();
    print "==========\n";
    print $result;
    print "==========\n";

    print "\n*** pipe ***\n";
    my $ret = open(IN, "$cmdline |");
    if ($ret) {
        while (<IN>) {
            chomp;
            print "[[$_]]\n";
        }
        close(IN);
        analyze();
    }
    else {
        print "! Error: Execution error: $!/$^E\n";
    }
}

test();

print "\n*** with stderror redirection ***\n";
$cmdline .= " 2>&1";
test();

print "\n*** with stderror and stdout redirection ***\n";
$cmdline .= " 1>$null";
test();

1;
__END__

The result (example with win32/ActivePerl 16.0.1):

*** system() ***
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 2.
Page 1
%%BoundingBox: 72 696 341 767
%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977
Page 2
%%BoundingBox: 72 95 321 770
%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977
* No error.

*** backticks ***
%%BoundingBox: 72 696 341 767
%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977
%%BoundingBox: 72 95 321 770
%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977
* No error.
==========
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 2.
Page 1
Page 2
==========

*** pipe ***
[[GPL Ghostscript 9.05 (2012-02-08)]]
[[Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.]]
[[This software comes with NO WARRANTY: see the file PUBLIC for details.]]
[[Processing pages 1 through 2.]]
[[Page 1]]
%%BoundingBox: 72 696 341 767
%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977
[[Page 2]]
%%BoundingBox: 72 95 321 770
%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977
* No error.

*** with stderror redirection ***

*** system() ***
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 2.
Page 1
%%BoundingBox: 72 696 341 767
%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977
Page 2
%%BoundingBox: 72 95 321 770
%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977
* No error.

*** backticks ***
* No error.
==========
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 2.
Page 1
%%BoundingBox: 72 696 341 767
%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977
Page 2
%%BoundingBox: 72 95 321 770
%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977
==========

*** pipe ***
[[GPL Ghostscript 9.05 (2012-02-08)]]
[[Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.]]
[[This software comes with NO WARRANTY: see the file PUBLIC for details.]]
[[Processing pages 1 through 2.]]
[[Page 1]]
[[%%BoundingBox: 72 696 341 767]]
[[%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977]]
[[Page 2]]
[[%%BoundingBox: 72 95 321 770]]
[[%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977]]
* No error.

*** with stderror and stdout redirection ***

*** system() ***
%%BoundingBox: 72 696 341 767
%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977
%%BoundingBox: 72 95 321 770
%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977
* No error.

*** backticks ***
* No error.
==========
%%BoundingBox: 72 696 341 767
%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977
%%BoundingBox: 72 95 321 770
%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977
==========

*** pipe ***
[[%%BoundingBox: 72 696 341 767]]
[[%%HiResBoundingBox: 72.287998 696.401979 340.685990 766.925977]]
[[%%BoundingBox: 72 95 321 770]]
[[%%HiResBoundingBox: 72.503998 95.147997 320.093990 769.985977]]
* No error.