Permalink
Browse files

IP-444 - Update mPDF library to 6.1 and updates the gitignores

  • Loading branch information...
1 parent ec51367 commit 8516d742c37bf50ed5c9253c14f2d365a8f640cc @Kovah Kovah committed Oct 5, 2016
View
@@ -23,6 +23,9 @@
/application/views/quote_templates/public/
!/application/views/quote_templates/public/InvoicePlane*
+# Composer
+/vendor/
+composer.lock
/.htaccess
.idea
@@ -18,100 +18,97 @@
function pdf_create($html, $filename, $stream = true, $password = null, $isInvoice = null, $isGuest = null, $zugferd_invoice = false, $associatedFiles = null)
{
- require_once(APPPATH . 'helpers/mpdf/mpdf.php');
+ $CI = &get_instance();
+ // Get the invoice from the archive if available
+ $invoice_array = array();
+
+ // mPDF loading
+ define('_MPDF_TEMP_PATH', FCPATH . 'uploads/temp/mpdf/');
+ define('_MPDF_TTFONTDATAPATH', FCPATH . 'uploads/temp/mpdf/');
+
+ require_once(FCPATH . 'vendor/mpdf/mpdf/mpdf.php');
$mpdf = new mPDF();
+
+ // mPDF configuration
$mpdf->useAdobeCJK = true;
- $mpdf->SetAutoFont();
+ $mpdf->autoScriptToLang = true;
+ if (IP_DEBUG) {
+ // Enable image error logging
+ $mpdf->showImageErrors = true;
+ }
+
+ // Include zugferd if enabled
if ($zugferd_invoice) {
- $CI = &get_instance();
$CI->load->helper('zugferd');
$mpdf->PDFA = true;
$mpdf->PDFAauto = true;
$mpdf->SetAdditionalRdf(zugferd_rdf());
$mpdf->SetAssociatedFiles($associatedFiles);
- } else {
- // Avoid setting protection when password is blank/empty
- if (!empty($password)) {
- $mpdf->SetProtection(array('copy', 'print'), $password, $password);
- }
}
- if (!(is_dir('./uploads/archive/') || is_link('./uploads/archive/'))) {
- mkdir('./uploads/archive/', '0777');
+ // Set a password if set for the voucher
+ if (!empty($password)) {
+ $mpdf->SetProtection(array('copy', 'print'), $password, $password);
}
- // Enable image error logging
- if (IP_DEBUG) {
- $mpdf->showImageErrors = true;
+ // Check if the archive folder is available
+ if (!(is_dir('./uploads/archive/') || is_link('./uploads/archive/'))) {
+ mkdir('./uploads/archive/', '0777');
}
- if (strpos($filename, trans('invoice')) !== false) {
- $CI = &get_instance();
+ // Set the footer if voucher is invoice and if set in settings
+ if ($isInvoice && !empty($CI->mdl_settings->settings['pdf_invoice_footer'])) {
$mpdf->setAutoBottomMargin = 'stretch';
$mpdf->SetHTMLFooter('<div id="footer">' . $CI->mdl_settings->settings['pdf_invoice_footer'] . '</div>');
}
- $invoice_array = array();
$mpdf->WriteHTML($html);
- // If $stream is true (default) the PDF will be displayed directly in the browser, otherwise will be returned as a download
- if ($stream) {
-
- if (!$isInvoice) {
- return $mpdf->Output($filename . '.pdf', 'I');
- }
+ if ($isInvoice) {
- foreach (glob('./uploads/archive/*' . $filename . '.pdf') as $file) {
+ foreach (glob(UPLOADS_FOLDER . 'archive/*' . $filename . '.pdf') as $file) {
array_push($invoice_array, $file);
}
- if (!empty($invoice_array) && $isGuest) {
+ if (!empty($invoice_array) && !is_null($isGuest)) {
rsort($invoice_array);
- header('Content-type: application/pdf');
- return readfile($invoice_array[0]);
- } else
- if ($isGuest) {
- // @TODO flashdata is deleted between requests
- //$CI->session->flashdata('alert_error', 'sorry no Invoice found!');
- redirect('guest/view/invoice/' . end($CI->uri->segment_array()));
- }
-
- $mpdf->Output('./uploads/archive/' . date('Y-m-d') . '_' . $filename . '.pdf', 'F');
- return $mpdf->Output($filename . '.pdf', 'I');
-
- } else {
+ return $invoice_array[0];
+ }
- if ($isInvoice) {
+ $archived_file = UPLOADS_FOLDER . 'archive/' . date('Y-m-d') . '_' . $filename . '.pdf';
+ $mpdf->Output($archived_file, 'F');
- foreach (glob('./uploads/archive/*' . $filename . '.pdf') as $file) {
- array_push($invoice_array, $file);
- }
- if (!empty($invoice_array) && !is_null($isGuest)) {
- rsort($invoice_array);
- return $invoice_array[0];
- }
- $mpdf->Output('./uploads/archive/' . date('Y-m-d') . '_' . $filename . '.pdf', 'F');
- return './uploads/archive/' . date('Y-m-d') . '_' . $filename . '.pdf';
+ if ($stream) {
+ return $mpdf->Output($filename . '.pdf', 'I');
+ } else {
+ return $archived_file;
}
+ }
- $mpdf->Output('./uploads/temp/' . $filename . '.pdf', 'F');
+ // If $stream is true (default) the PDF will be displayed directly in the browser
+ // otherwise will be returned as a download
+ if ($stream) {
+ return $mpdf->Output($filename . '.pdf', 'I');
+ } else {
+
+ $mpdf->Output(UPLOADS_FOLDER . 'temp/' . $filename . '.pdf', 'F');
// Housekeeping
// Delete any files in temp/ directory that are >1 hrs old
$interval = 3600;
if ($handle = @opendir(preg_replace('/\/$/', '', './uploads/temp/'))) {
while (false !== ($file = readdir($handle))) {
if (($file != '..') && ($file != '.') && !is_dir($file) && ((filemtime('./uploads/temp/' . $file) + $interval) < time()) && (substr($file, 0, 1) !== '.') && ($file != 'remove.txt')) { // mPDF 5.7.3
- unlink('./uploads/temp/' . $file);
+ unlink(UPLOADS_FOLDER . 'temp/' . $file);
}
}
closedir($handle);
}
- // Return the pdf itself
- return './uploads/temp/' . $filename . '.pdf';
+ return UPLOADS_FOLDER . 'temp/' . $filename . '.pdf';
+
}
}
@@ -27,6 +27,7 @@ function generate_invoice_pdf($invoice_id, $stream = true, $invoice_template = n
$CI->load->helper('country');
$invoice = $CI->mdl_invoices->get_by_id($invoice_id);
+
if (!$invoice_template) {
$CI->load->helper('template');
$invoice_template = select_pdf_invoice_template($invoice);
@@ -38,6 +39,7 @@ function generate_invoice_pdf($invoice_id, $stream = true, $invoice_template = n
// Determine if discounts should be displayed
$items = $CI->mdl_items->where('invoice_id', $invoice_id)->get()->result();
$show_discounts = false;
+
foreach ($items as $item) {
if ($item->item_discount != '0.00') {
$show_discounts = true;
@@ -49,6 +51,7 @@ function generate_invoice_pdf($invoice_id, $stream = true, $invoice_template = n
if ($include_zugferd) {
$CI->load->helper('zugferd');
+
$associatedFiles = array(array(
'name' => 'ZUGFeRD-invoice.xml',
'description' => 'ZUGFeRD Invoice',
@@ -73,7 +76,7 @@ function generate_invoice_pdf($invoice_id, $stream = true, $invoice_template = n
$CI->load->helper('mpdf');
return pdf_create($html, trans('invoice') . '_' . str_replace(array('\\', '/'), '_', $invoice->invoice_number),
- $stream, $invoice->invoice_password, 1, $isGuest, $include_zugferd, $associatedFiles);
+ $stream, $invoice->invoice_password, true, $isGuest, $include_zugferd, $associatedFiles);
}
function generate_quote_pdf($quote_id, $stream = true, $quote_template = null)
@@ -245,13 +245,11 @@ private function check_writables()
$writables = array(
'./uploads',
- './uploads/temp',
'./uploads/archive',
'./uploads/customer_files',
+ './uploads/temp',
+ './uploads/temp/mpdf',
'./' . APPPATH . 'config/', // for database.php
- './' . APPPATH . 'helpers/mpdf/tmp',
- './' . APPPATH . 'helpers/mpdf/ttfontdata',
- './' . APPPATH . 'helpers/mpdf/graph_cache',
'./' . APPPATH . 'logs'
);
View
@@ -0,0 +1,5 @@
+{
+ "require": {
+ "mpdf/mpdf": "6.1.2"
+ }
+}
View
@@ -190,6 +190,9 @@
// Detect the URL subfolder
define('URL_SUBFOLDER', ($_SERVER['DOCUMENT_ROOT'] != str_replace('\\', '/', dirname(__FILE__)) ? str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', dirname(__FILE__))) : ''));
+// Set the uploads folder
+define('UPLOADS_FOLDER', FCPATH . 'uploads/');
+
/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
@@ -1,2 +1,3 @@
*
+!mpdf
!.gitignore
@@ -0,0 +1,2 @@
+*
+!.gitignore

0 comments on commit 8516d74

Please sign in to comment.