Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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 eFax.com (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 <scruss@bigfoot.com> # 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', '<scruss@bigfoot.com>'); 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!

Comment on Faxes in your browser
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://206742]
Front-paged by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-28 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls