Search & replace issue on mPDF, CAM'PDF on Ubuntu 18.04

552 views Asked by At

On Ubuntu 18.04 I have a problem with editing PDF files - specifically search & replace strings.

I tried:

  • PHP mPDF Overwrite () do nothing.

  • perl CAM :: PDF 1.60 changepagestring.pl do nothing

  • sed, do nothing.

Does not work with compressed or decompressed PDF, Does not even work with PDF generated from mPDF. UPDATE: after reinstalling libsodium mPDF works fine with PDF files generated fromm mPDF. For other PDF files issue still exist.

Also tried in var / www folders user / group www-data: www / data and in other folders / home e.g.

Any idea for bulk search & replace because I have over 1000 files to process?

The text in the files is readable. Check.

P.S. Search / Replace from the program and online service works with the same files. enter image description here

Permission on files 0755 i 0777

root@sasa-ubuntu-1:/var/www/website.local/wp-content/test/2018/12# ls -la *.pdf
-rwxr-xr-x 1 www-data www-data 847451 Oct 18 12:21 clean.pdf
-rwxrwxrwx 1 www-data www-data 395527 Oct 17 21:41 My-First.pdf
-rwxr-xr-x 1 www-data www-data 838307 Oct 17 23:30 My.pdf
-rwxr-xr-x 1 www-data www-data 838167 Oct 18 12:24 New2.pdf
-rwxr-xr-x 1 www-data www-data 838167 Oct 18 01:20 New.pdf
-rwxrwxrwx 1 www-data www-data 270340 Oct 17 16:39 Test2.pdf
-rwxrwxrwx 1 www-data www-data 274022 Oct 17 16:39 Test1.pdf
-rwxr-xr-x 1 www-data www-data 838000 Oct 18 00:55 Test2.pdf
-rwxrwxrwx 1 www-data www-data 205679 Oct 17 23:44 test.pdf

Perl script allways return "Could not find title" nevermind of readability of file when I print $page variable (see images)

use CAM::PDF;

my $pdf = CAM::PDF->new('test.pdf'); # existing document
my $nump = $pdf->numPages();
#print $nump;

my $page = $pdf->getPageContent(1);

print $page;
# $page now holds the uncompressed page content as a string

# replace the text part
if ($page =~ s/Wagner/SoundTech/g) {
$pdf->setPageContent(1, $page);
}
else {
die "Could not find title\n";
}

$pdf->cleanoutput('Test2.pdf');

enter image description here

A lot of files ends on this way.

The string that I try to find is "Wagner International Music Examinations" or only "Wagner"

mPDF and CAM-PDF are properly installed without warnings and erros and with all dependencies, I hope. Ubuntu 18.04 mPDF version 8.0 PHP 7.2 Perl 5.26.1 CAM-PDF version 1.60

mPDF occasionally have bug with Overwrite() function, I found on their github community.

Any suggestion or another way for bulk search & replace in PDF files?

enter image description here

1

There are 1 answers

2
Håkon Hægland On BEST ANSWER

Here is a hack that currently works almost for your case (I will come back later and try improve this):

use feature qw(say);
use strict;
use warnings;
# the PDF uses a non-standard encoding so it does not help to use UTF-8
# use open qw(:std :encoding(UTF-8)); 
use utf8;
use CAM::PDF;

my $fn = 'test.pdf';  # uncompressed file..
my $save_fn = 'test2.pdf';
my $pdf = CAM::PDF->new($fn);
my $nump = $pdf->numPages();
my $match = 0;
my $replace = '[(\x{a9} SoundTech International Music Examinations)]TJ';
for my $i (1..$nump) {
    my $page = $pdf->getPageContent( $i );
    # replace the text part
    if ($page =~ s/\[\(\x{a9}\).*?\]TJ/$replace/g) {
        $match = 1;
        $pdf->setPageContent($i, $page);
    }
}

if ( $match ) {
    $pdf->cleanoutput($save_fn);
    say "Save $save_fn ..";
}
else {
    say "No match";
}