From 840dcfec38961b3f3e81dd4ecaa61f3fbf846b70 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Mon, 29 Apr 2013 09:25:42 +0200
Subject: [PATCH] Fix: Bad detection of PDF format.

---
 htdocs/core/lib/functions2.lib.php | 16 ++++++++++------
 htdocs/core/lib/invoice2.lib.php   |  2 +-
 htdocs/core/lib/pdf.lib.php        | 13 +++++++------
 3 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php
index dc6c57002c9..d8f86063e18 100644
--- a/htdocs/core/lib/functions2.lib.php
+++ b/htdocs/core/lib/functions2.lib.php
@@ -105,14 +105,18 @@ function dolGetModulesDirs($subdir='')
 /**
  *  Try to guess default paper format according to language into $langs
  *
- *	@return		string		Defautl paper format code
+ *	@param		Translate	$outputlangs		Output lang to use to autodetect output format if setup not done
+ *	@return		string							Default paper format code
  */
-function dol_getDefaultFormat()
+function dol_getDefaultFormat($outputlangs='')
 {
     global $langs;
+
     $selected='EUA4';
-    if ($langs->defaultlang == 'ca_CA') $selected='CAP4';        // Canada
-    if ($langs->defaultlang == 'en_US') $selected='USLetter';    // US
+    if (empty($outputlangs) || ! is_object($outputlangs)) $outputlangs=$langs;
+
+    if ($outputlangs->defaultlang == 'ca_CA') $selected='CAP4';        // Canada
+    if ($outputlangs->defaultlang == 'en_US') $selected='USLetter';    // US
     return $selected;
 }
 
@@ -739,7 +743,7 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
         $maskLike = str_replace(dol_string_nospecial('{'.$masktri.'}'),$counterpadded,$maskLike);
         if ($maskrefclient) $maskLike = str_replace(dol_string_nospecial('{'.$maskrefclient.'}'),str_pad("",dol_strlen($maskrefclient),"_"),$maskLike);
         if ($masktype) $maskLike = str_replace(dol_string_nospecial('{'.$masktype.'}'),$masktype_value,$maskLike);
-        
+
         $ref='';
         $sql = "SELECT ".$field." as ref";
         $sql.= " FROM ".MAIN_DB_PREFIX.$table;
@@ -748,7 +752,7 @@ function get_next_value($db,$mask,$table,$field,$where='',$objsoc='',$date='',$m
         $sql.= " AND entity IN (".getEntity($table, 1).")";
         if ($where) $sql.=$where;
         if ($sqlwhere) $sql.=' AND '.$sqlwhere;
-        
+
         dol_syslog("functions2::get_next_value sql=".$sql);
         $resql=$db->query($sql);
         if ($resql)
diff --git a/htdocs/core/lib/invoice2.lib.php b/htdocs/core/lib/invoice2.lib.php
index 057dffd7d13..715ff2f877a 100644
--- a/htdocs/core/lib/invoice2.lib.php
+++ b/htdocs/core/lib/invoice2.lib.php
@@ -174,7 +174,7 @@ function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filte
 
 
 	        // Define format of output PDF
-	        $formatarray=pdf_getFormat();
+	        $formatarray=pdf_getFormat($langs);
 	        $page_largeur = $formatarray['width'];
 	        $page_hauteur = $formatarray['height'];
 	        $format = array($page_largeur,$page_hauteur);
diff --git a/htdocs/core/lib/pdf.lib.php b/htdocs/core/lib/pdf.lib.php
index 4fc377f896c..a9f882dd4e1 100644
--- a/htdocs/core/lib/pdf.lib.php
+++ b/htdocs/core/lib/pdf.lib.php
@@ -29,11 +29,12 @@
 
 
 /**
- *      Return array with format properties of default PDF format
-*
-*      @return     array		Array('width'=>w,'height'=>h,'unit'=>u);
-*/
-function pdf_getFormat()
+ *	Return array with format properties of default PDF format
+ *
+ *	@param		Translate	$outputlangs		Output lang to use to autodetect output format if setup not done
+ *  @return     array							Array('width'=>w,'height'=>h,'unit'=>u);
+ */
+function pdf_getFormat($outputlangs='')
 {
 	global $conf,$db;
 
@@ -43,7 +44,7 @@ function pdf_getFormat()
 	if (empty($conf->global->MAIN_PDF_FORMAT))
 	{
 		include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
-		$pdfformat=dol_getDefaultFormat();
+		$pdfformat=dol_getDefaultFormat($outputlangs);
 	}
 	else $pdfformat=$conf->global->MAIN_PDF_FORMAT;
 
-- 
GitLab