6
votes

Could you tell me how can I convert TIFF to PDF with using Ghostscript or Postscript?

I tried to use this command:

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

But it doesn't work.

It produces an 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.
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
While its nice to see gs can be made to do this, either tiff2pdf ( remotesensing.org/libtiff ) or tifftopnm,pnmtops (netpbm.sourceforge.net/) are likely perferable, more robust, faster solutionsagentp

6 Answers

8
votes

Ghostscript reads PDF and PostScript as input, it doesn't read image formats, and in particular doesn't read TIFF. However PostScript is a programming language, so it is entirely possible to write a PostScript program to read a TIFF file (the viewgif.ps and viewjpeg.ps program supplied with Ghostscript do this for GIF and JPEG formats)

I do have a program which does this, up to a point, and which has been posted on comp.lang.postscript a few times. Its kind of large to share here (33Kb) but I can email you a copy if you are interested.

8
votes

The libtiff software package (available on all major OS platforms) ships with a command line tool called 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

So a simple command to get your PDF would be:

$ tiff2pdf -o output.pdf -p A4 -F test.tif
4
votes

Use the gdal_translate utility. It's designed for geospatial raster imagery, but it doesn't care if it's just a regular image.

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

Additional info on the geo-pdf driver and it's options: http://www.gdal.org/frmt_pdf.html

The default compression applied is DEFLATE, which is good because it's lossless, but doesn't produce very small files. Usually using PREDICTOR and TILED options can increase compression (but not always, test with your data).

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

For smallest files use JPEG. For combined smallest and least lossy use JPEG2000, but test in client's pdf reader because support is not universal (recent Adobe Reader is fine).

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

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

-co is for brevity, it's interchangeable with --config. Upper case in first example is convention only, on the command line it doesn't matter. ^ is Windows character to suppress linefeed, omit when all in one line.

Otaining pre-built binaries: http://trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries

Figuring out which distribution package to use will be a bit of a pain if you don't know anything about Geographic Information Systems. If all you want is to stuff the program somewhere and run it, grab "compiled binaries in single zip" from GIS Internals, 2015-Jan 32bit stable release here, unpack and start from SDKShell.bat.

1
votes

I Convert the Tiff file to Pdf File by use Imagick

code :

$document = new Imagick(test.tiff); 

$document->setImageFormat("pdf");

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

The best tool I've used so far has been Irfanview. It allowed us to do a very obscure version of TIFF files, and also let us do mixed TIFF to PDF conversions where some pages where grayscale, some were color, and some were CCITT4 fax encoded.

I created a .NET program that converted millions of documents using Irfanview using this command line:

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

The .NET program essentially just passed in the full paths to the TIFF input and PDF output files.

Setup

Download Irfanview 32bit. Not all plugins are available for 64-bit version, so I used 32-bit.

Then download the plugin package for Irfanview and install it.

Then open Irfanview, and you need to define the settings for either the IMPdf Plugin (legacy 32-bit only plugin) or the newer PDF Plugin by going to File>>Batch Conversion/Rename.

The settings in the Batch Conversion/Rename window stay in effect for every subsequent execution of the program as they are stored in the Irfanview INI file.

Click Output Format, then choose PDF.

Then click the OPTIONS button. That will allow you to control parameters on what will happen to the input TIFF file when it is converted to PDF.

Then try irfanview to do it manually for a few TIFF files to ensure the output is desired.

Then you can automate it using a program...

-3
votes

I Convert the PDF file to Tiff File by Ghostscript with Java on ubuntu.
snippets code :

String convertCommand = "gs -dNOPAUSE -q -sDEVICE=tiff24nc -sCompression=lzw -dBATCH -sOutputFile=" + outputFile + " " + sourceFile;
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(convertCommand);
pr.waitFor();

If you want compression then simply replace command to,
String convertCommand = "gs -dNOPAUSE -q -sDEVICE=tifflzw -dBATCH -sOutputFile=" + outputFile + " " + sourceFile;

Please install Ghostscript before using it,
1. sudo apt-get install ghostscript libtiff-tools