Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Faxes in your browser

by Willard B. Trophy (Hermit)
on Oct 21, 2002 at 00:29 UTC ( #206742=CUFP: print w/replies, xml ) Need Help??

I don't have space for a fax machine, and if I did, I could guarantee it would have no paper or be broken or something just when I need it.

Companies like (free for certain US & UK locations) and Protus Fax (commercial, Canada & US) provide virtual fax numbers which forward the fax page images on to you by e-mail. Unfortunately, these tend to be sent as TIFFs, which somehow the web missed out on supporting, despite them being the lingua franca of the prepress world. (It's something to do with the TIFF file format being a hideous knot that they're not supported. I work in prepress, I have to grok them. Bleah.).

Most PDF readers include full TIFF support, and integrate reasonably well with browsers. I'll leave the actual interfacing of the converted PDF to the reader, but the code follows. You'll need to install pdflib, which installs its not-very-Perlish support module in the right place.

#!/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;

And that's it. It just works. Yeah, you can do this with tiff2ps and gs's ps2pdf, but that's a lot of software overhead.

This also works with any TIFF I've ever met. I've met some odd TIFF flavours in my time.

If you need to archive the faxes, a useful trick is to convert them to G4 fax with tiffcp -c g4 -r5000 in.tif out.tif beforehand. This will make the resultant PDF much smaller. Of course, if you really want the files to be tiny, investigate DjVu, but that's not a Perl thing, so I'll be quiet now …

Update, July 11th 2003: The free version of PDFlib is now called PDFlib Lite, and it has a rather odd licence. I guess this use would be covered under their "royalty free" terms. Ideally, I'd like to shift this code to work under the more open PDF::API2 module.

foreach(split('',"\3\3\3c>\0>c\177cc\0~c~``\0cc\177cc")) {$_=unpack('B8',$_);tr,01,\40#,;print$_,"\n";}##IYDKINT!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://206742]
Front-paged by Aristotle
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-01-21 09:50 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.