10

I tried to compress a pdf-file with the following command:

gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=test-compressed.pdf test.pdf

I am in the directory of test.pdf and I can also open it, but when I execute the command I get the following error, although the output file should be specified with -sOutputFile=test-compressed.pdf:

GPL Ghostscript 9.16: Device 'pdfwrite' requires an output file but no file was specified.
**** Unable to open the initial device, quitting.
MEVIS3000
  • 521
  • 3
  • 18

5 Answers5

19

I solved it myself: it doesn't work with PowerShell, whereas it works with cmd.exe .

ssube
  • 47,010
  • 7
  • 103
  • 140
MEVIS3000
  • 521
  • 3
  • 18
4

For me the following works in PowerShell:

gswin64c -sDEVICE=pdfwrite -o test_compressed.pdf -dCompatibilityLevel='1.4' -dNOPAUSE -dQUIET -dBATCH  test.pdf
Fabian
  • 1,100
  • 11
  • 14
3

For me, the following command works:

& "gswin64c" "-sDEVICE=pdfwrite" "-dCompatibilityLevel=1.4" "-dNOPAUSE"
"-dQUIET" "-dBATCH" "-sOutputFile=test-compressed.pdf" "test.pdf"
randers
  • 5,031
  • 5
  • 37
  • 64
2

Firstly, Ghostscript's pdfwrite device does not 'compress' PDF files. It produces new PDF files which should be visually identical with the input (whatever format the input is).

What its telling you is that you haven't specified an output file. So either the command line you gave above is not what you actually typed (this error is usually the result of a typo in the command line), or there is some other problem (eg the directory does not exist or you do not have permission to create a file there)

KenS
  • 30,202
  • 3
  • 34
  • 51
-1

To compress PDF and reduce file size, set PDFSETTINGS to screen:

gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

However, the output quality may deteriorate.

For better quality, consider convert to ps first, and then back to pdf. Sample .ps1 script file:

if (!$env:Path.Contains("gs\gs10.00.0")) { 
    $env:Path += ";C:\Program Files\gs\gs10.00.0\bin;C:\Program Files\gs\gs10.00.0\lib"
}
$source = "J:\temp\output\large.pdf"
$ps = "J:\temp\output\temp.ps"
$dest = "J:\temp\output\small.pdf"
pdf2ps $source $ps
ps2pdf $ps $dest
kiatng
  • 2,847
  • 32
  • 39