I have a comment in PhpSpreadsheet and I want to add a backgroundpicture to it.
The comment is created like this:
$spreadsheet->getActiveSheet()->getComment($cell)->getText()->createTextRun('Test text');
But there is nothing in the documentation about how to add a backgroundpicture to it.
I was able to solve my issue with a (bad, but working) workaround. It seems that PHPSpreadsheet is not able (yet) to create a backgroundpicture to a comment, maybe PHPSpreadsheet can do it in the future.
Workaround:
PHPSpreadsheet is not able to create a comment with backgroundimage, but Powershell is. With the following Powershell script you are adding a background image to a comment in row C2, in worksheet 1.
$excel = new-object -comobject excel.application
$workbook = $excel.workbooks.open("C:\MyExcel.xlsx")
$worksheet = $workbook.worksheets.item(1)
$image = "C:\MyPicture.png"
$worksheet.Range("C2").AddComment(" ")
$worksheet.Range("C2").Comment.Shape.Fill.UserPicture($image)
$workbook.save()
$workbook.close()
Stop-Process -processname powershell*
With that knowledge, you are able to create a dynamically Powershell script inside PHP:
$ps_script=Array();
$ps_script[]='$excel = new-object -comobject excel.application';
$ps_script[]='$workbook = $excel.workbooks.open("'.$excel_output_path.'")';
$ps_script[]='$worksheet = $workbook.worksheets.item('.($sheetindex+1).')';
$ps_script[]='$image = "'.$img_path.'"';
$ps_script[]='$worksheet.Range("'.$cell.'").AddComment(" ")';
$ps_script[]='$worksheet.Range("'.$cell.'").Comment.Shape.Fill.UserPicture($image)';
$ps_script[]='$workbook.save()';
$ps_script[]='$workbook.close()';
$ps_script[]='Stop-Process -processname powershell*';
$ps_script = implode("\r\n", $ps_script);
file_put_contents($powershell_output, $ps_script);
Now we create a CMD script, calling that Powershell script:
$call_powershell_script=Array();
$call_powershell_script[]="TASKKILL /F /IM powershell.exe";
$call_powershell_script[]="TASKKILL /F /IM EXCEL.exe";
$call_powershell_script[]=$powershell_path.'powershell.exe "'.$powershell_output.'"';
$call_powershell_script[]="TASKKILL /F /IM powershell.exe";
$call_powershell_script[]="TASKKILL /F /IM EXCEL.exe";
$call_powershell_script = implode("\r\n", $call_powershell_script);
file_put_contents($call_powershell_script_path, $call_powershell_script);
And finally we call this CMD script inside PHP:
$output = shell_exec($cpowershell_script_path." 2>&1");
echo "<br>";
echo $output;
echo "<br>";
Note:
Normally the Powershell script doesn't need this line:
Stop-Process -processname powershell*
But without it, the PHP script takes forever.
Note2:
I received an error in Powershell, when the script was run by PHP, that my Excel file was not accessable:
Exception calling "Open" with "1" argument(s): "Microsoft Office Excel cannot access the file 'C:\MyExcel.xlsx'. There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook.
The solution for this error was found
here.
Related
I've been trying to run a php file using window task scheduler. I've tried using the .bat file, but it won't work.
Here's what I've done:
I've created a task "Download Image"
I've created a .bat file, and the content is:
"C:\xampp\php\php.exe" -f "D:\server\newxml\download.php";
The php file that I want to run is:
ini_set('max_execution_time', 300);
$doc = simplexml_load_file('xml_edit_feeds.xml');
foreach ($doc->xpath("//item") as $item) {
$name = $item->productname;
$realname = preg_replace('/\s/', '',$name);
$url = $item->thumbnail_url;
$img = 'D:/server/newxml/imagethumbnail/'.$realname.'.png';
$filename = 'D:server/newxml/imagethumbnail/'.$realname.'.png';
file_put_contents($img, file_get_contents($url));
}
?>
This is the screen shoot of the windows task scheduler:
Is there something wrong from what I did? Thank you for your help
I didn't try this before, but I think you should use a browser exe program to open your webpage instead of php.exe.
Eureka! I got the answer to this, so, I what I've done is:
Set the Program/Script to: C:\xampp\php\php.exe
Set the arguments to: D:\path\to\php.php (In my case D:\server\newxml\download.php)
Set the start-in to the folder that contains download.php (php file that you want to run). In my case: D:\server\newxml\
i have php file that run from cmd matlab function -> that function is create .txt file and fill it up with the analysis results. then the php file takes this .txt file and sending the information as a string(lines from .txt) to a database (phpmyadmin).
my problem is that the php start to send the info from the .txt while the matlab is still writing to the file.
i thought to solve it with a global var that matlab and php know him, and use it as a flag that Flag determines when Matlab finished building the necessary file. i thought to use the window registry, but it is very complicated. there is any easier way?
thanks alot,
doron
my php file:
unlink('test.txt');
if(isset($_POST['filepath'])) {
$filename = $_POST['filepath'];
$inputDir = "C:\\xampp\\htdocs\\login";
$outputDir = "C:\\xampp\\htdocs\\login";
// here php open the matlab function from the cmd:
$command = "matlab -sd ".$inputDir." -r phpcreatefile2('".$outputDir."\\".$filename.".txt')";
exec($command);
$fileLoc= "".$outputDir."\\".$filename.".txt" ;
echo $fileLoc ;
echo "The following command was run: ".$command."<br/>";
echo $filename." was created in ".$outputDir."<br/>";
echo " Now the txt file will write in the DB <br/>";
// here i tried to check if the file exists and if it is not empty. but its not working because matlab still writing to the file.
while (1) {
if (file_exists("test.txt") ){
echo "check1";
if (filesize("test.txt")!= 0) {
echo "check2";
$file = file('test.txt');
$sql = "INSERT INTO `ID_5525_Medical_record`(`Data`,`AV_Power`,`Highest_Amp`,`90BW`,`Url_figure`) VALUES ('$file[0]','$file[1]','$file[2]','$file[3]','$file[4]')" ;
if(mysqli_query($connection,$sql))
{
unlink('test.txt');
echo "the txt file is now in the DB <br/>";
}
break;
}
else {
echo "i am going to sleep";
sleep(1);
echo "i am awake";
}
}
}
`
Normally, using COM would be the proper way to implement inter process communication between php and MATLAB (Or maybe some alternative, think all are sufficient for this simple task).
Here it seems you call MATLAB only one, you can fix the behaviour appending -wait to the command. This forces launcher to stay open until matlab closes, blocking your PHP-Script at exec($command);. Big disadvantage is, that you have to start Matlab for every function call and close it afterwards. With com one instance can stay open and do everything.
I used following command to convert the 3d models with the assimp Assimp, and it is working fine on Windows:
assimp2json seaknight.obj seaknight.json
I need to know how can I run this command from the PHP? I know that there re functions to run the shell execution from PHP, but it didn't work and I don't get any error.
PHP code is used is follows.
system("D:\assimp2json-2.0-win32\Release\assimp2json.exe assimp2json seaknight.obj seaknight.json");
and another one is
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json.exe assimp2json seaknight.obj seaknight.json');
chdir($old_path);
Found it myself
working code is below
$old_path = getcwd();
chdir('D:\assimp2json-2.0-win32\Release');
$output = shell_exec('assimp2json monster.blend monster.json');
chdir($old_path);
no need to include the .exe, after remove it the command worked
I have this PHP script:
<?php
if($_GET["name"] != null && $_GET["name"] != ""){
$name = urlencode($_GET["name"]);
$name = htmlentities($name);
$title = urlencode($_GET["title"]);
$title = htmlentities($title);
$art = urlencode($_GET["art"]);
$art = htmlentities($art);
$output = array();
exec("python add.py $name $title $art",$output);
}
?>
and here's the add.py file:
import sys
import sqlite3
name = sys.argv[1]
title = sys.argv[2]
art = sys.argv[3]
tup = (name,title,art)
conn = sqlite3.connect('arts.db')
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS arts(name,title,art)')
c.execute('INSERT INTO arts VALUES(?,?,?)',tup)
conn.commit()
conn.close()
When the file runs there is no arts.db file in my current directory. Not only that, when I debugged my program by adding print statements every here and there I realised that my program runs till conn = sqlite3.connect('arts.db') and then exits before the statement is executed.
There is no error in my program because I used the python editor in the terminal (I use Ubuntu) and then I could execute this program successfully but this doesn't happen when I execute this from the PHP script.
If you're trying to reference an existing arts.db file, which is not in the current directory when the PHP script runs, then it's probably easiest to refer to the file by its full path, i.e. change the line...
conn = sqlite3.connect('arts.db')
...to something like...
conn = sqlite3.connect('/the/full/path/to/arts.db')
If you're trying to create a new arts.db, it's highly probable that the webserver process doesn't have permission to create the file in the current directory when the PHP script runs.
It's probably safer not to give PHP permission to write to the directory containing the PHP script[s], so create a directory somewhere else to store the file, give it the appropriate permissions, and use the full path to that directory in the script.
Update
i have figured out that i dont have permissions to write hence i
created a db using another program and created a table in it but still
for inserting values into it i require permissions, can you tell me
how to do that?
Problem is that any process spawned by the PHP script will inherit the permissions from the parent process, and will have the same limitations.
On recent versions of Ubuntu, the /var/tmp directory can always be written to by any user, so you could put it there with...
conn = sqlite3.connect('/var/tmp/arts.db')
...or you can put it anywhere else by modifying the permissions, with...
$ sudo chgrp www-data /the/full/path/to/directory
$ sudo chmod g+ws /the/full/path/to/directory
Of course, this means there's not much point in using a seprate Python script, so you may as well do it all in PHP.
I am trying to make a PHP program triggered by a web submit tell a bash script to run with a single command line parameter. I am using the shflags command line parser for bash.
The pertinent part of the PHP script is as follows:
// generate unique filename
$destinationFolder = Mage::getBaseDir('media') . DS . 'webforms' . DS . 'xml';
$filename = $destinationFolder . DS . $result->getId().'.xml';
// create folder
if (!(#is_dir($destinationFolder) || #mkdir($destinationFolder, 0777, true))) {
throw new Exception("Unable to create directory '{$destinationFolder}'.");
}
// export to file
$xmlObject->getNode()->asNiceXml($filename);
// define parameters to pass
exec ( '/opt/bitnami/apache2/htdocs/sfb/scripts/xform.sh --xmlfile'.' '.$filename);
}
}
?>
The bash script (xform.sh) (just a test script) is as follows.
#!/bin/bash
. ./shflags
echo "foo" >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/$$".txt"
echo "foo" >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt
DEFINE_string 'xmlfilename' 'test' 'filename of current x.xml file from webforms' 'Z'
FLAGS "$#" || exit 1
eval set -- "${FLAGS_argv}"
echo "xml file was" ${FLAGS_xmlfilename} >> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt
The bash script works correctly from the command line, i.e.
$xform.sh --xmlfilename 1.xml
writes "xml file was 1.xml" to the foo.txt file.
When the PHP script is triggered from the web, the first part works correctly, i.e. it writes "foo" to the two target files, foo.txt and $$.txt. However, the xmlfilename variable is not coming along, and I really need that file name to be passed to the command line! (Note I should not need to use escapeshellarg because the file name is generated by my PHP program, not by user input.)
I have checked all the file permissions I can think of. xform.sh and shflags are both members of the www-data (Apache) group, owned by Apache, and a+x.
My suspicions are that the problem is related either to a) my PHP exec syntax or b) file permissions. Everything works as intended except the bit after xform.sh in this line!
exec ( '/opt/bitnami/apache2/htdocs/sfb/scripts/xform.sh --xmlfile'.' '.$filename);
UPDATE:
I've narrowed the problem some more by isolating the problem with some test code. With:
$script="echo";
$xmlfilename="$filename";
$target=">> /opt/bitnami/apache2/htdocs/sfb/scripts/seeds/xform/foo.txt";
exec ("$script $xmlfilename $target");
...
PHP correctly writes the $filename to foo.txt, so $script works when value is "echo" and $filename works too.
When I set $script to a different simple form of the xform script that (only) writes the data to the file, that also works correctly.
So the problem is specifically with something that happen when PHP tries to write the $filename as a command line variable. Does a script run by Apache need more permissions than usual if it includes a command line variable?
Sigh.
In your exec() call you have the flag as --xmlfile but you are calling it from the command line as --xmlfilename