Home  >  Article  >  Backend Development  >  Make word resume with PHP

Make word resume with PHP

不言
不言Original
2018-04-03 15:59:355452browse

PHP has a very easy-to-use wheel for operating word, which is phpword. This wheel can be found on github (PHPOffice/PHPWord). There are more detailed examples and codes above, and the source code inside contains some common operation examples, including common operations such as setting headers, footers, page numbers, font styles, tables, inserting pictures, etc. Here's how to use the wheel to build a resume.

Many recruitment websites have a resume download function. How to implement it using PHP? In PHPOffice/PHPWord, there is a very simple way to generate a word document and insert some text into the document. The method I use here is rather clever. The documentation for this wheel includes template processing, which I understand to be template replacement, which is the same concept as laravel's blade template. I won’t talk too much next, let’s just talk about how to operate it. I’d like to mention that the laravel framework is used.

1. Install PHPOffice/PHPWord

composer require phpoffice/phpword

2. Create the controller DocController and test method for testing, and establish routing.

php artisan make:controller DocController

3. Create a word template. Let me explain here. The wheel replaces the string in the ${value} format in the word document. Here I simply build a template as shown in Figure 1 below:
Make word resume with PHP
You can see from the picture that there is some basic information, which can be used to retrieve data from the database. However, this time the replacement method is used directly. For multi-line tables such as work experience and education experience, only one line is needed as a template.

4. Specific code

//load template docx
        $templateProcessor = new TemplateProcessor('./sample.docx');

        //基础信息填写替换
        $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', '待业/aaa');
        $templateProcessor->setValue('liveArea', '福建省莆田市涵江区');
        $templateProcessor->setValue('domicile', '福建省莆田市涵江区');
        $templateProcessor->setValue('address', '');
        $templateProcessor->setValue('hopetodo', 'IT');
        $templateProcessor->setValue('hopeworkin', '互联网');
        $templateProcessor->setValue('hopes', '7000+');
        $templateProcessor->setValue('worklocation', '福建省莆田市');
        $templateProcessor->setValue('phone', '123456789');
        $templateProcessor->setValue('mail', '456789@qq.com');
        $templateProcessor->setValue('qqnum', '456789');
        $templateProcessor->setValue('selfjudge', '哇哈哈哈哈哈哈哈');

        //工作经历表格替换
        $templateProcessor->cloneRow('experience_time', 2);//该表通过克隆行的方式,形成两行
        $templateProcessor->setValue('experience_time#1', '2010-09~2014-06');//每行参数是用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');

        //教育经历
        $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');

This way you can generate a resume by creating a word template.

Related recommendations:

How to make a circular user avatar in PHP

How to make a millisecond timestamp in PHP

How to use Thinkphp to create a web live broadcast




##

The above is the detailed content of Make word resume with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn