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

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.
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');
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?

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";
}

Related

Php FOPEN - CENTOS 7 - Cant Create File or Folder

EDIT 01 UPDATE:
ERROR Showing UP Warning: mkdir(): Permission denied in
/var/www/html/projeto01/index.php on line 7 Sucess. Dir Created
EDIT 02
Change to FEDORA and is working.
Thank you guys
before submitting this topic I read and tried all the topics here. No success.
The environment is working great until here. Php 7.4, Apache, Mysql Workbench, Atom... i can code and select from database, everything was great.
The code is working with no error execpt it did not create the directory. :/
If i create the dir manually, return the message that it already exist.
After read a lot of topics and get help from facebook.
I did change on group polices. Not worked.
chmod on the folder, not worked.
On CENTOS the apache user is APACHE instead www:data
ls -l /var/www return:
drwxr-x---. 2 apache apache 6 Nov 16 13:19 cgi-bin
drwxrwxrwx. 3 apache apache 39 Mar 19 13:54 html
ls -l /var/www/html return:
-rw-r--r--. 1 root root 86 Mar 19 00:09 info.php
drwxrwxrwx. 2 apache apache 23 Mar 19 13:58 projeto01
ls -l /var/www/html/projeto1 return:
-rwxrwxrwx. 1 root root 148 Mar 19 15:14 index.php
Thanks for the help!
My code is:
> <?php
$name = "images";
if (!is_dir($name)) {
mkdir($name);
echo "Sucess. Dir Created.";
} else {
echo "Dir already exist. $name";
}
?>

PHP script not executing pdflatex

I have a PHP script which accepts data from an incoming POST request, downloads the data to a file, and then runs a shell script. Only some parts of the shells script are executed --- don't know why. The request is
https://knode.work/processLatex.php
where the body is the JSON {data: SOURCE} and SOURCE is LaTeX source text. Here is the PHP script:
<?php
$post_data = $_POST['data'];
if (!empty($post_data)) {
$dir = '/var/www/html/files/';
$file = 'foo';
$filename = $dir.$file.'.tex';
$handle = fopen($filename, "w");
fwrite($handle, $post_data);
fclose($handle);
chdir($dir);
exec("sh texit.sh");
echo("Done!!");
}
?>
And here is the shell script:
/usr/bin/pdflatex foo.tex
/usr/bin/pdfatex foo.tex
rm yuuk.txt
rm *.out *.log *.aux
The /usr/bin/pdflatex lines are not executed, but the other lines are. What the heck? Are not all lines equal?
((Could there be a permissions problem?))
Below is a listing of the ./files directory. I created foo.pdf by running pdflatex foo.tex as root.
-rw-r--r-- 1 root root 207383 Aug 13 17:31 foo.pdf
-rw-r--r-- 1 www-data www-data 11372 Aug 13 18:35 foo.tex
-rw-r--r-- 1 root root 84 Aug 13 17:40 texit.sh
So perhaps the problem lies in the ownership of foo.tex versus that of pdflatex (re comment below) by RiggsFolly. Here is the dope on pdflatex:
lrwxrwxrwx 1 root root 6 Aug 21 2017 /usr/bin/pdflatex -> pdftex
Also, I set everything up as root, e.g.,
drwxr-xr-x 6 root root 4096 Aug 13 18:36 html
What should I do?

perform shell_exec over browser - raspberry pi

I want to run a PHP script from chromium-browser which will print a file. The file is located in /tmp folder on the Raspberry Pi. The file is copied from the server using SAMBA. The script has shell_exec command which attempts to do something with the file. The script works from the command line but not through the browser (chromium). I run the latest Stretch version of Raspbian.
I suspect it is something to do with permissions but I do not know where the problem is located. I have previous image Jessie with similar setup and functionality and it works. I compared all the permissions on both systems and they are exactly the same but I must be missing something.
Here is the PHP script:
<?php
header("Access-Control-Allow-Origin: *");
shell_exec("sudo lpr -P printerName /tmp/sample.pdf");
shell_exec("rm /tmp/sample.pdf");
?>
Here are related permissions:
drwxrwxrwt 15 root root 4096 Nov 23 16:17 tmp
drwxr-xr-x 12 root root 4096 Nov 3 13:58 var
-rwxrw-rw- 1 nobody nogroup 320855 Nov 23 15:31 sample.pdf

PHP copy() method doesn't work on server (NGINX)

