source: main/trunk/greenstone2/perllib/plugins/PBConvertBinaryFile.pm@ 22674

Last change on this file since 22674 was 22674, checked in by sjm84, 14 years ago

Added a version of ConvertBinaryFile for PDFBox

File size: 3.8 KB
Line 
1package PBConvertBinaryFile;
2
3use ConvertBinaryFile;
4
5# ****
6# Look into making a shared base class that does the same, but is parameterized
7# on the name of the actual extension.
8#
9# i.e. this file is the same as OOConvertBinaryFile with the following substitutions
10#
11#OO -> PB
12#OpenOffice -> PDFBox
13#openoffice -> pdfbox
14#Open Office Conversion-> PDFBox Conversion
15
16use gsprintf 'gsprintf';
17# @ISA dynamically configured to be either PDFBoxConverter or ConvertBinaryFile
18
19# do not initialise these variables
20my $pdfbox_ext_installed;
21my $pdfbox_ext_working;
22sub BEGIN {
23 eval("require PDFBoxConverter");
24 if ($@) {
25 # Useful debugging statement if there is a syntax error in PDFBoxConverter:
26 # print STDERR "$@\n";
27 $pdfbox_ext_installed = 0;
28 $pdfbox_ext_working = 0;
29 }
30 else {
31 # Successfully found
32 $pdfbox_ext_installed = 1;
33 # now check whether it can run soffice
34 if ($PDFBoxConverter::pdfbox_conversion_available) {
35 $pdfbox_ext_working = 1;
36
37 } else {
38 $pdfbox_ext_working = 0;
39 }
40 }
41
42 if ($pdfbox_ext_working) {
43 @PBConvertBinaryFile::ISA = ('ConvertBinaryFile', 'PDFBoxConverter');
44 } else {
45 @PBConvertBinaryFile::ISA = ('ConvertBinaryFile');
46 }
47}
48
49my $opt_pdfbox_args =
50 [ { 'name' => "pdfbox_scripting",
51 'desc' => "{PDFBoxConverter.pdfbox_scripting}",
52 'type' => "flag",
53 'reqd' => "no" } ];
54
55my $arguments = [];
56
57my $options = { 'name' => "PBConvertBinaryFile",
58 'desc' => "{PBConvertBinaryFile.desc}",
59 'abstract' => "yes",
60 'inherits' => "yes",
61 'args' => $arguments };
62
63sub new {
64 my ($class) = shift (@_);
65 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
66 push(@$pluginlist, $class);
67
68 if ($pdfbox_ext_installed) {
69 print STDERR "$class: PDFBox Extension to Greenstone detected\n";
70 if (! $pdfbox_ext_working) {
71 print STDERR "... but it appears to be broken\n";
72 &gsprintf(STDERR, "PDFBoxConverter: {PDFBoxConverter.noconversionavailable} ({PDFBoxConverter.$PDFBoxConverter::no_pdfbox_conversion_reason})\n");
73 }
74 }
75
76 if ($pdfbox_ext_working) {
77 push(@$arguments,@$opt_pdfbox_args);
78 }
79
80 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
81 push(@{$hashArgOptLists->{"OptList"}},$options);
82
83 if ($pdfbox_ext_working) {
84
85 my $ooc_self = new PDFBoxConverter($pluginlist, $inputargs, $hashArgOptLists,1);
86 my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
87 $self = BasePlugin::merge_inheritance($ooc_self, $cbf_self);
88 $self->{'pdfbox_available'} = 1;
89 }
90 else {
91 $self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
92 $self->{'pdfbox_available'} = 0;
93 }
94
95 return bless $self, $class;
96
97}
98
99
100sub init {
101 my $self = shift (@_);
102 my ($verbosity, $outhandle, $failhandle) = @_;
103
104 $self->SUPER::init($verbosity,$outhandle,$failhandle);
105 if ($pdfbox_ext_working) {
106 $self->PDFBoxConverter::init();
107 }
108}
109
110sub begin {
111 my $self = shift (@_);
112
113 if ($pdfbox_ext_working) {
114 $self->PDFBoxConverter::begin(@_);
115 }
116 $self->SUPER::begin(@_);
117}
118
119sub deinit {
120 # called only once, after all plugin passes have been done
121 my ($self) = @_;
122
123 if ($pdfbox_ext_working) {
124 $self->PDFBoxConverter::deinit();
125 }
126 $self->SUPER::deinit();
127}
128
129# if we are using open office, we use PDFBoxConverter's convert method, otherwise fall back to ConvertBinaryFile method.
130sub tmp_area_convert_file {
131 my $self = shift (@_);
132 my ($output_ext, $input_filename, $textref) = @_;
133
134 if ($pdfbox_ext_working && $self->{'pdfbox_scripting'}) {
135 my ($result, $result_str, $new_filename) = $self->PDFBoxConverter::convert($input_filename, $output_ext);
136 if ($result != 0) {
137 return $new_filename;
138 }
139 my $outhandle=$self->{'outhandle'};
140 print $outhandle "PDFBox Conversion error\n";
141 print $outhandle $result_str;
142 return "";
143 }
144 else {
145 return $self->ConvertBinaryFile::tmp_area_convert_file(@_);
146 }
147}
Note: See TracBrowser for help on using the repository browser.