-
-
Save nanis/0f15bffc9b31df57069f to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl | |
=for comment | |
Office of the Unites States Trade Representative released the full text of the | |
Trans-Pacific Partnership trade agreement on November 5, 2015. For some reason, | |
it was released in 239 individual PDF documents, instead of a single file. | |
ToC at https://ustr.gov/trade-agreements/free-trade-agreements/trans-pacific-partnership/tpp-full-text | |
This script downloads the individual parts in order, and assembles them into a | |
single document. | |
It sleeps a few seconds between requests so as to avoid hammering the server too | |
hard. If you decide to use this to download your own copy of the text, do keep the | |
sleep statement. | |
=cut | |
use utf8; | |
use strict; | |
use warnings; | |
use HTML::TokeParser::Simple; | |
use PDF::Reuse; | |
run('tpp-full-text'); | |
sub run { | |
my $toc = shift; | |
my $parser = HTML::TokeParser::Simple->new(file => $toc); | |
my $partno; | |
my @files; | |
while (my $anchor = $parser->get_tag('a')) { | |
my $href = $anchor->get_attr('href'); | |
next unless $href; | |
next unless $href =~ / [.] pdf \z/xi; | |
my $file = sprintf '%03d-%s', ++$partno, (split qr{/}, $href)[-1]; | |
if (-e $file) { | |
push @files, $file; | |
next; | |
} | |
my @cmd = (wget => $href => '-O' => $file); | |
warn "@cmd"; | |
system @cmd; | |
if (-e $file) { | |
push @files, $file; | |
} | |
my $duration = int(1 + rand(7)); | |
warn "Sleeping $duration seconds\n"; | |
sleep $duration; | |
} | |
print "TPP has $partno parts\n"; | |
printf "Successfully downloaded %d of them\n", scalar @files; | |
print "Combining them into a single document\n"; | |
prFile('TPP.pdf'); | |
prDoc($_) for @files; | |
prEnd(); | |
return; | |
} | |
=for LICENSE | |
The MIT License (MIT) | |
Copyright © 2015 A. Sinan Unur | |
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. | |
=cut |
It turns out, some of the PDFs on the US Trade Representative's page, and all available on NZ's TPP site, have disabled page extraction. In addition, GraphicsMagick's gm convert
chokes on both sets of PDFs with
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Adobe PDF Library 11.0 <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
and similar error messages. On the other hand, ImageMagick's convert
is able to extract individual pages intact at least from the U.S. set (I haven't tried the NZ set). It will take a while, but you should then be able to combine all extracted pages into a single document.
You can use this example to figure out how to extract individual pages from a PDF document in PDF format using ImageMagick's convert
. But, then, you lose the text information.
Extracting using gs
leaves all of that intact. You can then combine those files using PDF::Reuse
.
See "Download all the pieces of the full text of TPP, and combine them into a single PDF document" on my blog for more details.
TPP Table of Contents available at https://ustr.gov/trade-agreements/free-trade-agreements/trans-pacific-partnership/tpp-full-text