| 1 | package App::optex::textconv::pdf; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '1.08'; | |||||||||
| 4 | |||||||||||
| 5 | =encoding utf-8 | ||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | textconv::pdf - optex::textconv submodule to handle PDF files | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | Version 1.08 | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | optex command -Mtextconv | ||||||||||
| 18 | |||||||||||
| 19 | optex command -Mtextconv::pdf::set(pagebreak=number) -Mtextconv | ||||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | This is a submodule for L<App::optex::textconv> to handle PDF | ||||||||||
| 24 | documents. You don't have to call it explicitly. | ||||||||||
| 25 | |||||||||||
| 26 | =head1 OPTIONS | ||||||||||
| 27 | |||||||||||
| 28 | To set options, call C<textconv::pdf> module before C<textconv>. | ||||||||||
| 29 | |||||||||||
| 30 | optex command -Mtextconv::pdf::set(pagebreak=number) -Mtextconv ... | ||||||||||
| 31 | |||||||||||
| 32 | Accept following parameters. | ||||||||||
| 33 | |||||||||||
| 34 | =over 4 | ||||||||||
| 35 | |||||||||||
| 36 | =item B<pagebreak>=I<type> | ||||||||||
| 37 | |||||||||||
| 38 | Takes one of follwoing I<type>s. Default is C<rule>. | ||||||||||
| 39 | |||||||||||
| 40 | =over 4 | ||||||||||
| 41 | |||||||||||
| 42 | =item B<rule> | ||||||||||
| 43 | |||||||||||
| 44 | Draw horizontal rule. | ||||||||||
| 45 | Default is successive 78 characters of C<->. | ||||||||||
| 46 | Looks B<width> and B<mark> parameter. | ||||||||||
| 47 | |||||||||||
| 48 | =item B<number> | ||||||||||
| 49 | |||||||||||
| 50 | Print page number. Format is defined by B<format> parameter. | ||||||||||
| 51 | |||||||||||
| 52 | =item B<np> | ||||||||||
| 53 | |||||||||||
| 54 | Print ASCII new page code (C<^L>, C<0x0c>). | ||||||||||
| 55 | |||||||||||
| 56 | =back | ||||||||||
| 57 | |||||||||||
| 58 | =item B<width> | ||||||||||
| 59 | |||||||||||
| 60 | Set length of horizontal rule. | ||||||||||
| 61 | Default is 78. | ||||||||||
| 62 | |||||||||||
| 63 | =item B<mark> | ||||||||||
| 64 | |||||||||||
| 65 | Set character to used in horizontal rule. | ||||||||||
| 66 | Default is C<->. | ||||||||||
| 67 | |||||||||||
| 68 | =item B<format> | ||||||||||
| 69 | |||||||||||
| 70 | Define format for B<number> parameter. | ||||||||||
| 71 | Default is C<[ Page %d ]>. | ||||||||||
| 72 | |||||||||||
| 73 | =item B<raw> | ||||||||||
| 74 | |||||||||||
| 75 | Use C<-raw> option with L<pdftotext(1)> command. | ||||||||||
| 76 | |||||||||||
| 77 | =back | ||||||||||
| 78 | |||||||||||
| 79 | =head1 SEE ALSO | ||||||||||
| 80 | |||||||||||
| 81 | L<https://github.com/kaz-utashiro/optex> | ||||||||||
| 82 | |||||||||||
| 83 | L<https://github.com/kaz-utashiro/optex-textconv> | ||||||||||
| 84 | |||||||||||
| 85 | =head1 AUTHOR | ||||||||||
| 86 | |||||||||||
| 87 | Kazumasa Utashiro | ||||||||||
| 88 | |||||||||||
| 89 | =head1 LICENSE | ||||||||||
| 90 | |||||||||||
| 91 | Copyright 2019-2022 Kazumasa Utashiro. | ||||||||||
| 92 | |||||||||||
| 93 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 94 | it under the same terms as Perl itself. | ||||||||||
| 95 | |||||||||||
| 96 | =cut | ||||||||||
| 97 | |||||||||||
| 98 | 1 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 99 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 100 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 101 | |||||||||||
| 102 | 1 | ▶ | use App::optex::textconv::Converter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 103 | |||||||||||
| 104 | 1 | our @CONVERTER = ( | |||||||||
| 105 | [ qr/\.pdf$/i => \&to_text ], | ||||||||||
| 106 | ); | ||||||||||
| 107 | |||||||||||
| 108 | 1 | our %param = ( | |||||||||
| 109 | pagebreak => 'rule', | ||||||||||
| 110 | width => 78, | ||||||||||
| 111 | mark => '-', | ||||||||||
| 112 | format => '[ Page %d ]', | ||||||||||
| 113 | raw => 0, | ||||||||||
| 114 | ); | ||||||||||
| 115 | |||||||||||
| 116 | my %pagebreak = ( | ||||||||||
| 117 | 0 | ▶ | rule => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 118 | 0 | my $rule = $param{mark} x $param{width} . "\n\n"; | |||||||||
| 119 | 0 | ▶ | sub { $rule }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 120 | }, | ||||||||||
| 121 | 0 | ▶ | number => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 122 | 0 | my $n = 1; | |||||||||
| 123 | 0 | my $format = $param{format} . "\n\n"; | |||||||||
| 124 | 0 | ▶ | sub { sprintf $format, $n++ }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 125 | }, | ||||||||||
| 126 | 0 | ▶ | np => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 127 | 0 | ▶ | sub { "\f" }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 128 | }, | ||||||||||
| 129 | 1 | ); | |||||||||
| 130 | |||||||||||
| 131 | 0 | ▶ | sub to_text { | ||||||||
|
Subroutine
to_text: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 132 | 0 | my $file = shift; | |||||||||
| 133 | 0 | ▶ | my $type = ($file =~ /\.(pdf)$/i)[0] or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | my $break = $pagebreak{$param{pagebreak}}->(); | |||||||||
| 135 | 0 | my $pdftotext = 'pdftotext'; | |||||||||
| 136 | 0 | ▶ | $pdftotext .= ' -raw' if $param{raw}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 0 | local $_ = qx{ $pdftotext -q \"$file\" - }; | |||||||||
| 138 | 0 | s/\f/$break->()/ger; | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 0 | ▶ | sub set { | ||||||||
|
Subroutine
set: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 142 | 0 | my %opt = @_; | |||||||||
| 143 | 0 | ▶ | while (my($k, $v) = each %opt) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 0 | ▶ | exists $param{$k} or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 0 | $param{$k} = $v; | |||||||||
| 146 | } | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 1 | 1; | |||||||||