How to trim PDF margins and edit metadata

I often download academic articles as PDFs to read later. I regularly find two really annoying problems:

  1. Huge margins make the PDF nearly unreadable on my Kindle Fire
  2. Title and authors are missing from PDF metadata, making them harder to find later via search

Today, I found an answer to the first and wrote an answer to the second.

For fixing huge margins, I found the free briss tool, which shows a composite image of all odd and even pages and lets you set a crop box around them.

briss in action cropping PDF margins

briss in action cropping PDF margins

To fix the metadata, I did a bit of Googling for tools, then realized I could whip up a tool with Perl and PDF::API2 faster than hunting for an existing one.

This program reads PDF metadata, opens an editor with the data in JSON format, and takes the result and saves it to a new PDF.

#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;

use JSON::MaybeXS;
use PDF::API2;
use Path::Tiny;

die "Usage: $0 <infile> <outfile>\n" unless @ARGV == 2;

my ( $infile, $outfile ) = @ARGV;

unless ( $infile || -r $infile ) {
    die "Input file '$infile' can't be read\n";
}

my $pdf = PDF::API2->open($infile);
my $json = JSON::MaybeXS->new( utf8 => 1, pretty => 1 );
my $temp = Path::Tiny->tempfile;
$temp->spew( $json->encode( { $pdf->info } ) );

if ( $ENV{EDITOR} ) {
    system( $ENV{EDITOR}, $temp )
      and die "Error editing temp file: $!\n";
}
else {
    die "No EDITOR environment variable set.\n";
}

$pdf->info( %{ $json->decode( $temp->slurp ) } );
$pdf->saveas($outfile);
Posted in hacks, perl programming | Tagged , , , | Comments closed

MongoDB v1.0.0 released

About to ship v1.0.0
No more release candidates! Earlier this week, with great joy, I shipped MongoDB v1.0.0 to CPAN.

I wrote an article for the MongoDB websiteIntroducing the 1.0 Perl Driver

Also, if you use MongoDB with Perl, I'd greatly appreciate if you could give it a "++" click on MetaCPAN.

Posted in mongodb, perl programming | Tagged , , , | Comments closed

MongoDB Perl driver v1.0.0 RC 3 available – last chance to test

Today, I released the MongoDB Perl Driver v1.0.0 RC3 to CPAN as MongoDB-v0.999.999.6-TRIAL. It has only a handful of small changes, so might be your last chance to test a release candidate before a stable release is shipped.

I've blogged about RC1 and RC2 already, so I don't have much more to add.

As mentioned earlier, please read the MongoDB::Upgrading document, which describes the major changes from the "v0" series to v1.0.0.

Testing the release candidate

You can install RC3 from CPAN like this:

$ cpan MONGODB/MongoDB-v0.999.999.6-TRIAL.tar.gz

If you encounter problems or have questions, please open tickets on the MongoDB Perl driver bug tracker.

Barring any show-stoppers or urgent changes, the stable v1.0.0 MongoDB Perl driver will be shipped to CPAN around the end of August.

Posted in mongodb, perl programming | Tagged , , , | Comments closed

MongoDB Perl driver v1.0.0 RC 2 available – please test!

Yesterday, I released the MongoDB Perl Driver v1.0.0 RC2 to CPAN as MongoDB-v0.999.999.5-TRIAL. As I mentioned in my RC1 announcement last week that summarized the rationale and changes, everyone who uses MongoDB is strongly encouraged to download it and test it with their code.

In particular, please read the MongoDB::Upgrading document, which describes the major changes from the "v0" series to v1.0.0. Here is the table of contents so you have a sense of what you'll get from it:

  • INSTALLATION AND DEPENDENCY CHANGES
    • Moo instead of Moose
    • SSL and SASL
  • BEHAVIOR CHANGES
    • MongoClient configuration
    • Lazy connections and reconnections on demand
    • Exceptions are the preferred error handling approach
    • Cursors and query responses
    • Aggregation API
    • Read preference objects and the read_preference method
    • Write concern objects and removing the safe argument
    • Authentication based only on configuration options
    • Bulk API
    • GridFS
    • Low-level functions removed
    • MongoDB::Connection removed
    • BSON encoding changes
    • DBRef objects
  • DEPRECATED METHODS

Testing the release candidate

You can install RC2 from CPAN like this:

$ cpan MONGODB/MongoDB-v0.999.999.5-TRIAL.tar.gz

If you encounter problems or have questions, please open tickets on the MongoDB Perl driver bug tracker.

Barring any show-stoppers or urgent changes, the stable v1.0.0 MongoDB Perl driver will be shipped to CPAN around the end of August.

Posted in mongodb, perl programming | Tagged , , , | Comments closed

Review a git branch in the terminal

When I review a pull-request with many commits, I want to be able to see all the commits and step back and forth through them. I used to use a git GUI (or github's web pages), but I hated having to leave the terminal to do that.

Today I whipped up a Perl program to stage all the commit information into tempfiles and fire up vim on them. I saved it in my bin path as 'git-review', so I can call it as "git review". No promises of quality, but I'm sharing it in case anyone wants to steal/adapt it.

#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
use Path::Tiny;
use Capture::Tiny qw/capture_stdout/;

my ( $branch, $trunk ) = @ARGV;

die "Usage: $0 <branch> [trunk]"
  unless $branch;

$trunk //= 'master';

my $fork_point = qx{git merge-base --fork-point $trunk $branch};
chomp $fork_point;

die "Couldn't locate fork point for $branch from $trunk"
  unless $fork_point;

my @commits = map { chomp; $_ } qx/git rev-list $fork_point..$branch/;

my $tempdir = Path::Tiny->tempdir;

my @files;
for my $c (@commits) {
    push @files, my $file = $tempdir->child($c);
    $file->spew( scalar capture_stdout { system( qw/git show -p --stat/, $c ) } );
}

my $index = $tempdir->child("log");
$index->spew( scalar capture_stdout { system( qw/git log/, q[--pretty=%h %s%d], "$fork_point..$branch" ) } );
$index->append( "\ngit diff --stat\n", scalar capture_stdout { system( 'git', 'diff', '--stat', "$fork_point..$branch" ) } );

system( "vim", $index, reverse @files );

Now, since I use ingy's git-hub command line tool, my pull-request review is as easy as this:

$ git hub pr-fetch 27
$ git review PR/27
Posted in git, perl programming | Tagged , , , , | Comments closed

© 2009-2016 David Golden All Rights Reserved