CutyCapt with php exec - php

I'am using CutyCapt on my CentOS.
It works fine via terminal but it doesn't work via php exec function.
I've started xvfb by command in terminal:
Xvfb :99 -screen 0 1024x768x24
And I'am trying to do a screenshot by php script:
exec("DISPLAY=:99 /path/to/cutycapt --url=<some url> --out=<path/to/output>");
It doesn't show any errors but there is no output file (output directory has chmod 777)
Can somebody help me?
Thanks
UPD:
Maybe it is better somehow to allow executing of Xvfb by Apache?

I've managed to get CutyCapt to run successfully using the php at the end. The $_parameters are passed via AJAX to the php script. Hope this helps...
case 'Output_Chart': {
// We always create the .png. We use the ImageMagick convert (IMC) command to make .pdfs
if ($_Suffix == 'pdf') {
$IMC = ";convert -page 735x850 '$_PathOut/$_ChartName.png' '$_PathOut/$_ChartName.pdf'";
} else {
$IMC = '';
}
// Prepare the query string for the CutyCapt URL
$sQuery_Pattern = '?Path=%s&iDL=%d';
$sQuery = sprintf($sQuery_Pattern, $_Path, $_iDL);
// Prepare CutyCapt's command and parameters (NB: query string and out parameter are enclosed in aposts for the shell)
$sCC_Cmd = '/var/www/LF/Includes/CutyCapt';
$sCC_URL = "http://localhost/LF/LFPrint.html'$sQuery'"; // Note: Inner apostrophes
$sCC_Out = "'$_PathOut/$_ChartName.png'";
$sCC_Pattern = ' --url=%s --out=%s --delay=%d --min-width=%d';
$sCC_Options = sprintf($sCC_Pattern, $sCC_URL, $sCC_Out, $_Delay, $_MinWidth);
//$sCC_CmdLine = $sCC_Cmd . $sCC_Options . " 2> CutyCapt.err.txt";
$sCC_CmdLine = $sCC_Cmd . $sCC_Options . " 2> /dev/null";
// Prepare the final command line with xvfb-run, CutyCapt, and the URL?QueryString
$sCC_CmdLine = 'xvfb-run --auto-servernum --server-args="-screen 0, 800x1000x24" ' . $sCC_CmdLine . $IMC;
exec( $sCC_CmdLine, $aOutput = array(), $ret);
// Wait for and then return the results. sCC_CmdLine and aOutput are just for debugging
echo json_encode(array("ret" => $ret, "cmd" => $sCC_CmdLine, "Output" => $aOutput));
break;
}

Related

shell_exec() not working on php script

