久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      玩轉(zhuǎn)PHP之快速制作Word簡(jiǎn)歷

      PHP制作word簡(jiǎn)歷

      PHP操作word有一個(gè)非常好用的輪子,就是phpword,該輪子可以在github上查找到(PHPOffice/PHPWord)。上面有較為詳細(xì)的例子和代碼,其中里面的源碼包含有一些常用的操作例子,包括設(shè)置頁(yè)眉、頁(yè)腳、頁(yè)碼、字體樣式、表格、插入圖片等常用的操作。這里介紹的是如何使用該輪子來(lái)制作一個(gè)簡(jiǎn)歷。

      模板替換的方式制作簡(jiǎn)歷

      在許多招聘網(wǎng)站都有一個(gè)簡(jiǎn)歷下載的功能,如何用php實(shí)現(xiàn)呢?在PHPOffice/PHPWord里面就有一個(gè)非常簡(jiǎn)單的生成一個(gè)word文檔,向文檔中插入一些文字。這里我使用的方式比較取巧,這個(gè)輪子的說(shuō)明文檔中有template processing,我理解為模板替換,也就是跟laravel的blade模板一個(gè)概念。接下來(lái)就不多廢話,直接說(shuō)如何操作,這里提一句使用的是laravel框架。

      1.安裝PHPOffice/PHPWord

      composer require phpoffice/phpword

      2.創(chuàng)建控制器DocController及test方法用于測(cè)試,并建立路由。

      php artisan make:controller DocController

      3.建立word模板,這里說(shuō)明一下,該輪子替換的是word文檔中格式為${value}格式的字符串,這里我簡(jiǎn)易的搭建一個(gè)模板如下圖1所示:
      玩轉(zhuǎn)PHP之快速制作Word簡(jiǎn)歷

      從圖中可以看到有一些基本的信息,這些可以從數(shù)據(jù)庫(kù)中撈取數(shù)據(jù)。不過(guò)這次是直接使用替換的方式,像工作經(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);//該表通過(guò)克隆行的方式,形成兩行         $templateProcessor->setValue('experience_time#1', '2010-09~2014-06');//每行參數(shù)是用value#X(X表示行號(hào),從1開(kāi)始)         $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');          //語(yǔ)言能力         $templateProcessor->cloneRow('lang',2);         $templateProcessor->setValue('lang#1', '漢語(yǔ)|精通');         $templateProcessor->setValue('lang#2', '英語(yǔ)|精通');          //技能         $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');

      這樣就可以通過(guò)建立word模板的方式產(chǎn)生一個(gè)簡(jiǎn)歷了。以上內(nèi)容沒(méi)有提到如何將圖片替換進(jìn)去,如果你查看文檔的話會(huì)發(fā)現(xiàn)這個(gè)包的模板替換并沒(méi)有說(shuō)怎么替換圖片,因?yàn)楹孟駢焊@種方式就沒(méi)有提供,暈死。不過(guò)github的issue中有人提出了這個(gè)問(wèn)題并且也有人給出了解決方案。下面我就來(lái)說(shuō)說(shuō)如何實(shí)現(xiàn)將圖片替換進(jìn)去的功能。

      替換圖片

      假設(shè)你的簡(jiǎn)歷模板中有個(gè)表格單元格中要插入一張圖片,如下:
      玩轉(zhuǎn)PHP之快速制作Word簡(jiǎn)歷

      我要將public/img下的against the current.jpg圖片替換進(jìn)去,而源代碼沒(méi)有將圖片替換進(jìn)word的功能,所以只能自己編寫了。

      • 1.修改composer.json,將TemplateDocx類自動(dòng)加載進(jìn)來(lái):
      "autoload": {         "classmap": [             "database/seeds",             "database/factories",             "app/Core/TemplateDocx.php"         ],         "psr-4": {             "App\": "app/"         }     },

      運(yùn)行下列代碼:

      composer dump-autoload
      • 2.實(shí)現(xiàn)TemplateDocx類:

      該類的內(nèi)容我直接放在我的gist上了,連接TemplateDocx.php

      由于code是放在gist上,國(guó)內(nèi)訪問(wèn)不了所以我直接把code貼出來(lái),如下:

      <?php namespace AppCore; use PhpOfficePhpWordExceptionException; use PhpOfficePhpWordTemplateProcessor; class TemplateDocx extends TemplateProcessor {     /**      * @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception      *      * @param string $documentTemplate The fully qualified template filename      *      * @throws PhpOfficePhpWordExceptionCreateTemporaryFileException      * @throws PhpOfficePhpWordExceptionCopyFileException      */     public function __construct($documentTemplate)     {         parent::__construct($documentTemplate);         //添加下列屬性,后面會(huì)用到         $this->_countRels = 100; //start id for relationship between image and document.xml         $this->_rels = '';         $this->_types = '';     }     /**      * Saves the result document.      *      * @throws PhpOfficePhpWordExceptionException      *      * @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;     }     /**      * 實(shí)現(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);//該表通過(guò)克隆行的方式,形成兩行         $templateProcessor->setValue('key#1', '2010-09~2014-06');//每行參數(shù)是用value#X(X表示行號(hào),從1開(kāi)始)         $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.運(yùn)行結(jié)果

      玩轉(zhuǎn)PHP之快速制作Word簡(jiǎn)歷

      至此就可以產(chǎn)生簡(jiǎn)歷啦,如果這篇文章對(duì)你有所幫助記得點(diǎn)贊哦,親!如果有任何問(wèn)題可以留言??!(* ̄︶ ̄)

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)