The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DateTime::Format::PDF - PDF DateTime Parser and Formatter.

SYNOPSIS

 use DateTime::Format::PDF;

 my $obj = DateTime::Format::PDF->new;
 my $dt = $obj->parse_datetime($pdf_date);
 my $pdf_date = $obj->format_datetime($dt);

DESCRIPTION

This module understands the formats used by PDF file. It can be used to parse these formats in order to create DateTime objects, and it can take a DateTime object and produce a string representing it in a format accepted by PDF.

METHODS

new

 my $obj = DateTime::Format::PDF->new(%params);

Constructor.

Returns instance of object.

parse_datetime

 my $dt = $obj->parse_datetime($pdf_date);

Parse PDF datetime string.

Possible valid strings:

D:YYYY
D:YYYYMM
D:YYYYMMDD
D:YYYYMMDDHH
D:YYYYMMDDHHmm
D:YYYYMMDDHHmmSS
D:YYYYMMDDHHmmSSZ
D:YYYYMMDDHHmmSSOHHmm
D:YYYYMMDDHHmmSSOHH'mm
D:YYYYMMDDHHmmSSOHH'mm'
D:YYYYMMDDHHmmSSOHHmm'

Returns DateTime object.

format_datetime

 my $pdf_date = $obj->format_datetime($dt);

Format DateTime object to PDF datetime string. Output value is D:YYYYMMDDHHmmSSOHHmm.

Returns string.

ERRORS

 format_datetime():
         Bad DateTime object.
                 Value: %s

 parse_datetime():
         Invalid date format: %s

EXAMPLE1

 use strict;
 use warnings;

 use DateTime::Format::PDF;

 # Object.
 my $obj = DateTime::Format::PDF->new;

 # Parse date.
 my $dt = $obj->parse_datetime("D:20240401084337-01'30");

 # Print out.
 print $dt->strftime("%a, %d %b %Y %H:%M:%S %z")."\n";

 # Output like:
 # Mon, 01 Apr 2024 08:43:37 -0130

EXAMPLE2

 use strict;
 use warnings;

 use DateTime;
 use DateTime::Format::PDF;

 # Object.
 my $obj = DateTime::Format::PDF->new;

 # Example date.
 my $dt = DateTime->now;

 # Format.
 my $pdf_date = $obj->format_datetime($dt);

 # Print out.
 print "PDF date: $pdf_date\n";

 # Output like:
 # PDF date: D:20240401084337+0000

EXAMPLE3

 use strict;
 use warnings;

 use DateTime::Format::PDF;
 use PDF::Builder;

 if (@ARGV < 1) {
         print STDERR "Usage: $0 pdf_file\n";
         exit 1;
 }
 my $pdf_file = $ARGV[0];

 # Open file.
 my $pdf = PDF::Builder->open($pdf_file);

 # Parser.
 my $pdf_date_parser = DateTime::Format::PDF->new;

 my ($dt_created, $dt_modified);
 my $print_format = "%a, %d %b %Y %H:%M:%S %z";
 if (defined $pdf->created) {
         $dt_created = $pdf_date_parser->parse_datetime($pdf->created);
         print "Created: ".$dt_created->strftime($print_format)."\n";
 }
 if (defined $pdf->modified) {
         $dt_modified = $pdf_date_parser->parse_datetime($pdf->modified);
         print "Modified: ".$dt_modified->strftime($print_format)."\n";
 }

 # Output:
 # Created: Fri, 15 May 2009 08:40:48 +0200
 # Modified: Fri, 15 May 2009 08:44:00 +0200

EXAMPLE4

 use strict;
 use warnings;

 use DateTime::Format::PDF;
 use PDF::API2;

 if (@ARGV < 1) {
         print STDERR "Usage: $0 pdf_file\n";
         exit 1;
 }
 my $pdf_file = $ARGV[0];

 # Open file.
 my $pdf = PDF::API2->open($pdf_file);

 # Get meta info.
 my %meta = $pdf->info;

 # Parser.
 my $pdf_date_parser = DateTime::Format::PDF->new;

 my ($dt_created, $dt_modified);
 my $print_format = "%a, %d %b %Y %H:%M:%S %z";
 if (exists $meta{'CreationDate'}) {
         $dt_created = $pdf_date_parser->parse_datetime($meta{'CreationDate'});
         print "Created: ".$dt_created->strftime($print_format)."\n";
 }
 if (exists $meta{'ModDate'}) {
         $dt_modified = $pdf_date_parser->parse_datetime($meta{'ModDate'});
         print "Modified: ".$dt_modified->strftime($print_format)."\n";
 }

 # Output:
 # Created: Fri, 15 May 2009 08:40:48 +0200
 # Modified: Fri, 15 May 2009 08:44:00 +0200

DEPENDENCIES

DateTime::Format::Builder, Error::Pure, Scalar::Util.

REPOSITORY

https://github.com/michal-josef-spacek/DateTime-Format-PDF

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© 2024 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.02