I wants to run shell_exec('youtube-dl -j ' . $url);, but it's not working on my PHP script and return blank array. When I try to run manually through cmd it's working fine.
I try localhost and also in my server but both are not working ... i already try many solution and implement it but still i can't.
Here is my PHP script
$url = clearString($this->security->xss_clean($this->input->post("url")));
$data['url'] = $url;
if(filter_var($url, FILTER_VALIDATE_URL)) {
$domain = strtolower(getDomainName($url));
$source = $this->DefaultModel->getSource($domain);
$data['source'] = $domain;
$cacheVar = "mediaInfo-".md5($url);
if(!$mediaInfo = $this->cache->get($cacheVar)) {
$mediaInfo = shell_exec('youtube-dl -j '.$url);
$this->cache->save($cacheVar,$mediaInfo,$source['linkCacheTime']);
}
}
When I execute $output = shell_exec("set"); it's return output i wants to run shell_exec('youtube-dl -j '.$url); command on my xampp server loaclhost.
Steps to resolve above issue.
upload youtube-dl in public_html directory.
provide full path like below
shell_exec('/home/{path}/public_html/youtube-dl -j
'.$url);

HTMLDOC does not execute from PHP

I am trying to create a PDF from an HTML file from a PHP page (Apache, LAMP) Wierd thing is, when I execute the script from the command line, it works and creates the PDF as expected. However when I browse to the page in my browser, it does nothing. I'm thinking it's a permissions issue somewhere, but I'm stumped! Here's the code. (NOTE the ls command DOES produce output in the browser so it's not just an issue of PHP not being allowed to execute shell commands)
<?php
$htmlName = ("output2/alex" . time() . ".html");
$pdfName = ("output2/alex" . time() . ".pdf");
$html = "<html><body><h1>Hello, World!</h1></body></html>";
$fileHandle = fopen($htmlName, "w+");
fwrite($fileHandle, $html);
fclose($fileHandle);
$command= "htmldoc -t pdf --browserwidth 1000 --embedfonts --header ... --footer t./ --headfootsize 5.0 --fontsize 9 --bodyfont Arial --size letter --top 4 --bottom 25 --left 28 --right 30 --jpeg --webpage $options '$htmlName' -f '$pdfName'";
echo "OUTPUT: \r\n";
$X=passthru($command);
echo "TESTING LS:";
$y=passthru("ls -al");
if(file_exists($htmlName) && file_exists($pdfName)) {
echo "Success.";
} else {
echo "Sorry, it did not create a PDF";
}
?>
When I execute the script from the command line it produces the expected output, and creates a PDF file like it's supposed to:
> php alextest.php
Zend OPcache requires Zend Engine API version 220131226.
The Zend Engine API version 220100525 which is installed, is outdated.
OUTPUT:
PAGES: 1
BYTES: 75403
TESTING LS:total 2036
drwxr-xr-x 9 ----- and so on...
When I browse the page in Chrome, it outputs only the LS command.
help!?
You might try using a full path as your php file my be executing in a different directory than it is saved in depending on how it is loaded. (IE via include, require, or .htaccess or directly by apache.)
IE
$htmlName = ("/home/alex/html/output2/alex" . time() . ".html");
$pdfName = ("/home/alex/html/output2/output2/alex" . time() . ".pdf");
I agree with the comments that using a package like http://dompdf.github.io/ or https://tcpdf.org/ would be best though.
I've seen the same issue, and for the life of me I simply couldn't find the answer to why it wouldn't do it from a web based call, but never a problem from the command line. So, instead of fighting my way to a solution on that front, I created a Perl proxy to allow me to parse PDFs from the command line making it useful for virtually any given purpose. For, with Perl, I've never had a problem parsing PDFs, and I've been doing it for decades now.
So, here's what you do. PHP Code:
exec("/usr/local/bin/perl5 -s /path/to/perl/executable/parse-pdf-from-html-document.pl -source=markup-file.html",$output);
foreach ($output as $aline) {
#- WAS SUCCESSFUL
if (strstr($aline,'Successful!') == TRUE) {
#- no feedback, win silently
}
#- NOT SUCCESSFUL
else {
echo $aline . "\n";
}
}
With $output holding the results of running exec.
Now let's look at the Perl code for parse-pdf-from-html-document.pl:
#!/usr/local/bin/perl5 -s
#- $document coming from commandline variable, via caller: PHP script
$myDocumentLocale = "/path/to/markup/document/".$document;
if (-e $myDocumentLocale) {
$documentHTML = $myDocumentLocale;
$documentPDF = $documentHTML;
$documentPDF =~ s/\.html/\.pdf/gi;
$myDocumentHTML = `cat $myDocumentLocale`;
$badPDF = 0;
$myPDFDocumentLocale = $myDocumentLocale;
$myPDFDocumentLocale =~ s/\.html/\.pdf/gi;
$badPDF = &parsePDF($myDocumentLocale, $myPDFDocumentLocale);
if ($badPDF == 0) {
print "Successful!";
}
else {
print "Error: No PDF Created.";
}
exit;
}
else {
print "Error: No document found.";
exit;
}
sub parsePDF {
my ($Ihtml, $Ipdf) = #_;
$wasBad = 0;
#- create PDF
$ENV{HTMLDOC_NOCGI} = 1;
$commandline="/usr/local/bin/htmldoc -t pdf13 --pagemode document --header ... --footer ... --left 1cm --size Letter --webpage -f $Ipdf $Ihtml";
select(STDOUT);
$| = 1;
#print "Content-Type: application/pdf\n\n";
system($commandline);
if (-e $Ipdf) {
$wasBad = 0;
}
else {
$wasBad = 1;
}
return $wasBad;
}
exit;

Php exec command pdftoimage not work

I have problem with my code.
My Code look like this:
$destinationFolder = $destinationRootFolder . '/';
// mkdir($destinationFolder,777);
$options = $this->buildOptions($saveAsJpeg, $inputPdf, $destinationFolder);
print_r($options);
// exit;
try {
$command = "/usr/bin/pdfimages ".$options[0]." ".$options[1]." ".$options[2];
echo $command;
// exit;
shell_exec($command);
exec($command);
// $command;
// echo $r;
} catch (ExecutionFailureException $e) {
throw new RuntimeException('PdfImages was unable to extract images', $e->getCode(), $e);
}
code entered first command before it executes it. When the copy command to the console everything works well but does not create php files png.
edit
root#mat-K50AB:~# php -a
Interactive mode enabled
php > ls
php > exec("/usr/bin/pdfimages -png /path/pdf/file.pdf /tmp/savefile/")
php > shell_exec("/usr/bin/pdfimages -png /path/pdf/file.pdf /tmp/savefile/")
php >
It also does not work
It sounds like the apache does not have permissions to run it, A few things to check
1) ( if CentOS/RHEL ) Is selinux stoping it, TO temporarly disable it
setenforce 0
Perminetly allow it ( Replace /usr/bin/pdfimages with all files that need access )
chcon -v --type=httpd_sys_content_t /usr/bin/pdfimages
2) Not executible by apache, Try
chmod +x /usr/bin/pdfimages
If nether of thoughs work, What os is your server running?

