#! /Users/jj/bin/perl # a2pdf - converts ASCII text to PDF format, with optional # line/page numbering and Perl syntax highlighting # # Copyright (C) 2007 Jon Allen # # This software is licensed under the terms of the Artistic # License version 2.0. # # For full license details, please read the file 'artistic-2_0.txt' # included with this distribution, or see # http://www.perlfoundation.org/legal/licenses/artistic-2_0.html use strict; use warnings; use App::a2pdf; use Getopt::Long; use Pod::Usage; our $VERSION = '1.13'; binmode *STDOUT; # Set default options my %options = ( header => 1, # Include header on all pages footer => 1, # Include footer on all pages line_numbers => 1, # Print line numbers page_width => 595, # A4 page_height => 842, # A4 left_margin => 48, # 0.75" right_margin => 48, # 0.75" top_margin => 60, # bottom_margin => 60, # font_face => 'Courier', # Monospaged text font_size => 9, # Text size = 9 points perl_syntax => 1, # Perform Perl syntax highlighting icon_scale => 0.5, # Icon scaling (%age) ); # Parse command line options GetOptions(\%options, qw/ font_size|font-size=i font_face|font-face=s line_spacing|line-spacing=i page_size|page-size=s page_height|page-height=i page_width|page-width=i left_margin|left-margin=i right_margin|right-margin=i top_margin|top-margin=i bottom_margin|bottom-margin=i header! footer|page_numbers|page-numbers! line_numbers|line-numbers! perl_syntax|perl-syntax|perl! title=s icon=s icon_scale|icon-scale=s timestamp noformfeed /, 'output_file|o|output-file=s' => sub{open STDOUT,">$_[1]" or die("Cannot open output file $_[1]: $!\n")}, 'margins=i' => sub{$options{top_margin} = $_[1]; $options{bottom_margin} = $_[1]; $options{left_margin} = $_[1]; $options{right_margin} = $_[1]}, 'version' => sub{print "This is a2pdf, version $VERSION\n"; exit}, 'help' => sub{pod2usage(-verbose => 1, -noperldoc => 1)}, 'doc' => sub{pod2usage(-verbose => 2, -noperldoc => 1)} ) or die; $options{title} = (@ARGV) ? $ARGV[0] : 'STDIN' unless (exists $options{title}); $options{title} .= (' - ' . ((@ARGV) ? scalar localtime($^T - (-M $ARGV[0])*24*60*60) : scalar localtime)) if (exists $options{timestamp}); # Create PDF object my @text = (<>); my $pdf = App::a2pdf->new(%options); $pdf->line_number_chars(($options{line_numbers}) ? length sprintf("%d",scalar @text) : 0); # Print document if ($options{perl_syntax}) { PERL: { eval "use Perl::Tidy"; if ($@) { warn "Cannot perform syntax highlighting, Perl::Tidy not installed\n"; goto NOPERL; } Perl::Tidy::perltidy( source => \@text, formatter => $pdf ); } } else { NOPERL: { foreach my $line (@text) { $pdf->print($line); } } } $pdf->output; exit; #----------------------------------------------------------------------- sub _MANIFEST { require Perl::Tidy; } =head1 NAME a2pdf - converts ASCII text to PDF format, with optional line/page numbering and Perl syntax highlighting =head1 SYNOPSIS a2pdf [options] [filename] =head2 Options B recognises the following command line options: =over 4 =item --help Displays usage message and exits. =item --doc Displays full documentation and exits. =item --version Prints the version number and exits =item --output-file | -o Specifies the filename for the PDF file. If this option is not set, B will output to STDOUT. =item --title Sets the title to be included in the page header. If unspecified, the title will default to the name of the file which is being converted, or to 'STDIN' if B is processing from standard input. =item --timestamp Boolean option - if set, the timestamp of the file to be converted will be included in the page header. This option is turned off by default. =item --icon Path to an image file which will be included as part of the header in the top left of each page. Image files may be in any format supported by L. =item --icon-scale Scaling value for icon images, default is 0.25. =item --header | --noheader | --notitle Prints a header consististing of the page title, and optionally the timestamp and an image icon at the top of each page. This option is enabled by default, use C<--notitle> or C<--noheader> to disable. =item --footer | --nofooter | --page-numbers | --nopage-numbers Adds the current page number to the bottom of each page. This is enabled by default, use C<--nofooter> or C<--nopage-numbers> to disable. =item --line-numbers | --noline-numbers By default, line numbers will be included in the output PDF file. To disable this behaviour, use the C<--noline-numbers> option. =item --perl-syntax | --noperl-syntax Enables or disables (default is enabled) Perl syntax highlighting. This feature requires that the Perl::Tidy module is installed. =item --page-width =item --page-height Page width and height in points. Default page size is 595 x 842 (A4). =item --margins =item --left-margin =item --right-margin =item --top-margin =item --bottom-margin Specifies the non-printable area of the page. The C<--margin> option will set all margins to the same value, however individual margins may be altered with the appropriate options. Values must be given in points. The default value for the left and right margins is 48 points, and for the top and bottom margins is 60 points. =item --font-face Sets the font to use for the PDF file - currently this must be one of the PDF core fonts. The default font face is Courier. =item --font-size Font size in points, default value is 10. =item --line-spacing Line spacing in points, default value is the font size + 2. =item --noformfeed By default, any formfeed characters in the input stream will be processed and will act as expected, i.e. a new page will be started in the output PDF file. This can be disabled with the C<--noformfeed> option which will cause all formfeed characters to be ignored. =back Options may be given in any format recognised by the I Perl module, e.g. C<--name=value> or C<--name value>. Option names may be abbreviated to their shortest unique value. If the input filename is not given, then B will expect to receive input from STDIN. =head1 DEPENDENCIES B requires the I Perl module (tested with PDF::API2 version 0.60). Perl syntax highlighting requires the I module (tested with Perl::Tidy version 20070504). To include images in the page header, the modules I and I must be installed. =head1 BUGS / ISSUES =over 4 =item * If the Perl syntax highlighting feature is used and the input Perl code uses source filter modules, then depending on the changes made by the source filter the syntax highlighting may not be performed correctly. =item * When running under Red Hat 9, the LANG environment variable must be set to 'C'. =back =head1 SEE ALSO B homepage - L PDF::API2 - L Perl::Tidy - L File::Type - L Image::Size - L =head1 AUTHOR Written by Jon Allen (JJ), / L =head1 COPYRIGHT and LICENCE Copyright (C) 2007 Jon Allen (JJ) This software is licensed under the terms of the Artistic License version 2.0. For full license details, please read the file 'artistic-2_0.txt' included with this distribution, or see http://www.perlfoundation.org/legal/licenses/artistic-2_0.html =cut