7

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

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
user2065631
  • 91
  • 1
  • 1
  • 5
  • 2
    While its nice to see gs can be made to do this, either tiff2pdf ( http://remotesensing.org/libtiff/ ) or tifftopnm,pnmtops (netpbm.sourceforge.net/) are likely perferable, more robust, faster solutions – agentp Mar 07 '13 at 16:22

6 Answers6

10

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 
ijoseph
  • 6,505
  • 4
  • 26
  • 26
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • 1
    tiff2pdf (0.40.0) is quite limited - it does not handle certain exotic TIFFs, in particular TIFFs that contain JPEG encoded samples. It silently corrupts the image and renders a valid PDF. Earlier versions are worse. – jbarlow Feb 09 '16 at 10:27
  • @jbarlow: My version of libtiff/tiff2pdf is 4.0.6 (but you are correct with pointing to its limitations -- there are limitations in *ANY* software you use; you have to find out yourself if it concerns your own practical use case by trying + testing it...) – Kurt Pfeifle Feb 09 '16 at 10:40
  • 1
    For Debian / Ubuntu etc the package is libtiff-tools – scoobydoo May 09 '17 at 15:45
9

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.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • 3
    Found a link (https://groups.google.com/d/msg/comp.lang.postscript/xLMTVNzGNXQ/g7gItvNwPEIJ) using the amusing search string "ken read tiff". Very nice job! +1 – luser droog Mar 06 '13 at 06:18
  • There are some bugs in that version of the code, if anyone is interested I'm happy to make the current version available. But please note there are known limitations with the implementation (see FIXMEs) and I can't provide any warranty or promise to support the program. Its also probably better to use some decent image application to do the job, this was (as I said in comp.lang.postscript) just an intellectual exercise. – KenS Mar 06 '13 at 08:11
  • Understood. But it's always nice to see professional-looking code, to compare one's own clay-sculptures against. :) – luser droog Mar 07 '13 at 06:19
  • @KenS HI,What i understand is like,first we need to convert tif to pdf using any postscript, and then with that pdf as input we need compress the pdf in ghostscript.Is that correct? – User09111993 Apr 03 '20 at 08:06
  • I'm sorry but I dont know what you are asking here. This question is about reading a TIFF file using Ghostscript, which you can do by writing a PostScript program (not 'any PostScript' but a PostScript program specifically written to do that job). Ghostscript doesn't 'compress' PDF files. – KenS Apr 03 '20 at 08:34
4

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.

matt wilkie
  • 17,268
  • 24
  • 80
  • 115
2

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...

Matt
  • 425
  • 3
  • 13
0

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);
HP371
  • 860
  • 11
  • 24
-5

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

NikhilK
  • 181
  • 2
  • 12