exec pdfimages in PHP

I have full root access through VPS (CentOS).
In Unix shell I am able to extract images with following command:
pdfimages -j xyz.pdf images
But I am unable to execute the command through PHP
exec ("pdfimages -j xyz.pdf images");
xpdf is installed. Also I have checked that // Exec function exists; // Exec is not disabled; // Safe Mode is not on.. by using following code:
$exec_enabled =
function_exists('exec') &&
!in_array('exec', array_map('trim', explode(', ', ini_get('disable_functions')))) &&
strtolower(ini_get('safe_mode')) != 1;
if($exec_enabled) { echo "enabled"; }
The following is however getting executed properly:
exec("ls -1 *.php", $output);
foreach ($output as &$tmp){
echo "$tmp<br>";
}
What am I doing wrong? Where is the issue?
You need to specify the path like below , this will save each image in the folder of your PDF.
'image' is just the file name ..
$path = 'path/to/your/folder/';
$pdf = $path.'xyz.pdf';
$command = 'pdfimages -j '.$pdf.' '.$path.'image';
exec($command);

shell script works fully in terminal but not when run by php

I have a shell script that uses unoconv and then pdftk. when i run the script through the command line it works exactly how i want it to. When i use shell_exec($cmd) in php with the same exact command it runs the script (i know because of the echo's in the script) but it looks like it does not use unoconv (and therefore cannot use pdftk). Any idea on how to troubleshoot this problem? here some code:
if(isset($_FILES["file"]["name"]) && !empty($_FILES["file"]["name"])){
$fname = $_FILES["file"]["name"];
$tmp_name = $_FILES["file"]["tmp_name"];
$dir = "powerpoints/".$name."/";
$ispdf = "1";
$output = shell_exec('mkdir '.$dir);
chmod($dir, 0777);
echo $output;
if(move_uploaded_file($tmp_name, $dir.$fname)){
chmod($dir.$fname, 0777);
$cmd = 'importppt.sh '.$name.' '.str_replace(".ppt", "", $fname);
echo "\n".$cmd;
$output = shell_exec($cmd);
echo $output;
}else{
$message = "move_uploaded_file() Failed";
}
and here is the shell script
#!/bin/bash
echo $1 ' is the argument:' $2 ' is the second '
STRING="/var/www/html/devclassroomproject/powerpoints/"
echo $STRING$1/$2'.ppt '
unoconv $STRING$1/$2'.ppt'
pdftk $STRING$1/$2'.pdf' burst output $STRING$1/$1'_%2d.pdf'
This is what is printed from echos:
importppt.sh pptest pptestpptest is the argument: pptest is the second
/var/www/html/devclassroomproject/powerpoints/pptest/pptest.ppt
edit:
to decipher my debugging
the command: "importppt.sh pptest pptest"
importppt.sh being the shell script; pptest is the first and second argument
printed by the first echo in the shell script: "pptest is the argument: pptest is the second"
printed by the second echo in the script verifying the complete path of the pdf which does exist: "/var/www/html/devclassroomproject/powerpoints/pptest/pptest.ppt"
sorry for the confusion
Found out what was wrong. the answer is here
http://johnparsons.net/index.php/2013/08/05/how-to-keep-unoconv-apache-from-making-you-sad/
basically you have to set up a home directory for the user for apache2 as www-data and change the path to the shell in the passwd file
he doesnt mention it but the changes will not work unless you restart apache

Categories