0

GhostScript v9.10 64bit version

I'd like to take a list of jpgs and turn them into a single pdf file where each image has its own page.

I tried to follow the method used here: Using Ghostscript to convert JPEG to PDF but I'm getting errors.

Here is my command:

C:\Temp>gswin64c -sDEVICE=pdfwrite -o cafb0173-f4be-47a8-a39e-f479ca0a4d09.pdf viewjpeg.ps -c 5c520934-461a-44f3-9f00-9fdc9fe666c1.jpg

Here is what I get:

GPL Ghostscript 9.10 (2013-08-30)
Copyright (C) 2013 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /undefined in 5c520934-461a-44f3-9f00-9fdc9fe666c1.jpg
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_pus
h   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   .runexec2   --
nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--
Dictionary stack:
   --dict:1179/1684(ro)(G)--   --dict:0/20(G)--   --dict:80/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.10: Unrecoverable error, exit code 1

I've tried using the full path to viewjpeg.ps, and I've tried putting viewJPEG showpage at the end, and that didn't help either.

When I run this c# code, however:

     try
            {
                var guid = System.Guid.NewGuid().ToString();
                Process p = new Process();
                p.StartInfo.WorkingDirectory = @"C:\Temp";
                p.StartInfo.FileName = Program.ghostScriptLocation;
                p.StartInfo.Arguments = " -sDEVICE=pdfwrite -o " + guid + ".pdf " + "\""+Program.viewJPEGLoc+"\"" +" -c " + String.Join(" ", pictures.ToArray()); 
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();

                string outp = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
        }

I get a blank pdf file as well as an error saying "Unrecoverable error, exit code 1"

Community
  • 1
  • 1

1 Answers1

1

Following viewjpeg.ps you have put "-c" This switch means 'treat the following command line as PostScript, up until a -f is encountered". So Ghostscript then tries to parse "5c52......jpg" as PostScript operations. Since that is gibberish in PostScript, you get an error.

From the header of viewjpeg.ps:

% Usage example:
%   (jpeg-6/testimg.jpg) viewJPEG

So your command line should read:

C:\Temp>gswin64c -sDEVICE=pdfwrite -o cafb0173-f4be-47a8-a39e-f479ca0a4d09.pdf viewjpeg.ps -c "(5c520934-461a-44f3-9f00-9fdc9fe666c1.jpg) viewJPEG" -f

Update I rather felt that the problem was that you couldn't get viewJPEG to work. Once that is working, well, its not terribly hard to have viewjpeg operate on multiple files. Here's an example:

C:\Temp>gswin64c -sDEVICE=pdfwrite -o cafb0173-f4be-47a8-a39e-f479ca0a4d09.pdf viewjpeg.ps -c "(file1.jpg) viewJPEG (file2.jpg) viewJPEG (file3.jpg) viewJPEG" -f

Obviously you can expand the command line as required. Of course if you continue to use extremely long names for your JPEG files you'll probably run out of command line, so you will want to switch to using a command file (see the Ghostscript documentation) or put all the filenames in a file and write PostScript to pull the names from the file and then execute viewJPEG on each one.

This will, of course, stick all the JPEG images into one PDF file. If you don't like that then you'll have to use the '%d' syntax for the output file, so that each page goes to a separate numbered file, or you will have to write a script to invoke Ghostscript once for each file in your list.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • My question title mentions a list of files. How do I do this with more than one file? –  Oct 13 '14 at 16:54
  • Well, you could write a script to invoke Ghopstscript once for every file you want to process. Alternatively you can modify the viewjpeg.ps program and supply it with a number of files. For example instead of a string with a single filename you might supply an array of strings and have the viewjpeg.ps program iterate through the array. – KenS Oct 13 '14 at 17:55
  • So with stock viewjpeg.ps, it's impossible to wrap multiple jpg files into a single pdf? –  Oct 13 '14 at 18:12
  • Its a utility program intended to take one jpeg as an input. So no, you can't use it to input multiple jpegs. You can modify it however. – KenS Oct 14 '14 at 06:58
  • I don't think I'll accept this answer (as it is currently) since it doesn't answer my question of how to convert a *list* of jpgs to a powerpoint using ghostscript. –  Oct 14 '14 at 21:33
  • I tried implementing your solution for multiple images, and the resulting pdf ends up with only one page and looks the same as if I'd have only chosen one file –  Oct 15 '14 at 14:37
  • I feel you haven't really described very well what it is you actually want. Ghostscript won't do what you apparently want 'out of the box', I thought I'd been clear about that, so you will need to do 'some assembly' yourself. Try putting showpage after every viewJPEG, that'll put each JPEG on a new page. To be honest, since you are executing this from a C# program, it seems to me the easiest solution is simply to invoke Ghostscript once for every file in your list. I'm guessing you don't speak PostScript.... – KenS Oct 15 '14 at 14:58
  • Putting `showpage` after every viewJPEG gave me the result that I wanted –  Oct 15 '14 at 15:06