求助:TCPDF多语言PDF页眉页脚图片切换遇致命错误
问题:根据网站选中语言切换TCPDF页眉页脚图片
我使用TCPDF开发产品数据单PDF生成器,可生成包含产品描述、特性、标准、技术参数的PDF文档。目前已实现PDF中文本内容的多语言切换,但在尝试根据网站选中的语言切换PDF页眉页脚图片时遇到问题。
相关OCPDF类代码
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require_once DIR_SYSTEM . 'config/si/tcpdf/tcpdf.php'; class OCPDF extends TCPDF { public function Header() { if ($this->request->server['HTTPS']) { $server = HTTP_SERVER; } else { $server = HTTPS_SERVER; } //$language = $this->config->get('config_language'); ERROR IT WAS A TEST BUT DOEST WORK $language = 'pt-pt'; // Replace with the actual language code if (isset($this->session->data['language'])) { $language = $this->session->data['language']; } // Set the image file based on the language $logo_file = 'image/catalog/pdftemplate/header.png'; if ($language == 'en-gb') { $logo_file = $server . 'image/catalog/pdftemplate/header-en.png'; } elseif ($language == 'es') { $logo_file = $server . 'image/catalog/pdftemplate/header-es.png'; } elseif ($language == 'pt-pt') { $logo_file = $server . 'image/catalog/pdftemplate/header-pt.png'; } $header_details_address = $this->header_details_address; $header_details_email = $this->header_details_email; echo $language; $header_html = <<<EOD <img src ="$logo_file" height="190px"> <br/><br/><br/><br/> EOD; // Print text using writeHTMLCell() $this->writeHTMLCell(0, 0, '', '', $header_html, 0, 1, 0, true, '', true); } // Page footer public function Footer() { // Position at 15 mm from bottom $this->SetY(-25); // Set font $this->SetFont('helvetica', 'I', 8); // Page number if ($this->request->server['HTTPS']) { $server = HTTP_SERVER; } else { $server = HTTPS_SERVER; } $mlogo_file = $server.'image/catalog/pdftemplate/footer.png' ; $footer_logo_html = '<div style="width:100% !important;height:190px;"><img width:"100%;" src="' . $footer_image_file . '" /></div>'; $this->writeHTMLCell(0, 0, $this->Image($mlogo_file, '0', $this->GetY()+2, '210', 24, '', '', 'T', false, 300, '', false, false, 1, false, false, false), 0, false, 'C', 0, '', 0, false, 'T', 'M'); } public function setImageFile($logo_file) { $this->logo_file = $logo_file ; } public function setStoreheader_details($header_details_address ,$header_details_email) { $this->header_details_address=$header_details_address; $this->header_details_email = $header_details_email; } }
尝试获取语言的代码片段
//$language = $this->config->get('config_language'); ERROR IT WAS A TEST BUT DOEST WORK $language = 'pt-pt'; // Replace with the actual language code if (isset($this->session->data['language'])) { $language = $this->session->data['language']; } // Set the image file based on the language $logo_file = 'image/catalog/pdftemplate/header.png'; if ($language == 'en-gb') { $logo_file = $server . 'image/catalog/pdftemplate/header-en.png'; } elseif ($language == 'es') { $logo_file = $server . 'image/catalog/pdftemplate/header-es.png'; } elseif ($language == 'pt-pt') { $logo_file = $server . 'image/catalog/pdftemplate/header-pt.png'; }
文本多语言切换的控制器代码
class ControllerExtensionModulesiproductpdf extends Controller { private $modulePath; public function __construct($registry) { parent::__construct($registry); $this->config->load('si/siproductpdf'); $this->modulePath = $this->config->get('siproductpdf_path'); } public function index($settings){ /// if (isset($this->request->get['product_id'])) { $product_id = (int)$this->request->get['product_id']; } else { $product_id = 0; } $this->document->addStyle('catalog/view/theme/default/stylesheet/siproductpdf.css'); //$data['moduleData'] = $settings['siproductpdf']; $data['status'] = (isset($settings['status'])) ? $settings['status'] : false; $data['language_id'] = $this->config->get('config_language_id'); //HERE $data['pdffile'] = HTTPS_SERVER . 'index.php?route=extension/module/siproductpdf/downloadproductpdf&product_id='.$product_id; return $this->load->view($this->modulePath, $data); }
错误信息
当启用$language = $this->config->get('config_language');时,出现如下致命错误:
Fatal error: Uncaught Error: Call to a member function get() on null in /home/coelhost/cls.pt/catalog/controller/extension/module/siproductpdf.php:17 Stack trace: #0 /home/coelhost/cls.pt/system/config/si/tcpdf/tcpdf.php(3541): OCPDF->Header() #1 /home/coelhost/cls.pt/system/config/si/tcpdf/tcpdf.php(3208): TCPDF->setHeader() #2 /home/coelhost/cls.pt/system/config/si/tcpdf/tcpdf.php(3120): TCPDF->startPage('', '', false) #3 /home/coelhost/cls.pt/catalog/controller/extension/module/siproductpdf.php(273): TCPDF->AddPage() #4 /home/coelhost/cls.pt/vqmod/vqcache/vq2-storage_modification_system_engine_action.php(79): ControllerExtensionModulesiproductpdf->downloadproductpdf() #5 /home/coelhost/cls.pt/catalog/controller/startup/router.php(25): Action->execute(Object(Registry)) #6 /home/coelhost/cls.pt/vqmod/vqcache/vq2-storage_modification_system_engine_action.php(79): ControllerStartupRouter->index() #7 /home/coelhost/cls.pt/vqmod/vqcache/vq2-storage_modification_system_engine_router.php(130): Action->execute(Object(Registry)) in /home/coelhost/cls.pt/catalog/controller/extension/module/siproductpdf.php on line 17
解决方案
问题根源
OCPDF类中直接调用$this->config或$this->session会报错,因为这些对象未注入到TCPDF子类中,导致调用get()方法时出现“Call to a member function get() on null”错误。
解决步骤
- 在控制器中获取当前语言
在生成PDF的控制器方法(如downloadproductpdf)中,先获取网站选中的语言:
// 在downloadproductpdf方法内 $language = $this->config->get('config_language'); // 优先从session获取用户选中的语言 if (isset($this->session->data['language'])) { $language = $this->session->data['language']; }
- 为OCPDF类添加语言设置方法
修改OCPDF类,新增存储和设置语言的属性与方法:
class OCPDF extends TCPDF { private $language; public function setLanguage($lang) { $this->language = $lang; } // 其他原有方法... }
- 在Header/Footer方法中使用已设置的语言
修改Header和Footer方法,直接使用$this->language变量:
// 修改后的Header方法 public function Header() { $server = $this->request->server['HTTPS'] ? HTTP_SERVER : HTTPS_SERVER; $language = $this->language; $logo_file = $server . 'image/catalog/pdftemplate/header.png'; if ($language == 'en-gb') { $logo_file = $server . 'image/catalog/pdftemplate/header-en.png'; } elseif ($language == 'es') { $logo_file = $server . 'image/catalog/pdftemplate/header-es.png'; } elseif ($language == 'pt-pt') { $logo_file = $server . 'image/catalog/pdftemplate/header-pt.png'; } $header_details_address = $this->header_details_address; $header_details_email = $this->header_details_email; $header_html = <<<EOD <img src="$logo_file" height="190px"> <br/><br/><br/><br/> EOD; $this->writeHTMLCell(0, 0, '', '', $header_html, 0, 1, 0, true, '', true); } // 修改后的Footer方法 public function Footer() { $this->SetY(-25); $this->SetFont('helvetica', 'I', 8); $server = $this->request->server['HTTPS'] ? HTTP_SERVER : HTTPS_SERVER; $language = $this->language; $mlogo_file = $server . 'image/catalog/pdftemplate/footer.png'; if ($language == 'en-gb') { $mlogo_file = $server . 'image/catalog/pdftemplate/footer-en.png'; } elseif ($language == 'es') { $mlogo_file = $server . 'image/catalog/pdftemplate/footer-es.png'; } elseif ($language == 'pt-pt') { $mlogo_file = $server . 'image/catalog/pdftemplate/footer-pt.png'; } // 修复原代码中$footer_image_file未定义的错误 $footer_logo_html = '<div style="width:100% !important;height:190px;"><img width="100%" src="' . $mlogo_file . '" /></div>'; $this->writeHTMLCell(0, 0, '', '', $footer_logo_html, 0, 1, 0, true, 'C', true); }
- 在控制器中实例化OCPDF并传递语言
// 在downloadproductpdf方法内 $pdf = new OCPDF(); $pdf->setLanguage($language); // 执行其他PDF生成逻辑(添加内容、设置参数等) $pdf->Output();
内容的提问来源于stack exchange,提问作者Diogo Amorim
相关产品推荐
相关产品推荐