I'm building a blog and I need to store images on my server. The images are given with full URL and when I post the article it saves the image with the copy() method.
In local all works well, but on my server nothing changes. However allow_url_fopen is set to On in php.ini file.
The strange thing is that the server changes the name of the file expected to be saved on, but it doesn't store it. For example I want to save this picture : http://s.tfou.fr/mmdia/i/03/8/bob-l-eponge-10479038eajyx.png . I put this URL inside my form, I submit it and then the server is saving this path : content/images/563b62825ab53.png as expected... But the path returns a 404 and the image is nowhere.
Here is my php code (cover is the name of the field where I put the image URL) :
$extension = explode('.', $_POST['cover']);
$uniq = uniqid();
$path = 'content/images/'.$uniq.'.'.$extension[count($extension)-1];
copy($_POST['cover'], $path);
$cover = $path;
here is what I have on the server with existing images pulled from my repo (from my local where all is working well) :
total 1328
-rw-r--r-- 1 root www-data 105331 Nov 5 14:59 563b578484ea1.jpg
-rw-r--r-- 1 root www-data 311132 Nov 5 14:59 563b57cf1db89.png
-rw-r--r-- 1 root www-data 132129 Nov 5 14:59 563b5a33d6c3b.jpg
-rw-r--r-- 1 root www-data 180274 Nov 5 14:59 563b5bbfe649b.jpeg
-rw-r--r-- 1 root www-data 283665 Nov 5 14:59 563b5c068e0bf.jpg
-rw-r--r-- 1 root www-data 311132 Nov 5 14:57 563b5fb480e73.png
But if I delete all and I try to make the server copy its own pictures, nothing appears... And here are the rights of the folders that contain the pictures :
drwxr-sr-x 3 root www-data 4096 Nov 5 14:57 content
which contains :
drwxr-sr-x 2 root www-data 4096 Nov 5 14:59 images
which contains nothing...
I don't know if it's a server issue or a lack in my php.ini or something else.
you need to give write permission to the user/group that nginx is running under (it shouldn't be root). chmod g+w images should do what you want assuming nginx is running as group www-data on your system

executing python in the command line inside a controller - PHP/CodeIgniter

Inside my automation.php controller, I have the following function:
public function deploy_test() {
echo json_encode(system("python --version"));
}
When the user wants to deploy a test, by clicking a test button in the webpage, he would be able to accomplish such a task.
However, when I click the test button, my output is:
""
Meanwhile, when I execute the same function with the command:
public function deploy_test() {
echo json_encode(system("ls -l"));
}
I'm getting:
total 32
drwxr-xr-x. 15 philippe philippe 4096 Mar 4 16:48 application
drwxrwxr-x. 2 philippe philippe 4096 Mar 4 17:28 css
-rw-r--r--. 1 philippe philippe 6357 Jan 30 11:53 index.php
drwxrwxr-x. 2 philippe philippe 4096 Feb 27 15:38 js
-rw-r--r--. 1 philippe philippe 2496 Jan 30 11:53 license.txt
drwxr-xr-x. 8 philippe philippe 4096 Jan 30 11:53 system
drwxr-xr-x. 12 philippe philippe 4096 Jan 30 11:53 user_guide
Could someone please help me to get that straighten out?
The problem is not with your code or PHP.
The problem is with your permissions.
php uses permissions which are set in the env-vars of apache.
Which is ideally set as :
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
under your apache2 / httpd conf file.
For example:
Try running:
<?= `whoami` ?>
via your shell and via your browser.
Your browser will probably say www-data and shell will say your username or if you are on AWS - default, you would get root
You do not have to use system() , use exec() instead.
We should be close as :
echo json_encode(exec("python --version"));
Performing operations will require you to have correct User and Groups set.
Look up for : In the shell, what does " 2>&1 " mean?
So your code should be :
echo json_encode(exec("python --version 2>&1"));
Hope it helps!
This works fine for my production server
public function deploy_test() {
echo json_encode(system("python --version 2>&1"));
}
with the output
Python 2.7.3
"Python 2.7.3"
Output of the unix command printed twice as system() itself outputs the result to browser. So exec() can be used in the place of system to avoid this.
public function deploy_test() {
echo json_encode(exec("python --version 2>&1"));
}
which outputs
"Python 2.7.3"
as expected.
I suspect it is not in the path. Try:
Type the full path to the python command (such as /usr/bin/python --version)
Find out with the command which, 'which python'
try executing your script from the command line, 'php script.php' => sometimes the web
server sets up things differently
make sure the error displaying is enabled with
ini_set('display_errors',1);
system returns only last line.
system
public function deploy_test() {
system("python --version", $out);
echo json_encode(implode($out));
}
It's a bit of hack, but you can find out the version number with 1 decimal even if you aren't allowed to execute python (which is the case with the 'user' CI).
//find python path
exec("which python", $path);
//show all subdirs in python
exec("ls -l ".$path[0]."*", $output);
$output = implode("\n", $output);
//preg match on version numbers
preg_match_all("#python(\d+(?:\.\d{1,2})?)#", $output , $matches);
$installed_versions = $matches[1];
//sort in reversing order
$versions_sorted_desc = array_reverse($installed_versions);
//latest version is element 0
$latest_version = $versions_sorted_desc[0];
echo $latest_version;

Categories