這篇文章將為大家詳細(xì)講解有關(guān)PHP如何快速制作Word簡歷的方式,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),新昌企業(yè)網(wǎng)站建設(shè),新昌品牌網(wǎng)站建設(shè),網(wǎng)站定制,新昌網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,新昌網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
PHP制作word簡歷
PHP操作word有一個非常好用的輪子,就是phpword。
在許多招聘網(wǎng)站都有一個簡歷下載的功能,如何用php實現(xiàn)呢?在PHPOffice/PHPWord里面就有一個非常簡單的生成一個word文檔,向文檔中插入一些文字。這里我使用的方式比較取巧,這個輪子的說明文檔中有template processing,我理解為模板替換,也就是跟laravel的blade模板一個概念。接下來就不多廢話,直接說如何操作,這里提一句使用的是laravel框架。
1.安裝PHPOffice/PHPWord
composer require phpoffice/phpword
2.創(chuàng)建控制器DocController及test方法用于測試,并建立路由。
php artisan make:controller DocController
3.建立word模板,這里說明一下,該輪子替換的是word文檔中格式為${value}格式的字符串,這里我簡易的搭建一個模板如下圖1所示:
從圖中可以看到有一些基本的信息,這些可以從數(shù)據(jù)庫中撈取數(shù)據(jù)。不過這次是直接使用替換的方式,像工作經(jīng)歷和教育經(jīng)歷這種多行表格的模式這里也只需要取一行作為模板即可。
4.具體代碼
//load template docx $templateProcessor = new TemplateProcessor('./sample.docx'); //基礎(chǔ)信息填寫替換 $templateProcessor->setValue('update_at', date('Y-m-d H:i:s')); $templateProcessor->setValue('number', '123456'); $templateProcessor->setValue('Name', '張三'); $templateProcessor->setValue('sex', '男'); $templateProcessor->setValue('birth', '1996年10月'); $templateProcessor->setValue('age', '22'); $templateProcessor->setValue('shortcut', '待業(yè)/aaa'); $templateProcessor->setValue('liveArea', '福建省莆田市涵江區(qū)'); $templateProcessor->setValue('domicile', '福建省莆田市涵江區(qū)'); $templateProcessor->setValue('address', ''); $templateProcessor->setValue('hopetodo', 'IT'); $templateProcessor->setValue('hopeworkin', '互聯(lián)網(wǎng)'); $templateProcessor->setValue('hopes', '7000+'); $templateProcessor->setValue('worklocation', '福建省莆田市'); $templateProcessor->setValue('phone', '123456789'); $templateProcessor->setValue('mail', '456789@qq.com'); $templateProcessor->setValue('qqnum', '456789'); $templateProcessor->setValue('selfjudge', '哇哈哈哈哈哈哈哈'); //工作經(jīng)歷表格替換 $templateProcessor->cloneRow('experience_time', 2);//該表通過克隆行的方式,形成兩行 $templateProcessor->setValue('experience_time#1', '2010-09~2014-06');//每行參數(shù)是用value#X(X表示行號,從1開始) $templateProcessor->setValue('job#1', 'ABC company CTO'); $templateProcessor->setValue('experience_time#2', '2014-09~至今'); $templateProcessor->setValue('job#2', 'JBC company CTO'); //教育經(jīng)歷 $templateProcessor->cloneRow('time', 2); $templateProcessor->setValue('time#1', '2010-09~2014-06'); $templateProcessor->setValue('school#1', 'ABC'); $templateProcessor->setValue('major#1', 'Computer science'); $templateProcessor->setValue('time#2', '2014-09~至今'); $templateProcessor->setValue('school#2', 'JBC'); $templateProcessor->setValue('major#2', 'Computer science'); //語言能力 $templateProcessor->cloneRow('lang',2); $templateProcessor->setValue('lang#1', '漢語|精通'); $templateProcessor->setValue('lang#2', '英語|精通'); //技能 $templateProcessor->cloneRow('skill',3); $templateProcessor->setValue('skill#1', 'JAVA|精通'); $templateProcessor->setValue('skill#2', 'Python|精通'); $templateProcessor->setValue('skill#3', 'PHP|精通'); // Saving the document $templateProcessor->saveAs('my.docx');
這樣就可以通過建立word模板的方式產(chǎn)生一個簡歷了。以上內(nèi)容沒有提到如何將圖片替換進(jìn)去,如果你查看文檔的話會發(fā)現(xiàn)這個包的模板替換并沒有說怎么替換圖片,因為好像壓根這種方式就沒有提供,暈死。不過github的issue中有人提出了這個問題并且也有人給出了解決方案。下面我就來說說如何實現(xiàn)將圖片替換進(jìn)去的功能。
假設(shè)你的簡歷模板中有個表格單元格中要插入一張圖片,如下:
我要將public/img下的against the current.jpg圖片替換進(jìn)去,而源代碼沒有將圖片替換進(jìn)word的功能,所以只能自己編寫了。
1.修改composer.json,將TemplateDocx類自動加載進(jìn)來:
"autoload": { "classmap": [ "database/seeds", "database/factories", "app/Core/TemplateDocx.php" ], "psr-4": { "App\\": "app/" } },
運行下列代碼:
composer dump-autoload
2.實現(xiàn)TemplateDocx類:
該類的內(nèi)容我直接放在我的gist上了,連接TemplateDocx.php
由于code是放在gist上,國內(nèi)訪問不了所以我直接把code貼出來,如下:
<?php namespace App\Core; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\TemplateProcessor; class TemplateDocx extends TemplateProcessor { /** * @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception * * @param string $documentTemplate The fully qualified template filename * * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException * @throws \PhpOffice\PhpWord\Exception\CopyFileException */ public function __construct($documentTemplate) { parent::__construct($documentTemplate); //添加下列屬性,后面會用到 $this->_countRels = 100; //start id for relationship between image and document.xml $this->_rels = ''; $this->_types = ''; } /** * Saves the result document. * * @throws \PhpOffice\PhpWord\Exception\Exception * * @return string */ public function save() { foreach ($this->tempDocumentHeaders as $index => $xml) { $this->zipClass->addFromString($this->getHeaderName($index), $xml); } $this->zipClass->addFromString($this->getMainPartName(), $this->tempDocumentMainPart); /*****************重寫原有的save方法中添加的內(nèi)容******************/ if ($this->_rels != "") { $this->zipClass->addFromString('word/_rels/document.xml.rels', $this->_rels); } if ($this->_types != "") { $this->zipClass->addFromString('[Content_Types].xml', $this->_types); } /*********************我是分割線******************************/ foreach ($this->tempDocumentFooters as $index => $xml) { $this->zipClass->addFromString($this->getFooterName($index), $xml); } // Close zip file if (false === $this->zipClass->close()) { throw new Exception('Could not close zip file.'); } return $this->tempDocumentFilename; } /** * 實現(xiàn)將圖片替換進(jìn)word穩(wěn)定的方法 * @param $strKey * @param $img */ public function setImg($strKey, $img){ $strKey = '${'.$strKey.'}'; $relationTmpl = '<Relationship Id="RID" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/IMG"/>'; $imgTmpl = '<w:pict><v:shape type="#_x0000_t75" style="width:WIDpx;height:HEIpx"><v:imagedata r:id="RID" o:title=""/></v:shape></w:pict>'; $toAdd = $toAddImg = $toAddType = ''; $aSearch = array('RID', 'IMG'); $aSearchType = array('IMG', 'EXT'); $countrels=$this->_countRels++; //I'm work for jpg files, if you are working with other images types -> Write conditions here $imgExt = 'jpg'; $imgName = 'img' . $countrels . '.' . $imgExt; $this->zipClass->deleteName('word/media/' . $imgName); $this->zipClass->addFile($img['src'], 'word/media/' . $imgName); $typeTmpl = '<Override PartName="/word/media/'.$imgName.'" ContentType="image/EXT"/>'; $rid = 'rId' . $countrels; $countrels++; list($w,$h) = getimagesize($img['src']); if(isset($img['swh'])) //Image proportionally larger side { if($w<=$h) { $ht=(int)$img['swh']; $ot=$w/$h; $wh=(int)$img['swh']*$ot; $wh=round($wh); } if($w>=$h) { $wh=(int)$img['swh']; $ot=$h/$w; $ht=(int)$img['swh']*$ot; $ht=round($ht); } $w=$wh; $h=$ht; } if(isset($img['size'])) { $w = $img['size'][0]; $h = $img['size'][1]; } $toAddImg .= str_replace(array('RID', 'WID', 'HEI'), array($rid, $w, $h), $imgTmpl) ; if(isset($img['dataImg'])) { $toAddImg.='<w:br/><w:t>'.$this->limpiarString($img['dataImg']).'</w:t><w:br/>'; } $aReplace = array($imgName, $imgExt); $toAddType .= str_replace($aSearchType, $aReplace, $typeTmpl) ; $aReplace = array($rid, $imgName); $toAdd .= str_replace($aSearch, $aReplace, $relationTmpl); $this->tempDocumentMainPart=str_replace('<w:t>' . $strKey . '</w:t>', $toAddImg, $this->tempDocumentMainPart); //print $this->tempDocumentMainPart; if($this->_rels=="") { $this->_rels=$this->zipClass->getFromName('word/_rels/document.xml.rels'); $this->_types=$this->zipClass->getFromName('[Content_Types].xml'); } $this->_types = str_replace('</Types>', $toAddType, $this->_types) . '</Types>'; $this->_rels = str_replace('</Relationships>', $toAdd, $this->_rels) . '</Relationships>'; } }
3.使用方法:
$templateProcessor = new TemplateDocx('./sample.docx'); $imgPath = './img/against the current.jpg'; $templateProcessor->setImg('img', array( 'src' => $imgPath, //圖片路徑 'size' => array( 150, 150 ) //圖片大小,單位px )); $templateProcessor->setValue('name', 'Sun'); $templateProcessor->cloneRow('key', 2);//該表通過克隆行的方式,形成兩行 $templateProcessor->setValue('key#1', '2010-09~2014-06');//每行參數(shù)是用value#X(X表示行號,從1開始) $templateProcessor->setValue('val#1', 'ABC company CTO'); $templateProcessor->setValue('key#2', '2014-09~至今'); $templateProcessor->setValue('val#2', 'JBC company CTO'); // $templateProcessor->setValue('img', 'Sun'); $templateProcessor->saveAs('my.docx');
4.運行結(jié)果
關(guān)于PHP如何快速制作Word簡歷的方式就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
新聞標(biāo)題:PHP如何快速制作Word簡歷的方式
標(biāo)題URL:http://aaarwkj.com/article20/ihhpjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、服務(wù)器托管、網(wǎng)站收錄、微信小程序、全網(wǎng)營銷推廣、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)