本文的示例代码参考laravel-phpword
Startup
composer create-project laravel/laravel laravel-phpword --prefer-dist "5.5.*" && cd laravel-phpword
vim routes/web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/word', 'WordController@word');
vim app/Http/Controllers/WordController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
class WordController extends BaseController
{
public function word()
{
return ['code' => 'word'];
}
}
php artisan serve
curl localhost:8000/word
# {"code":"word"}
Word
composer require phpoffice/phpword
vim app/Http/Controllers/WordController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
class WordController extends BaseController
{
public function word()
{
$phpword = new PhpWord();
$filename = 'Bidding Cerificate';
$section = $phpword->addSection();
$header = $section->addHeader();
$header->addWatermark( base_path() . "/public/shuiyin.jpeg", array('marginTop' => 300, 'marginLeft' => 55));
$phpword->addTitleStyle(1, ['bold' => true, 'color' => '000', 'size' => 17, 'name' => '宋体'], ['align' => 'center']);
$section->addTitle($filename);
$section->addText("编号:20200917002 生成日期:2020-09-17");
$styleTable = [
'borderColor' => '000',
'borderSize' => 6,
'cellMargin' => 50,
];
$cellRowSpan = array('vMerge' => 'restart', 'valign' => 'center');
$cellRowContinue = array('vMerge' => 'continue');
$cellColSpan = array('gridSpan' => 3, 'valign' => 'center');
$cellHCentered = array('align' => 'center');
$cellVCentered = array('valign' => 'center');
$styleKey = array('name' => '宋体', 'size' => 14);
$styleValue = array('name' => '宋体', 'size' => 14);
$phpword->addTableStyle('tableStyle', $styleTable);
$table = $section->addTable('tableStyle');
for ($i = 0; $i < 15; $i++) {
$table->addRow(500);
$table->addCell(1200, $cellRowSpan)->addText('信息', $styleKey, $cellHCentered);
$table->addCell(1400, $cellVCentered)->addText("姓名", $styleKey, $cellHCentered);
$table->addCell(2000, $cellVCentered)->addText("小明", $styleValue, $cellHCentered);
$table->addCell(1400, $cellVCentered)->addText("身份证号", $styleKey, $cellHCentered);
$table->addCell(3100, $cellVCentered)->addText("283371201112103748", $styleValue, $cellHCentered);
$table->addRow(500);
$table->addCell(null, $cellRowContinue);
$table->addCell(1400, $cellVCentered)->addText("地址", $styleKey, $cellHCentered);
$table->addCell(7700, $cellColSpan)->addText("北京市", $styleValue, $cellHCentered);
}
$section->addText('注:以上信息正式有效');
$writer = IOFactory::createWriter($phpword, 'Word2007');
$writer->save("$filename.docx");
$files = base_path() . "/public/$filename.docx";
$name = basename($files);
return response()->download($files, $name, $headers = ['Content-Type' => 'application/zip;charset=utf-8']);
}
}
# 放置水印图片"public/shuiyin.jpeg"
php artisan serve
- 浏览器访问http://localhost:8000/word => 下载'Bidding Cerificate.docx'
# 基于Ubuntu Server 1604
sudo apt install -y openjdk-8-jdk libxinerama1 libcairo2 libcups2 libsm6
# 从LibreOffice官网下载
tar xf LibreOffice_6.4.6_Linux_x86-64_deb.tar.gz
sudo dpkg -i LibreOffice_6.4.6.2_Linux_x86-64_deb/DEBS/*.deb
tar xf LibreOffice_6.4.6_Linux_x86-64_deb_langpack_zh-CN.tar.gz
sudo dpkg -i LibreOffice_6.4.6.2_Linux_x86-64_deb_langpack_zh-CN/DEBS/*.deb
sudo apt install -y ttf-mscorefonts-installer
# simsun.ttc是宋体字体
sudo cp simsun.ttc /usr/share/fonts
cd /usr/share/fonts
sudo chmod 644 simsun.ttc
sudo mkfontscale
sudo mkfontdir
sudo fc-cache -fv
libreoffice6.4 --invisible --convert-to pdf ~/Bidding\ Cerificate.docx --outdir ~
# 生成最终的PDF文件
网友评论