TeX-AutoTeX

 view release on metacpan or  search on metacpan

lib/TeX/AutoTeX/StampPDF.pm  view on Meta::CPAN

package TeX::AutoTeX::StampPDF;

#
# $Id: StampPDF.pm,v 1.9.2.7 2011/01/27 18:42:29 thorstens Exp $
# $Revision: 1.9.2.7 $
# $Source: /cvsroot/arxivlib/arXivLib/lib/TeX/AutoTeX/StampPDF.pm,v $
#
# $Date: 2011/01/27 18:42:29 $
# $Author: thorstens $
#

use strict;
### use warnings;
use Carp qw(cluck carp);

our ($VERSION) = '$Revision: 1.9.2.7 $' =~ m{ \$Revision: \s+ (\S+) }x;

use parent qw(Exporter);
our @EXPORT_OK = qw(stamp_pdf);
use CAM::PDF;

sub stamp_pdf {
  my ($file, $stampref) = @_;

  if (!(-r $file && ref $stampref eq 'ARRAY')) {
    #FIXME: pod2usage;
    throw TeX::AutoTeX::FatalException "must provide pdf file and stamp contents\n";
  }

  # 6 * 72 - 1/2 * stringlength * 10
  # (10 = approx average char width in 20pt Times-Roman)
  my $yoffset = 432 - 5 * length $stampref->[0];

# minimal arXiv stamp used as a page overlay in grayscale
  my $pdfstamp = <<"EOSTAMP";
q
0.5 G 0.5 g
BT
/arXivStAmP 20 Tf 0 1 -1 0 32 $yoffset Tm
($stampref->[0])Tj
ET
Q
EOSTAMP

  rename($file, "$file.bk") || throw TeX::AutoTeX::FatalException q{couldn't backup PDF file};

  eval {
    my $doc = CAM::PDF->new("$file.bk") || cluck "$CAM::PDF::errstr\n";
    if (!$doc->canModify()) {
      throw TeX::AutoTeX::FatalException "This PDF forbids modification\n";
    }

    $doc->appendPageContent(1, $pdfstamp);
    $doc->addFont(1, 'Times-Roman', 'arXivStAmP') || cluck "$CAM::PDF::errstr\n";
    $doc->preserveOrder();
    $doc->cleanoutput($file) || cluck "$CAM::PDF::errstr\n";
    1;
  } or do {
    rename("$file.bk", $file) || carp 'woe is me, now that failed';
    throw TeX::AutoTeX::FatalException "stamp operation did not complete\n";
  };
  if ($@) {
    rename("$file.bk", $file) || carp 'woe is me, now that failed';
    throw TeX::AutoTeX::FatalException "an error occurred during stamp operation, reverted to original file\n";
  }
  unlink "$file.bk";
  return 0;
}

1;

__END__

=for stopwords PDF PDF-only arXiv arxiv.org pdflatex FontSize writeable euid www-admin Schwander arxiv perlartistic

=head1 NAME

TeX::AutoTeX::StampPDF - watermark PDF files

=head1 VERSION

This documentation refers to TeX::AutoTeX::StampPDF version 1.9.2.5

=head1 SYNOPSIS

use TeX::AutoTeX::StampPDF qw(stamp_pdf);

stamp_pdf($pdffile, $array_ref);

=head1 DESCRIPTION

This module modifies a given PDF file. It prints an arbitrary text string
(within certain length limits) onto the left edge of the 1st page of a well
formed PDF file.



( run in 2.131 seconds using v1.01-cache-2.11-cpan-7fcb06a456a )