#!/usr/bin/perl -w # fax2pdf - convert multipage TIFF fax to PDF # # usage: # fax2pdf file.tif # # written by Stewart C. Russell # but with so much code lifted from the PDFlib manual # that PDFlib GmbH and Thomas Merz should get some credit use strict; use pdflib_pl 3.03; my $file = shift; # get $1 my $outfile = $file; $outfile =~ s/tiff*$/pdf/; # make output file name die "Output file $file same as input file.\n" if ($outfile eq $file); my $p = PDF_new; # create new PDF object die "Couldn't open PDF file.\n" if (PDF_open_file($p, $outfile) == -1); # try to open PDF file # set some info fields; feel free to change these PDF_set_info($p, 'Creator', "$0"); PDF_set_info($p, 'Author', ''); PDF_set_info($p, 'Title', "Converted TIFF file \"$file\""); for (my $frame = 1; ; $frame++) { # for each page in TIFF my $image = PDF_open_image_file($p, 'tiff', $file, 'page', $frame); # open page in file last if ($image == -1); # exit loop if last page # query the dpi values my $dpi_x =PDF_get_value($p, 'resx', $image); my $dpi_y =PDF_get_value($p, 'resy', $image); # calculate scaling factors from the dpi values # see description of resx/resy in PDFlib manual my $scale_x = 1; my $scale_y = 1; if ($dpi_x > 0 && $dpi_y > 0) { $scale_x = 72 / $dpi_x; $scale_y = 72 / $dpi_y; } elsif ($dpi_x < 0 && $dpi_y < 0) { $scale_x = 1; $scale_y = $dpi_y / $dpi_x; } else { $scale_x = 1; $scale_y = 1; } # get image width and height my $width = PDF_get_value($p, 'imagewidth', $image); my $height = PDF_get_value($p, 'imageheight', $image); # open new PDF page scaled to the size of the image PDF_begin_page($p, $width * $scale_x, $height * $scale_y); # scale the image according to its resolution PDF_scale($p, $scale_x, $scale_y); PDF_place_image($p, $image, 0, 0, 1); PDF_close_image($p, $image); # close the image file PDF_end_page($p); # close the page } PDF_close($p); # close PDF file PDF_delete($p); # destroy PDF object exit;