264 lines
12 KiB
PHP
264 lines
12 KiB
PHP
<?php
|
|
/*=======================================================================
|
|
*
|
|
* inc_html2pdf.inc.php
|
|
*
|
|
* Autor: Marc Vollmann
|
|
*
|
|
=======================================================================*/
|
|
|
|
|
|
require_once ("../include/WkHtmlToPdf.php");
|
|
include_once ("../include/email/htmlMimeMail.php");
|
|
|
|
|
|
// Gets a HTML sniplet and generate a complete HTML page to be converted
|
|
function generateOutputHTML ($htmlSnip, $pageTitel) {
|
|
$htmlOut = "";
|
|
$htmlOut .= "<html>\n";
|
|
$htmlOut .= " <head>\n";
|
|
$htmlOut .= " <title>" . $pageTitel . "</title>\n";
|
|
$htmlOut .= " </head>\n";
|
|
$htmlOut .= " <body>\n";
|
|
$htmlOut .= $htmlSnip;
|
|
$htmlOut .= " </body>\n";
|
|
$htmlOut .= "</html>\n";
|
|
return $htmlOut;
|
|
}
|
|
|
|
|
|
// Generates PDF document
|
|
// $htmlSnip : HTML sniplet without tags <html> and <head>
|
|
// $pageTitel : Page title
|
|
// $sendToClientInlineDisplay : Send to client for inline display [= "1"]
|
|
// $sendToClientFileDownload : Send to client for file download [= "1"]
|
|
// $fileTargetType : Extension of generated document [Default "pdf"]
|
|
// $pdfPageSize : Proprietary sizes defined in this function [e.g. "label60x40"]
|
|
// $specialPath : Path for generating and downloading PDF documents [Default "/temp/download/"]
|
|
// $specialFileName : Special file name of the document NOT generating a secret file name
|
|
function generatePDF ($htmlSnip, $pageTitel, $sendToClientInlineDisplay, $sendToClientFileDownload, $fileTargetType = "", $pdfPageSize = "", $specialPath = "", $specialFileName = "") {
|
|
|
|
if ($specialPath == "") : $specialPath = "../temp/download/"; endif; // Default path for generated document
|
|
if ($fileTargetType == "") : $fileTargetType = "pdf"; endif; // Default document extension
|
|
|
|
// Generate "secret" filename
|
|
$f_secretFileName = $specialFileName;
|
|
if ($specialFileName == "") :
|
|
$f_secretFileName = "pdf_" . md5(strval(rand(1000,9999)) . strval(rand(1000,9999)) . strval(rand(1000,9999)));
|
|
endif;
|
|
|
|
// Source filename
|
|
$fileExtensionSource = "html";
|
|
$fileName = $specialPath . $f_secretFileName . "." . $fileExtensionSource;
|
|
$fileNameConversion = $specialPath . $f_secretFileName;
|
|
|
|
// Target filename
|
|
$fileExtensionTarget = $fileTargetType;
|
|
$fileNameTarget = $specialPath . $f_secretFileName . "." . $fileExtensionTarget;
|
|
|
|
// Generate HTML WITH ABSOLUTE path for creating PDF, etc.
|
|
$htmlOut = generateOutputHTML($htmlSnip, $pageTitel);
|
|
// $htmlOut = urlencode(my_str_check_http($htmlOut));
|
|
|
|
// Remove old file(s)
|
|
foreach (glob($fileName) as $fileNameToBoRemoved) {
|
|
if (file_exists($fileNameToBoRemoved)) :
|
|
unlink($fileNameToBoRemoved);
|
|
endif;
|
|
}
|
|
|
|
// Write new file according to the current employee
|
|
// writeToFile($fileName, $htmlOut); // Write attachement data into file
|
|
|
|
// **** Convert HTML file ****
|
|
$pdfGlobalOptions = "";
|
|
$pdfPageOptions = "";
|
|
if ($pdfPageSize != "") :
|
|
if ($pdfPageSize == "label60x40") :
|
|
$pdfGlobalOptions = array('page-height' => '40mm', 'page-width' => '60mm', 'margin-bottom' => '1mm', 'margin-left' => '3mm', 'margin-right' => '1mm', 'margin-top' => '1mm');
|
|
$pdfPageOptions = array('disable-smart-shrinking');
|
|
else :
|
|
$pdfGlobalOptions = array('page-size' => $pdfPageSize);
|
|
endif;
|
|
endif;
|
|
if ($pdfGlobalOptions == "" || !is_array($pdfGlobalOptions)) :
|
|
$pdf = new WkHtmlToPdf;
|
|
else :
|
|
$pdf = new WkHtmlToPdf($pdfGlobalOptions);
|
|
endif;
|
|
$pdf->addPage($htmlOut, $pdfPageOptions); // Add a HTML file, a HTML string or a page from a URL ["$pdf->addPage($fileName);" OR "$pdf->addPage('http://assecutor.de');" possible]
|
|
// $pdf->addCover('mycover.html'); // Add a cover (same sources as above are possible)
|
|
// $pdf->addToc(); // Add a Table of contents
|
|
// Save the PDF
|
|
if (!$pdf->saveAs($fileNameTarget)) :
|
|
throw new Exception('Could not create PDF: '.$pdf->getError());
|
|
endif;
|
|
// Send to client for inline display
|
|
if ($sendToClientInlineDisplay == "1") :
|
|
$pdf->send();
|
|
endif;
|
|
// Send to client for file download
|
|
if ($sendToClientFileDownload == "1") :
|
|
$pdf->send($fileNameTarget);
|
|
endif;
|
|
|
|
return $fileNameTarget;
|
|
}
|
|
|
|
|
|
// Generates PDF document by calling a special metafield template
|
|
// $specialPath : Path for generating and downloading PDF documents [Default "/temp/download/"]
|
|
// $specialFileName : Special file name of the document NOT generating a secret file name
|
|
function generateMetafieldPDF ($specialPath = "", $hqId = "") {
|
|
global $category, $objId, $cascadingObjType;
|
|
|
|
$retVal = "";
|
|
|
|
if ($category != "" && is_numeric($category) && $objId != "" && is_numeric($objId) && $cascadingObjType != "") :
|
|
|
|
if ($specialPageTitle == "") :
|
|
$specialPageTitle = $objId;
|
|
endif;
|
|
|
|
// Try to get headquartes. It is important send mails !!!!
|
|
if ($hqId == "" && $objId != "" && is_numeric($objId)) :
|
|
if ($cascadingObjType = "jb") :
|
|
$hqId = getFieldValueFromId("job","jb_id",$objId,"hq_id");
|
|
endif;
|
|
endif;
|
|
|
|
// Get current template stored in metafieldcategory for output
|
|
$currTemplateId = getFieldValueFromId("metafieldcategory","mtfc_id",$category,"mtft_id");
|
|
$atLeastOneImageDoesExistInForm = false; // Init according to image file upload
|
|
|
|
// Generate output
|
|
$absoluteSystemPath = getAbsoluteSystemPath(); // Definition of absolute path, filename and extension. Has to be called here BEFORE calling function "outputMetaFieldSpecial()" !!!!
|
|
|
|
getDBData("job", $objId); // Get associative array for JOB data. Has to be called here BEFORE calling function "outputMetaFieldSpecial()" !!!!
|
|
getDBData("tour", $objId); // Get associative array for TOUR data of ALL stations. Has to be called here BEFORE calling function "outputMetaFieldSpecial()" !!!!
|
|
getDBData("tourservice", $objId);
|
|
getDBData("tourarticle", $objId);
|
|
// print_r($jobData); die();
|
|
$editMode = 3; // Prepare for HTML email to convert e.g. to PDF
|
|
$outputMetaFieldsHtml = outputMetaFieldSpecial($currTemplateId, $editMode, $absoluteSystemPath);
|
|
$outputMetaFieldsHtmlPHP = $outputMetaFieldsHtml[0];
|
|
$outputMetaFieldsHtmlJS = $outputMetaFieldsHtml[1];
|
|
|
|
// Check configuration for output
|
|
if (isset($sendToClientInlineDisplay) && $sendToClientInlineDisplay == "1") : $sendToClientInlineDisplay = true; else : $sendToClientInlineDisplay = false; endif;
|
|
if (isset($sendToClientFileDownload) && $sendToClientFileDownload == "1") : $sendToClientFileDownload = true; else : $sendToClientFileDownload = false; endif;
|
|
if (!isset($fileTargetType) || $fileTargetType == "") : $fileTargetType = "pdf"; endif;
|
|
|
|
// Generate PDF document
|
|
$retVal = generatePDF($outputMetaFieldsHtmlPHP, $specialPageTitle, $sendToClientInlineDisplay, $sendToClientFileDownload, $fileTargetType, "", $specialPath, $f_secretFileName);
|
|
|
|
endif;
|
|
|
|
return $retVal;
|
|
}
|
|
|
|
|
|
// Send mail with PDF attachment
|
|
// $hqId : ID of the headquarters is a must to send a mail with corresponding mail options
|
|
// $htmlSnip : HTML sniplet without tags <html> and <head>
|
|
// $pageTitel : Simple page title of the HTML page
|
|
// $mailToAddresses : Mail addresses of the main receiver(s) [To]. (Comma separated list)
|
|
// $mailAttachedFileName : Filename of the file to be attached
|
|
// $mailSubject : Subject of the mail
|
|
// $mailText : Content of the mail
|
|
// $mailAttachedFileType : Type of the file to be attached to the mail (important for the mime type)
|
|
// $mailCcAddresses : Mail addresses of the receiver(s) [Cc].
|
|
// $mailBccAddresses : Mail addresses of the receiver(s) [Bcc].
|
|
// $parKeyForLogFile : Key in table "parameter" for name of the logfile (e.g. "AUTOMAILER_CASHFLOW_LOGFILE"). If it is emty "../log/automailer_pdf.log" will be used as default.
|
|
// $objId : If the headquarters ID does not exist in $hqId, the $hqId can be evaluated from $objId and $cascadingObjType
|
|
// $cascadingObjType : See description for parameter $objId
|
|
function sendPDFMail ($hqId, $htmlSnip, $pageTitel, $mailToAddresses, $mailAttachedFileName, $mailSubject = "", $mailText = "", $mailAttachedFileType = "", $mailCcAddresses = "", $mailBccAddresses = "", $mailSenderAddress = "", $parKeyForLogFile = "", $objId = "", $cascadingObjType = "") {
|
|
$sendMail = false;
|
|
|
|
// Try to get headquartes. It is important send mails !!!!
|
|
// Remember: hq_id, cr_id, cs_id are unique for the complete database about all mandators. No mo_id or category needed !!!!
|
|
if ($hqId == "" && $objId != "" && is_numeric($objId)) :
|
|
if ($cascadingObjType = "jb") : $hqId = getFieldValueFromId("job","jb_id",$objId,"hq_id"); endif;
|
|
if ($cascadingObjType = "cs") : $hqId = getFieldValueFromId("customer","cs_id",$objId,"hq_id"); endif;
|
|
if ($cascadingObjType = "cr") : $hqId = getFieldValueFromId("courier","cr_id",$objId,"hq_id"); endif;
|
|
endif;
|
|
|
|
// Define constants
|
|
if ($mailSenderAddress == "") :
|
|
$mailSenderAddress = getParameterValue("0", "MAIL_SENDER_ADDRESS", $hqId);
|
|
if ($mailSenderAddress == "") : $mailSenderAddress = getParameterValue("0", "MAIL_SENDER_ADDRESS", "0"); endif;
|
|
endif;
|
|
|
|
$currentTime = getDateTime("0");
|
|
if ($mailAttachedFileType == "") : $mailAttachedFileType = "pdf"; endif;
|
|
if ($pageTitel == "") : $pageTitel = $currentTime; endif;
|
|
|
|
// Check for sending mail if requested
|
|
if ($mailAttachedFileName != "" && $mailSenderAddress != "" && $mailToAddresses != "" && $hqId != "") :
|
|
$sendMail = true;
|
|
if (strpos($mailAttachedFileName, ".") === FALSE) :
|
|
$mailAttachedFileName .= "." . $mailAttachedFileType;
|
|
endif;
|
|
endif;
|
|
|
|
if ($sendMail) :
|
|
// Get HTML from sniplet
|
|
$htmlOut = generateOutputHTML($htmlSnip);
|
|
|
|
// Take default log for mails triggered by CRON independent from the type
|
|
if ($logFile == "" && $parKeyForLogFile != "") : $logFile = getParameterValue("0", $parKeyForLogFile, $hqId); endif;
|
|
if ($logFile == "" && $parKeyForLogFile != "") : $logFile = getParameterValue("0", $parKeyForLogFile, "0"); endif;
|
|
if ($logFile == "") : $logFile = "../log/automailer_pdf.log"; endif;
|
|
|
|
// Send mail to admin@assecutor.de
|
|
$mailObj = new htmlMimeMail();
|
|
$mailObj->setFrom($mailSenderAddress);
|
|
if ($mailCcAddresses != "") :
|
|
$mailObj->setCc($mailCcAddresses);
|
|
endif;
|
|
if ($mailBccAddresses != "") :
|
|
$mailObj->setBcc($mailBccAddresses);
|
|
endif;
|
|
$mailObj->setSubject($mailSubject);
|
|
|
|
// Mail content
|
|
// $mailObj->setHtml($htmlOut, null, "./");
|
|
$mailObj->setText($mailText);
|
|
|
|
// Attachment
|
|
if (file_exists($mailAttachedFileName)) :
|
|
$attachment = $mailObj->getFile($mailAttachedFileName);
|
|
if ($mailAttachedFileType == "pdf") :
|
|
$mailObj->addAttachment($attachment, $pageTitel . "." . $mailAttachedFileType, 'application/pdf');
|
|
// elseif ($mailAttachedFileType == "ps") :
|
|
// $mailObj->addAttachment($attachment, $pageTitel, 'application/????');
|
|
else :
|
|
$mailObj->addAttachment($attachment, $pageTitel . "." . $mailAttachedFileType, 'application/pdf');
|
|
endif;
|
|
endif;
|
|
|
|
// $writeToLogfile = true;
|
|
$mailResult = $mailObj->send(array($mailToAddresses), 'smtp');
|
|
if ($mailResult) :
|
|
// Write logdata into log file if not have been done before
|
|
// if ($writeToLogfile) :
|
|
// writeToFile($logFile, "[" . $pageTitel . "] [Status: OK] [Time: " . $currentTime . "] [Object: " . $objId . "] [Type: " . $cascadingObjType . "] [To: " . $mailToAddresses . "] [Cc: " . $mailCcAddresses . "] [Bcc: " . $mailBccAddresses . "]");
|
|
// endif;
|
|
else :
|
|
// Write logdata into log file if not have been done before
|
|
// if ($writeToLogfile) :
|
|
// writeToFile($logFile, "[" . $pageTitel . "] [Status: NOT_OK] [Time: " . $currentTime . "] [Object: " . $objId . "] [Type: " . $cascadingObjType . "] [To: " . $mailToAddresses . "] [Cc: " . $mailCcAddresses . "] [Bcc: " . $mailBccAddresses . "]");
|
|
// endif;
|
|
$sendMail = false;
|
|
endif;
|
|
$mailObj = NULL;
|
|
else :
|
|
// Write logdata into log file if not have been done before
|
|
if ($writeToLogfile) :
|
|
writeToFile($logFile, "[" . $pageTitel . "] [Status: NOT_OK] [Time: " . $currentTime . "] [Filename: " . $mailAttachedFileName . "] [Sender: " . $mailSenderAddress . "] [To: " . $mailToAddresses . "] [Hq: " . $hqId . "]");
|
|
endif;
|
|
endif;
|
|
|
|
return $sendMail;
|
|
}
|
|
?>
|