#!/usr/bin/perl -w

#
# this script requires tiffcp and tiff2pdf from the libtiff-tools package.
#
# starting from here:
#
# http://www.uspto.gov/patft/index.html
#
# perform a search... on the pages of any particular patent or patent
# application you'll find an "images" link.  copy that images link url (it's
# huge and ugly) and supply it as the argument to this script.  the script
# should be able to find the document id, the number of pages, and be able to
# download each page as a tiff file, and then concatenate them into a pdf.
#
# the script creates temp files named $docid-$page.tiff, $docid.tiff, and the
# result file is $docid.pdf ... all in the current directory.  if everything
# goes well it will clean out the tiff files afterwards.
#
# for example, the infamous RSA patent is number 4,405,829, and as of this
# writing, the following painfully long command will grab its pdf:
#
# patent2pdf 'http://patimg1.uspto.gov/.piw?Docid=04405829&homeurl=http%3A%2F%2Fpatft.uspto.gov%2Fnetacgi%2Fnph-Parser%3FSect1%3DPTO1%2526Sect2%3DHITOFF%2526d%3DPALL%2526p%3D1%2526u%3D%2Fnetahtml%2Fsrchnum.htm%2526r%3D1%2526f%3DG%2526l%3D50%2526s1%3D4,405,829.WKU.%2526OS%3DPN%2F4,405,829%2526RS%3DPN%2F4,405,829&PageNum=&Rtype=&SectionNum=&idkey=E41AD8B26268'
#

# Copyright (c) 2005 Dean Gaudet <deanatarctic.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# $Id: patent2pdf,v 1.3 2005/07/10 07:11:33 dean Exp $

use strict;

my $url = shift or die "usage: $0 `getpatenturl patentnumber`\n";

my ($host, $docid) = $url =~ m#^(http://[^/]+)/.*Docid=(\d+)#
	or die "unable to parse Docid from url\n";

require LWP::UserAgent;
require HTTP::Request;

my $ua = LWP::UserAgent->new;
my $request = HTTP::Request->new(GET => $url);
my $response = $ua->request($request);

unless ($response->is_success) {
	print STDERR $response->status_line . "\n";
	exit 1;
}

my ($pages) = $response->content =~ m#<b>\d+\s+of\s+(\d+) pages</b>#
	or die "unable to parse the number of pages from response\n";

my ($src) = $response->content =~ m#<embed src="([^"]+)" [^>]*type=image/tiff>#
	or die "unable to parse the embed url from response\n";

select(STDOUT); $| = 1;
print "$docid has $pages pages:";
my @cp_args = ();
for my $i (1..$pages) {
	print " $i";
	my $page_url = $src;
	$page_url =~ s#PageNum=\d+#PageNum=$i#;
	$request = HTTP::Request->new(GET => "$host$page_url");
	$response = $ua->request($request);
	unless ($response->is_success) {
		print STDERR "error on page $i: " . $response->status_line . "\n";
		exit 1;
	}
	my $f = "/tmp/$docid-$i.tiff";
	push(@cp_args, $f);
	open(F, ">$f") or die "unable to open $f for writing: $!\n";
	print F $response->content;
	close(F);
}
print "\n";
push(@cp_args, "/tmp/$docid.tiff");

system("tiffcp", @cp_args) == 0 or die "tiffcp error: $?\n";
system("convert", "/tmp/$docid.tiff", "$docid.pdf") == 0 or die "convert error: $?\n";
unlink(@cp_args) or warn "unable to delete temporary files: $!\n";
