Index: /main/trunk/greenstone2/perllib/plugins/PDFv2Plugin.pm
===================================================================
--- /main/trunk/greenstone2/perllib/plugins/PDFv2Plugin.pm	(revision 32285)
+++ /main/trunk/greenstone2/perllib/plugins/PDFv2Plugin.pm	(revision 32286)
@@ -1,6 +1,7 @@
 ###########################################################################
 #
-# PDFv2Plugin.pm -- pdf plugin that uses xpdftools or, if switched on,
-# pdfbox, to process PDFs.
+# PDFv2Plugin.pm -- pdf plugin that uses xpdftools and pdfbox to process PDFs.
+# It only works out of the box for GS3 since it assumes the pdfbox extension
+# is installed.
 # A component of the Greenstone digital library software
 # from the New Zealand Digital Library Project at the 
@@ -34,19 +35,21 @@
 use Mojo::DOM; # for HTML parsing
 
-use AutoLoadConverters;
+#use AutoLoadConverters;
+use PDFBoxConverter;
 use ConvertBinaryFile;
 
-@PDFv2Plugin::ISA = ('ConvertBinaryFile', 'AutoLoadConverters', 'ReadTextFile');
+#@PDFv2Plugin::ISA = ('ConvertBinaryFile', 'AutoLoadConverters', 'ReadTextFile');
+@PDFv2Plugin::ISA = ('ConvertBinaryFile', 'PDFBoxConverter', 'ReadTextFile');
 
 
 my $convert_to_list =
