Add fax file conversion with libreoffice headless to support sending faxes with .doc, .docx, xls, xlsx, and images such as png, jpg, gif and etc. Thanks Wei for sharing this code with the project.

This commit is contained in:
Mark Crane
2014-08-01 22:33:20 +00:00
parent 8e9d6e200b
commit b0b2375844

View File

@@ -229,11 +229,14 @@ else {
if (strlen($fax_number) > 0) {
$fax_number = preg_replace("~[^0-9]~", "",$fax_number);
}
$fax_name = $_FILES['fax_file']['name'];
$fax_name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $fax_name);
$fax_name = str_replace(" ", "_", $fax_name);
$fax_name = str_ireplace(".tif", "", $fax_name);
$fax_name = str_ireplace(".tiff", "", $fax_name);
$fax_name = str_ireplace(".pdf", "", $fax_name);
//$fax_name = str_ireplace(".tif", "", $fax_name);
//$fax_name = str_ireplace(".tiff", "", $fax_name);
//$fax_name = str_ireplace(".pdf", "", $fax_name);
//lua doesn't seem to like special chars with env:GetHeader
$fax_name = str_replace(";", "_", $fax_name);
$fax_name = str_replace(",", "_", $fax_name);
@@ -263,28 +266,50 @@ else {
}
//get the fax file extension
$fax_file_extension = substr($dir_fax_temp.'/'.$_FILES['fax_file']['name'], -4);
if (strtolower($fax_file_extension) == "tiff") { $fax_file_extension = ".tif"; }
$fax_file_extension = '.'. pathinfo($_FILES['fax_file']['name'], PATHINFO_EXTENSION);
if (strtolower($fax_file_extension) == ".tiff") { $fax_file_extension = ".tif"; }
if (strtolower($fax_file_extension) == ".tif") { $fax_file_extension = ".tif"; }
if (strtolower($fax_file_extension) == ".pdf") { $fax_file_extension = ".pdf"; }
//upload the file
move_uploaded_file($_FILES['fax_file']['tmp_name'], $dir_fax_temp.'/'.$fax_name.$fax_file_extension);
//file conversion
if ($fax_file_extension == ".pdf") {
chdir($dir_fax_temp);
exec("gs -q -sDEVICE=tiffg3 -r204x196 -g1728x2156 -dNOPAUSE -sOutputFile=".$fax_name.".tif -- ".$fax_name.".pdf -c quit");
//exec("rm ".$dir_fax_temp.'/'.$fax_name.".pdf");
} elseif($fax_file_extension != ".tif") {
exec("export HOME=/tmp && libreoffice --headless --convert-to pdf --outdir ".$dir_fax_temp." ".$dir_fax_temp.'/'.$fax_name.$fax_file_extension);
if(file_exists($dir_fax_temp.'/'.$fax_name.'.pdf')){
chdir($dir_fax_temp);
exec("gs -q -sDEVICE=tiffg3 -r204x196 -g1728x2156 -dNOPAUSE -sOutputFile=".$fax_name.".tif -- ".$fax_name.".pdf -c quit");
}
}
//if the pdf does not exist then convert the tif to a pdf
if (!file_exists($dir_fax_sent.'/'.$fax_name.".pdf")) {
if (is_file("/usr/local/bin/gs")) {
chdir($dir_fax_sent);
exec("gs -q -sDEVICE=tiffg3 -g1728x1078 -dNOPAUSE -sOutputFile=".$fax_name.".pdf -- ".$fax_name.".tif -c quit");
}
}
//get some more info to send the fax
$mailfrom_address = $_SESSION['email']['smtp_from']['var'];
if (isset($_SESSION['fax']['smtp_from']['var'])) {
$mailfrom_address = $_SESSION['fax']['smtp_from']['var'];
}
else {
$mailfrom_address = $_SESSION['email']['smtp_from']['var'];
}
//echo 'mail from: '.$mailfrom_address.'<br>';
$sql = "select fax_email from v_fax where fax_uuid = '".$fax_uuid."'; ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetch(PDO::FETCH_NAMED);
$mailto_address_fax = $result["fax_email"];
echo $mailto_address_fax;
//echo 'mail address fax: '.$mailto_address_fax.'<br>';
$sql = "select contact_uuid from v_users where user_uuid = '".$_SESSION['user_uuid']."'; ";
$prep_statement = $db->prepare(check_sql($sql));
@@ -298,7 +323,7 @@ else {
$result = $prep_statement->fetch(PDO::FETCH_NAMED);
//print_r($result);
$mailto_address_user = $result["contact_email"];
echo $mailto_address_user;
//echo 'mail address user: '.$mailto_address_user.'<br>';
if ($mailto_address_user != $mailto_address_fax) {
$mailto_address = "'".$mailto_address_fax."\,".$mailto_address_user."'";
@@ -310,7 +335,7 @@ else {
//send the fax
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp) {
//prepare the fax command
//prepare the fax command
$route_array = outbound_route_to_bridge($_SESSION['domain_uuid'], $fax_number);
$fax_file = $dir_fax_temp."/".$fax_name.".tif";
if (count($route_array) == 0) {
@@ -337,30 +362,18 @@ else {
//copy the .tif to the sent directory
exec("cp ".$dir_fax_temp.'/'.$fax_name.".tif ".$dir_fax_sent.'/'.$fax_name.".tif");
//copy the .pdf to the sent directory
if (file_exists($dir_fax_temp.'/'.$fax_name.".pdf")) {
exec("cp ".$dir_fax_temp.'/'.$fax_name.".pdf ".$dir_fax_sent.'/'.$fax_name.".pdf");
exec("cp ".$dir_fax_temp.'/'.$fax_name.".pdf ".$dir_fax_sent.'/'.$fax_name.".pdf");
}
//convert the tif to pdf
if (!file_exists($dir_fax_sent.'/'.$fax_name.".pdf")) {
if (is_file("/usr/local/bin/gs")) {
chdir($dir_fax_sent);
exec("gs -q -sDEVICE=tiffg3 -g1728x1078 -dNOPAUSE -sOutputFile=".$fax_name.".pdf -- ".$fax_name.".tif -c quit");
}
}
//delete the .tif from the temp directory
//exec("rm ".$dir_fax_temp.'/'.$fax_name.".tif");
//convert the tif to pdf and png
if (!file_exists($dir_fax_sent.'/'.$fax_name.".pdf")) {
if (is_file("/usr/local/bin/tiff2png")) {
chdir($dir_fax_sent);
exec($_SESSION['switch']['bin']['dir']."/tiff2pdf -f -o ".$fax_name.".pdf ".$dir_fax_sent.$fax_name.".tif");
}
}
//chdir($dir_fax_sent);
//which tiff2pdf
//if (is_file("/usr/local/bin/tiff2png")) {
// exec($_SESSION['switch']['bin']['dir']."/tiff2png ".$dir_fax_sent.$fax_name.".tif");
// exec($_SESSION['switch']['bin']['dir']."/tiff2pdf -f -o ".$fax_name.".pdf ".$dir_fax_sent.$fax_name.".tif");
//}
//redirect the browser
header("Location: fax_view.php?id=".$fax_uuid."&msg=".$response);