Laravel generate Word document - phpword

In the actual development or learning of a project, there is a need to export data to generate word documents. Here we use the elegant and popular laravel framework to simply teach you how to implement it.
phpword, which can easily generate word documents, can also add tables, directories, pictures, hyperlinks, headers, footers and other powerful functions in word.

Install phpWord

Requirement

Mandatory:

PHP 5.3.3+

  • XML parser extension
  • Zend \ escape components
  • Zend \ Stdlib components
  • Zend \ Validator component

Optional:

-Zip code extension
-GD extension
-XMLWriter extension
-XSL extension
-dompdf Library

PHPWord is installed through Composer. You just need to add a dependency on PHPWord to your package.

{
"require" : {
"phpoffice / phpword" : "v0.14. *​​"
}
}

Execute composer install after adding

In addition, you can install it from the command line, such as
composer require phpoffice/phpword

Sample code

        //Set default style
        $phpWord->setDefaultFontName('Imitation song');//Typeface
        $phpWord->setDefaultFontSize(16);//Font size

        //Add page
        $section = $phpWord->createSection();

        //Add catalog
        $styleTOC  = ['tabLeader' => \PhpOffice\PhpWord\Style\TOC::TABLEADER_DOT];
        $styleFont = ['spaceAfter' => 60, 'name' => 'Tahoma', 'size' => 12];
        $section->addTOC($styleFont, $styleTOC);

        //Default style
        $section->addText('Hello PHP!');
        $section->addTextBreak();//Line break

        //Specified style
        $section->addText(
            'Hello world!',
            [
                'name' => 'Song style',
                'size' => 16,
                'bold' => true,
            ]
        );
        $section->addTextBreak(5);//Multiple line breaks

        //custom style
        $myStyle = 'myStyle';
        $phpWord->addFontStyle(
            $myStyle,
            [
                'name' => 'Verdana',
                'size' => 12,
                'color' => '1BFF32',
                'bold' => true,
                'spaceAfter' => 20,
            ]
        );
        $section->addText('Hello laravel!', $myStyle);
        $section->addText('Hello Vue.js!', $myStyle);
        $section->addPageBreak();//Page break

        //Add text resource
        $textrun = $section->createTextRun();
        $textrun->addText('Thickening', ['bold' => true]);
        $section->addTextBreak();//Line break
        $textrun->addText('tilt', ['italic' => true]);
        $section->addTextBreak();//Line break
        $textrun->addText('Font color', ['color' => 'AACC00']);

        //list
        $listStyle = ['listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER];
        $section->addListItem('List Item I', 0, null, 'listType');
        $section->addListItem('List Item I.a', 1, null, 'listType');
        $section->addListItem('List Item I.b', 1, null, 'listType');
        $section->addListItem('List Item I.c', 2, null, 'listType');
        $section->addListItem('List Item II', 0, null, 'listType');
        $section->addListItem('List Item II.a', 1, null, 'listType');
        $section->addListItem('List Item II.b', 1, null, 'listType');

        //Hyperlinks
        $linkStyle = ['color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE];
        $phpWord->addLinkStyle('myLinkStyle', $linkStyle);
        $section->addLink('http://www.baidu.com', 'use Baidu Search', 'myLinkStyle');
        $section->addLink('http://www.baidu.com', null, 'myLinkStyle');

        //Add picture
        $imageStyle = ['width' => 480, 'height' => 640, 'align' => 'center'];
        $section->addImage('./img/t1.jpg', $imageStyle);
        $section->addImage('./img/t2.jpg',$imageStyle);

        //Add title
        $phpWord->addTitleStyle(1, ['bold' => true, 'color' => '1BFF32', 'size' => 38, 'name' => 'Verdana']);
        $section->addTitle('Title 1', 1);
        $section->addTitle('Title 2', 1);
        $section->addTitle('Title 3', 1);

        //Add table
        $styleTable = [
            'borderColor' => '006699',
            'borderSize' => 6,
            'cellMargin' => 50,
        ];
        $styleFirstRow = ['bgColor' => '66BBFF'];//First line style
        $phpWord->addTableStyle('myTable', $styleTable, $styleFirstRow);

        $table = $section->addTable('myTable');
        $table->addRow(400);//Row height 400
        $table->addCell(2000)->addText('Student ID');
        $table->addCell(2000)->addText('Full name');
        $table->addCell(2000)->addText('major');
        $table->addRow(400);//Row height 400
        $table->addCell(2000)->addText('2015123');
        $table->addCell(2000)->addText('Xiao Ming');
        $table->addCell(2000)->addText('Computer science and technology');
        $table->addRow(400);//Row height 400
        $table->addCell(2000)->addText('2016789');
        $table->addCell(2000)->addText('Silly');
        $table->addCell(2000)->addText('Pedagogical Technology');

        //Header and footer
        $header = $section->createHeader();
        $footer = $section->createFooter();
        $header->addPreserveText('header');
        $footer->addPreserveText('footer - The number of pages {PAGE} - {NUMPAGES}.');

        //The generated document is Word2007
        $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $writer->save('./word/hello.docx');

        //Word to HTML
        // $phpWord = \PhpOffice\PhpWord\IOFactory::load('./word/hello.docx');
        // $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
        // $xmlWriter->save('./html/hello.html'); 

Note: when Word transfers HTML, the directory loaded by Word document writes relative path and absolute path
I have not solved this problem. Please leave a message if there is any way

Reference document address: http://phpword.readthedocs.io/en/latest/intro.html

The renderings are as follows:

Tags: Laravel PHP xml Vue

Posted on Sun, 03 May 2020 15:06:33 -0700 by Ibnatu