使用Ghostscript将jpg列表转换为pdf [英] Convert list of jpgs to pdf with ghostscript

查看:133
本文介绍了使用Ghostscript将jpg列表转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GhostScript v9.10 64位版本

GhostScript v9.10 64bit version

我想获取一张jpg列表,并将其转换为一个pdf文件,其中每个图像都有自己的页面.

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

我尝试遵循此处使用的方法:使用Ghostscript将JPEG转换为PDF ,但出现错误.

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

这是我的命令:

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

这就是我得到的:

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

我尝试使用完整路径查看viewjpeg.ps,并且尝试将 viewJPEG showpage 放在最后,但这也无济于事.

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.

但是,当我运行此C#代码时:

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);
        }

我得到一个空白的pdf文件,并显示一条错误消息无法恢复的错误,退出代码1".

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

推荐答案

在viewjpeg.ps后面放置"-c".此开关的意思是将以下命令行作为PostScript处理,直到遇到-f."然后,Ghostscript尝试将"5c52 ...... jpg"解析为PostScript操作.由于在PostScript中这是乱码,因此会出现错误.

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.

从viewjpeg.ps的标题中:

From the header of viewjpeg.ps:

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

因此您的命令行应该阅读:

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

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

更新我宁愿觉得问题在于您无法使ViewJPEG正常工作.一旦工作了,好了,让viewjpeg对多个文件进行操作并不难.这是一个示例:

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

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

很显然,您可以根据需要扩展命令行.当然,如果您继续为JPEG文件使用极长的名称,则可能会用完命令行,因此您将希望切换为使用命令文件(请参阅Ghostscript文档)或将所有文件名放在文件中,然后编写PostScript以从文件中提取名称,然后在每个文件上执行viewJPEG.

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.

这当然会将所有JPEG图像粘贴到一个PDF文件中.如果您不喜欢这样做,则必须对输出文件使用'%d'语法,以便将每个页面转到一个单独的编号文件,否则您将必须编写一个脚本来为每个脚本调用一次Ghostscript文件到您的列表中.

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.

这篇关于使用Ghostscript将jpg列表转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