HylaFAX The world's most advanced open source fax server

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

[hylafax-users] Printing to multiple paper sizes



I posted a few days ago asking about how to make sure that faxes containing multiple paper sizes were printed correctly. Never got an answer so I had to get creative. The attached perl script, printfax.pl uses ImageMagick to do provide a couple of printing options. if the $mode variable is set to "resize" then the fax is run through ImageMagick's convert utility to resize all pages in the multi-page TIFF file to letter size (8.5x11"). It uses a 2-pass resize command to compensate for the fact that faxes do not use square pixels. The first pass doubles the number of pixels in the length to correct the aspect ratio then the second pass does the actual resizing of the image.

The second option the script provides is the "noresize" option. Using this option causes the script to first correct for the squashed pixels, then it splits the multi-page tiff out into individual pages. It uses ImageMagick's "Identify" routine to check the length of each page. If the page is greater than a set number of lines (somewhere between 2200-2300 for letter) then it sets a variable "$paper_size" with "legal" or if the page will fit on a letter size page then it will set the $paper_size to "letter".Once the variable is set the file is converted to postscript with the tiff2ps program and piped to lpr where the $paper_size is passed as an option. If your printer has multiple paper trays and multiple paper sizes available passing this option should allow the printer to pick the correct paper tray.

The script, as it is, has been tested to work both ways with my setup which has a server running Hylafax+ 5.2.7 sending all incoming faxes to a networked HP LaserJet 4000N. Using the resize option all of our faxes are printed correctly on letter size paper and using the noresize option, the printer selects legal size when necessary.

Hope this helps someone else.

Good luck,
Brent
#!/usr/bin/perl
##########################################################################
# printfax by Brent Davidson Copyright 2008
# This file is licensed under the terms of the GPL
# This script is an add-on for Hylafax that automatically
# resizes all pages to fit on a single paper size or 
# checks each page of a fax and applys an option to lpr
# that specifies the paper size.
# This program requires ImageMagick
#########################################################################

use strict;
use warnings;
use File::Glob qw(:globally :nocase);

my $file = $ARGV[0];

my $mode="resize";  #Set to noresize to print on legal or letter sized paper


my $printer = 'Fax01';  # Set the name of the printer you wish to print to here.

if ($mode eq 'resize') {
  resize ($file);
}elsif ($mode eq 'noresize') {
  noresize ($file);
}
  


sub resize {
  my $file = shift @_;
  my ($fname,$ext) = split (/\./,$file);
  my $newfile = $fname.'_resized.tif';
 
  # The line below does the resizing.  The -resize x2100 tells convert
  # to resize the image to be only 2100 lines long and only if they
  # are larger than 2100 lines long.  At 200DPI this is
  # just short of 11 inches, which is letter size paper.  Using only a
  # height parameter to resize will maintain aspect ratio on the resize.
  # The "-monochrome -compress group 4" makse sure we keep a B&W TIFF
  # file with CCITT Group4 compression to keep tiff2ps happy.
  # if you wanted to fit everything to A4 size paper you would change the
  # x2100 to x2300 (2338 would be full A4 size.  2300 leaves a small
  # margin so the printer won't crop the page.) The first -resize x200%
  # is to correct for the non-square pixels used by fax machines.
  # See the documentation for ImageMagick for further options to the
  # convert command.
  system ("/usr/bin/convert $file -resize x200%! -resize x2100 -monochrome -density 204x196 $newfile");

  my $psfile = $fname.'_resized.ps';
  system ("tiff2ps -a $newfile > $psfile");
  unlink ($newfile);
  chmod (0666,$psfile);
  system ("lpr -P $printer -r -o media=\"letter\" $psfile");
  # I chose to run lpr against the ps file rather than pipe
  # the tiff2ps output to lpr's STDIN so the printer
  # would display the name of the file it is currently printing
}

sub noresize {
  my $file = shift @_;
  my ($fname,$ext) = split (/\./,$file);
  my $cnvformat = $fname."_"."%02d".".tif";
  # This line splits the tif file into individual pages
  system ("convert $file -resize x200%! -monochrome -compress group4 $cnvformat");

  my $globspec = $fname."_"."*.tif";
  foreach my $page (glob ($globspec)) {
    my $size = `identify -format "%w %h" "$page"`;
    chomp $size;
    my ($width,$height) = split (' ',$size);
    my $paper_size;

    # We check the height based on 200DPI x 11 inches + a small margin for error.
    # If you need more/other paper sizes you can chain in more if statement or
    # change the comparison number.  For example an A4 wpage ould be 2338 lines
    # so you could change the line below to something like $height > 2400 to 
    # check for A4 size paper.
    # You will also need to change the value assigned to $paper_size.  See the
    # man page for lpr to see what sizes are valid.
    if ($height > 2300) {
      $paper_size = "legal";
    }else {
      $paper_size = "letter";
    }
    system ("tiff2ps $page | lpr -P $printer -o media=$paper_size");
    unlink ($page);
  }
}



Project hosted by iFAX Solutions