Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How PHP makes Word resumes quickly

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how PHP can quickly produce Word resumes. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

PHP makes word resume

PHP operation word has a very easy to use wheel, which is phpword.

Make a resume by replacing the template

There is a resume download function on many recruitment websites, how to achieve it with php? In PHPOffice/PHPWord, there is a very simple way to generate an word document and insert some text into the document. The way I use it here is more ingenious. There is template processing in the documentation of this wheel, which I understand as template replacement, which is the same concept as laravel's blade template. Next, let's cut the crap and just talk about how to do it. Here's a sentence that uses the laravel framework.

1. Install PHPOffice/PHPWord

Composer require phpoffice/phpword

two。 Create the controller DocController and test methods for testing and establishing routes.

Php artisan make:controller DocController

3. Create a word template. Here, the wheel replaces a string in the format of ${value} in the word document. Here, I can easily build a template as shown in figure 1 below:

You can see from the figure that there is some basic information that can be pulled from the database. This time, however, substitution is used directly, and in a multi-row table pattern such as work experience and educational experience, only one row is needed as a template.

4. Specific code

/ / load template docx $templateProcessor = new TemplateProcessor ('. / sample.docx'); / / basic information fill in and replace $templateProcessor- > setValue ('update_at', date (' Y-m-d Hlav')); $templateProcessor- > setValue ('number',' 123456'); $templateProcessor- > setValue ('Name',' Zhang San'); $templateProcessor- > setValue ('sex',' male') $templateProcessor- > setValue ('birth',' October 1996'); $templateProcessor- > setValue ('age',' 22'); $templateProcessor- > setValue ('shortcut',' unemployed / aaa'); $templateProcessor- > setValue ('liveArea',' Hanjiang District, Putian City, Fujian Province'); $templateProcessor- > setValue ('domicile',' Hanjiang District, Putian City, Fujian Province); $templateProcessor- > setValue ('address',') $templateProcessor- > setValue ('hopetodo',' IT'); $templateProcessor- > setValue ('hopeworkin',' Internet'); $templateProcessor- > setValue ('hopes',' 7000Internet'); $templateProcessor- > setValue ('worklocation',' Putian City, Fujian Province'); $templateProcessor- > setValue ('phone',' 123456789'); $templateProcessor- > setValue ('mail',' 456789 Internet q.com') $templateProcessor- > setValue ('qqnum',' 456789'); $templateProcessor- > setValue ('selfjudge',' Waha'); / / replace the work experience table with $templateProcessor- > cloneRow ('experience_time', 2); / / the table forms two rows of $templateProcessor- > setValue (' experience_time#1', '2010-09-2014-06') by cloning rows / / each line parameter is value#X (X represents line number, starting with 1) $templateProcessor- > setValue ('job#1',' ABC company CTO'); $templateProcessor- > setValue ('experience_time#2',' 2014-09 ~ present'); $templateProcessor- > setValue ('job#2',' JBC company CTO'); / / Educational experience $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 ~ present'); $templateProcessor- > setValue ('school#2',' JBC'); $templateProcessor- > setValue ('major#2',' Computer science') / / language ability $templateProcessor- > cloneRow ('lang',2); $templateProcessor- > setValue (' lang#1', 'Chinese | proficient'); $templateProcessor- > setValue ('lang#2',' English | proficient'); / / skills $templateProcessor- > cloneRow ('skill',3); $templateProcessor- > setValue (' skill#1', 'JAVA | proficient') $templateProcessor- > setValue ('skill#2',' Python | Mastering'); $templateProcessor- > setValue ('skill#3',' PHP | Mastering'); / / Saving the document $templateProcessor- > saveAs ('my.docx')

In this way, you can generate a resume by creating a word template. The above content does not mention how to replace the image, if you look at the document, you will find that the template replacement of this package does not say how to replace the image, because it seems that this way is not provided at all. But someone in github's issue has raised this problem and someone has come up with a solution. Next I will talk about how to achieve the function of replacing pictures.

Replace picture

Suppose you have a table cell in your resume template to insert a picture, as follows:

I want to replace the against the current.jpg picture under public/img, but the source code does not replace the picture with word, so I have to write it myself.

1. Modify composer.json to load the TemplateDocx class automatically:

"autoload": {"classmap": ["database/seeds", "database/factories", "app/Core/TemplateDocx.php"], "psr-4": {"App\": "app/"}}

Run the following code:

Composer dump-autoload

two。 Implement the TemplateDocx class:

I put the contents of this class directly on my gist and connect to TemplateDocx.php

Since code is placed on gist and cannot be accessed domestically, I will post the code directly, as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report