使用Ghostscript将TIFF转换为PDF

时间:2013-03-04 21:25:10

标签: pdf tiff ghostscript postscript

您能告诉我如何使用Ghostscript或Postscript将TIFF转换为PDF?

我尝试使用此命令:

gswin32c.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=o.pdf test.tif

但它不起作用。

产生错误:

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.
Error: /undefined in II*
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1910   1   3   %oparray_pop   1909   1   3   %oparray_pop   1893   1   3   %oparray_pop   1787   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--
Dictionary stack:
   --dict:1169/1684(ro)(G)--   --dict:0/20(G)--   --dict:77/200(L)--
Current allocation mode is local
Current file position is 4
GPL Ghostscript 9.06: Unrecoverable error, exit code 1

6 个答案:

答案 0 :(得分:7)

Ghostscript将PDF和PostScript作为输入读取,它不读取图像格式,特别是不读取TIFF。但是PostScript是一种编程语言,因此完全可以编写PostScript程序来读取TIFF文件(Ghostscript提供的viewgif.ps和viewjpeg.ps程序为GIF和JPEG格式执行此操作)

我确实有一个程序可以执行此操作,直到某个点,并且已经在comp.lang.postscript上发布了几次。它在这里很大(33Kb),但如果你有兴趣,我可以给你发一份电子邮件。

答案 1 :(得分:4)

libtiff软件包(适用于所有主要操作系统平台)附带一个名为tiff2pdf的命令行工具。

$ tiff2pdf -h
  LIBTIFF, Version 4.0.3
  Copyright (c) 1988-1996 Sam Leffler
  Copyright (c) 1991-1996 Silicon Graphics, Inc.

  usage:  tiff2pdf [options] input.tiff
  options:
   -o: output to file name
   -j: compress with JPEG
   -z: compress with Zip/Deflate
   -q: compression quality
   -n: no compressed data passthrough
   -d: do not compress (decompress)
   -i: invert colors
   -u: set distance unit, 'i' for inch, 'm' for centimeter
   -x: set x resolution default in dots per unit
   -y: set y resolution default in dots per unit
   -w: width in units
   -l: length in units
   -r: 'd' for resolution default, 'o' for resolution override
   -p: paper size, eg "letter", "legal", "A4"
   -F: make the tiff fill the PDF page
   -f: set PDF "Fit Window" user preference
   -e: date, overrides image or current date/time default, YYYYMMDDHHMMSS
   -c: sets document creator, overrides image software default
   -a: sets document author, overrides image artist default
   -t: sets document title, overrides image document name default
   -s: sets document subject, overrides image image description default 
   -k: sets document keywords
   -b: set PDF "Interpolate" user preference
   -h: usage

所以获取PDF的简单命令是:

tiff2pdf -o output.pdf -p A4 -F test.tif

答案 2 :(得分:2)

使用gdal_translate实用程序。它专为地理空间光栅图像而设计,但它并不关心它是否只是一个普通的图像。

gdal_translate -of pdf \path\to\someimage.tif test.pdf

有关geo-pdf驱动程序及其选项的其他信息: http://www.gdal.org/frmt_pdf.html

应用的默认压缩是DEFLATE,这很好,因为它无损,但不会产生非常小的文件。通常使用PREDICTOR和TILED选项可以增加压缩(但并非总是如此,使用您的数据进行测试)。

gdal_translate -of pdf ^
--config COMPRESS=DEFLATE --config PREDICTOR=2 --config TILED=YES ^
in.tif deflate.pdf

对于最小的文件,请使用JPEG。对于组合最小和最少有损使用JPEG2000,但在客户端的pdf阅读器中进行测试,因为支持不是通用的(最近的Adobe Reader很好)。

gdal_translate -of pdf -co compress=jpeg -co jpeg_quality=85 ^
inimage.tif outdoc.pdf

gdal_translate -of pdf -co compress=jpeg2000 ...

-co为简洁起见,可与--config互换。 第一个示例中的大写仅是约定,在命令行上它并不重要。 ^是用于抑制换行的Windows字符,在一行中省略。

获取预先构建的二进制文件: http://trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries

如果您对地理信息系统一无所知,找出使用哪个分发包会有点痛苦。如果你想要的只是将程序填入某个地方并运行它,那么从GIS Internals,2015-Jan 32bit稳定版here中获取"单个zip" 中的编译二进制文件,解压缩并从SDKShell.bat开始。

答案 3 :(得分:1)

我使用Imagick

将Tiff文件转换为Pdf文件

代码:

$document = new Imagick(test.tiff); 

$document->setImageFormat("pdf");

$document->writeImages("test.pdf", true);

答案 4 :(得分:0)

到目前为止,我使用过的最好的工具是Irfanview。它使我们能够制作非常模糊的TIFF文件版本,还使我们能够进行TIFF到PDF的混合转换,其中有些页面是灰度的,有些是彩色的,有些是CCITT4传真编码的。

我创建了一个.NET程序,该程序使用以下命令行使用Irfanview转换了数百万个文档:

C:\ Program Files(x86)\ IrfanView \ i_view32.exe TIFFFile.tiff /convert=OutputPDFFile.pdf / silent / cmdexit

.NET程序实际上只是在完整路径中传递到TIFF输入和PDF输出文件。

设置

下载Irfanview 32位。并非所有插件都支持64位版本,因此我使用了32位。

然后下载Irfanview的插件包并安装。

然后打开Irfanview,您需要通过转到文件>>批量转换/重命名来定义IMPdf插件(仅旧版32位插件)或较新的PDF插件的设置。

“批量转换/重命名”窗口中的设置对于程序的每次后续执行均有效,因为它们存储在Irfanview INI文件中。

点击输出格式,然后选择PDF。

然后单击“选项”按钮。这样一来,您就可以控制将输入TIFF文件转换为PDF时会发生什么情况的参数。

然后尝试irfanview手动处理几个TIFF文件,以确保需要输出。

然后您可以使用程序将其自动化...

答案 5 :(得分:-2)

我在ubuntu上使用Java将 PDF文件转换为Tiff文件 Ghostscript
片段代码:

String convertCommand =“gs -dNOPAUSE -q -sDEVICE = tiff24nc -sCompression = lzw -dBATCH -sOutputFile =”+ outputFile +“”+ sourceFile;
运行时rt = Runtime.getRuntime();
处理pr = rt.exec(convertCommand);
pr.waitFor();

如果您想要压缩,则只需将命令替换为
String convertCommand =“gs -dNOPAUSE -q -sDEVICE = tifflzw -dBATCH -sOutputFile =”+ outputFile +“”+ sourceFile;

请在使用之前安装Ghostscript,
1. sudo apt-get install ghostscript libtiff-tools