-    [ {	'name' => "auto",
+    [ {	'name' => "auto", # pretty_html using xpdftools' pdftohtml
 	'desc' => "{ConvertBinaryFile.convert_to.auto}" },
-      {	'name' => "text", # xpdftools
+      {	'name' => "text", # xpdftools' pdftotext
 	'desc' => "{ConvertBinaryFile.convert_to.text}" },
-      {	'name' => "paged_text",	# xpdftools
+      {	'name' => "paged_text",	# pdfbox
 	'desc' => "{ConvertBinaryFile.convert_to.paged_text}" },
       
-      {	'name' => "html", # pdfbox ## TODO: rename this to html_without_imgs
+      {	'name' => "html", # pdfbox ## TODO: rename this to html_without_imgs?
 	'desc' => "{PDFPlugin.convert_to.html}" },
       {	'name' => "pretty_html", # xpdftools
@@ -55,5 +58,5 @@
 	'desc' => "{PDFPlugin.convert_to.paged_pretty_html}"},
 
-      #pdfbox
+      # pdfbox for all pagedimg(txt) output formats:
       { 'name' => "pagedimg_jpg",
 	'desc' => "{ConvertBinaryFile.convert_to.pagedimg_jpg}"},
@@ -120,13 +123,15 @@
     push(@$pluginlist, $class);
 
-    push(@$inputargs,"-title_sub");
-    push(@$inputargs,'^(Page\s+\d+)?(\s*1\s+)?');
+#    push(@$inputargs,"-title_sub");
+#    push(@$inputargs,'^(Page\s+\d+)?(\s*1\s+)?');
 
     push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
     push(@{$hashArgOptLists->{"OptList"}},$options);
 
-    my $auto_converter_self = new AutoLoadConverters($pluginlist,$inputargs,$hashArgOptLists,["PDFBoxConverter"],1);
+    #    my $auto_converter_self = new AutoLoadConverters($pluginlist,$inputargs,$hashArgOptLists,["PDFBoxConverter"],1);
+    my $pdfbox_converter_self = new PDFBoxConverter($pluginlist, $inputargs, $hashArgOptLists);
     my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
-    my $self = BaseImporter::merge_inheritance($auto_converter_self, $cbf_self);
+    #    my $self = BaseImporter::merge_inheritance($auto_converter_self, $cbf_self);
+    my $self = BaseImporter::merge_inheritance($pdfbox_converter_self, $cbf_self);
     
     if ($self->{'info_only'}) {
@@ -149,10 +154,8 @@
     $self->{'convert_options'} .= " -pdf_dpi $dpi";
 
-    # PDFv2Plugin now supports PDF to txt conversion on Windows too:
-    # using XPDF Tools (incl pdftotext) on Windows/Linux/Mac
-    if ($self->{'convert_to'} eq "text" && $ENV{'GSDLOS'} =~ /^windows$/i) {
-	&gsprintf::gsprintf(STDERR, "{PDFv2Plugin.win_pdftotext_info}\n");
-    }
-    elsif ($self->{'convert_to'} eq "auto") {
+    # The old pdftohtml tool used by PDFPlugin didn't do PDF to txt conversion on Windows
+    # But PDFv2Plugin now supports PDF to txt conversion on Windows too using XPDFTools' pdftotext
+
+    if ($self->{'convert_to'} eq "auto") {
 	# choose pretty_html is the best default option when using xpdftools
 	$self->{'convert_to'} = "pretty_html";
@@ -169,23 +172,23 @@
     # Not all available conversion output options are possible with xpdftools, as some are
     # only handled by pdfbox. If a format is unavailable with xpdftools, default to pretty_html
-    if (!$self->{"pdfbox_conversion"}) {
-	my $convert_to = $self->{'convert_to'};
-	my $fallback_convert_to = $convert_to;
-	if($convert_to =~ /^html$/) {
-	    $fallback_convert_to = "pretty_html";
-	}
-	elsif ($self->{'convert_to'} =~ /^pagedimg/) {
-	    $fallback_convert_to = "paged_pretty_html";
-	}
-	elsif ($self->{'convert_to'} =~ /^paged_text$/) {
-	    #	print STDERR "@@@ Conversion to " . $self->{'convert_to'} , " with Xpdf Tools is not yet implemented.\n";
-	    $fallback_convert_to = "text";
-	}
-
-	if($convert_to =~ /^(html|pagedimg|paged_text)/) {
-	    &gsprintf::gsprintf(STDERR, "{PDFv2Plugin.conversion_needs_pdfbox}\n", ($self->{'convert_to'}, $fallback_convert_to));
-	    $self->{'convert_to'} = $fallback_convert_to;
-	}
-    }
+    # if (!$self->{"pdfbox_conversion"}) {
+    # 	my $convert_to = $self->{'convert_to'};
+    # 	my $fallback_convert_to = $convert_to;
+    # 	if($convert_to =~ /^html$/) {
+    # 	    $fallback_convert_to = "pretty_html";
+    # 	}
+    # 	elsif ($self->{'convert_to'} =~ /^pagedimg/) {
+    # 	    $fallback_convert_to = "paged_pretty_html";
+    # 	}
+    # 	elsif ($self->{'convert_to'} =~ /^paged_text$/) {
+    # 	    #	print STDERR "@@@ Conversion to " . $self->{'convert_to'} , " with Xpdf Tools is not yet implemented.\n";
+    # 	    $fallback_convert_to = "text";
+    # 	}
+
+    # 	if($convert_to =~ /^(html|pagedimg|paged_text)/) {
+    # 	    &gsprintf::gsprintf(STDERR, "{PDFv2Plugin.conversion_needs_pdfbox}\n", ($self->{'convert_to'}, $fallback_convert_to));
+    # 	    $self->{'convert_to'} = $fallback_convert_to;
+    # 	}
+    # }
     
     # set convert_to_plugin and convert_to_ext
@@ -203,5 +206,5 @@
     # "1", which is often the page number at the top of the page. Bad Luck
     # if your document title actually starts with "1 " - is there a better way?
-    push(@$specific_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
+#    push(@$specific_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
     my $associate_tail_re = $self->{'associate_tail_re'};
     if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
@@ -264,5 +267,6 @@
     # ConvertBinaryFile init
     $self->SUPER::init(@_);
-    $self->AutoLoadConverters::init(@_);
+#   $self->AutoLoadConverters::init(@_);
+    $self->PDFBoxConverter::init(@_);
 
 }
@@ -271,5 +275,6 @@
     my $self = shift (@_);
 
-    $self->AutoLoadConverters::begin(@_);
+#   $self->AutoLoadConverters::begin(@_);
+    $self->PDFBoxConverter::begin(@_);
     $self->SUPER::begin(@_);
 
@@ -278,6 +283,7 @@
 sub deinit {
     my $self = shift (@_);
-    
-    $self->AutoLoadConverters::deinit(@_);
+
+    $self->PDFBoxConverter::deinit(@_);
+#   $self->AutoLoadConverters::deinit(@_);
     $self->SUPER::deinit(@_);
 
@@ -302,17 +308,28 @@
 }
   
-  
+
 sub tmp_area_convert_file {
 
     my $self = shift (@_);
+    my ($output_ext, $input_filename, $textref) = @_;
     
-    if($self->{'convert_to'} =~ m/pretty_html$/) { # if outputting paged_pretty_html or pretty_html:
-	# only xpdftools can output pretty_html regardless of whether pdfbox_conversion is switched on
-	print STDERR "@@@@ PDFBox_conversion is switched on, but pretty_html variants are generated by xpdftools.\n";
+    if($self->{'convert_to'} eq "text" || $self->{'convert_to'} =~ m/pretty_html$/) { # use xpdftools
 	return $self->ConvertBinaryFile::tmp_area_convert_file(@_);
     }
-    # else, output format uses pdfbox:
-    return $self->AutoLoadConverters::tmp_area_convert_file(@_);
-
+    
+    # for all other output formats, use pdfbox:    
+    #return $self->AutoLoadConverters::tmp_area_convert_file(@_);
+    # Here, we now do what AutoLoadConverters::tmp_area_convert_file(@_) does:
+    my ($result, $result_str, $new_filename) = $self->PDFBoxConverter::convert($input_filename, $output_ext);
+    if (defined $result && $result != 0) {
+	return $new_filename;
+    }
+    my $outhandle=$self->{'outhandle'};
+    print $outhandle "PDFBoxConverter had a conversion error\n";
+    print $outhandle "$@\n";
+    if (defined $result_str) {
+	print $outhandle "$result_str\n";
+    }
+    return "";
 }
 
