欢迎各位兄弟 发布技术文章
这里的技术是共享的
如果你需要基本的东西,你可以这样做: //下面这个生成 doc 对 html 里的图像不起作用
$headers = array(
"Content-type"=>"text/html",
"Content-Disposition"=>"attachment;Filename=myfile.doc"
);
$content = '<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>
My Content
</p>
</body>
</html>';
return Response::make($content,200, $headers);
如果你需要更复杂的东西,我建议PHPOffice / PHPWord
并可以给我一个基本的例子,使用PHPOffice / PHPWord
首先,通过作曲家安装PHPWord:
{
"require": {
"laravel/framework": "4.1.*",
"phpoffice/phpword": "dev-master"
}
}
您可以使用简单路由快速测试基本用法示例:
Route::get('test', function() {
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Every element you want to append to the word document is placed in a section.
// To create a basic section:
$section = $phpWord->addSection();
// After creating a section, you can append elements:
$section->addText('Hello world!');
// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.',
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
// If you often need the same style again you can create a user defined style
// to the word document and give the addText function the name of the style:
$phpWord->addFontStyle('myOwnStyle',
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style',
'myOwnStyle');
// You can also put the appended element to local object like this:
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Verdana');
$fontStyle->setSize(22);
$myTextElement = $section->addText('Hello World!');
$myTextElement->setFontStyle($fontStyle);
// Finally, write the document:
// The files will be in your public folder
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'RTF');
$objWriter->save('helloWorld.rtf');
});
有太多的信息,你必须看看。请看看文档
我做了你提供的不行,
使用包信息加载作曲家资料库更新依赖关系(包括require-dev)
phpoffice / phpword建议安装ext-gd2(用于添加图像)phpoffice / phpword建议安装dompdf / dompdf(用于写PDF)写入锁定文件生成自动加载文件生成优化的类加载器编译公共类
我收到以下错误:
哎呀,看起来像出事了。
这是错误:
PhpOffice \ PhpWord \ Exception \ Exception无法关闭zip文件helloWorld.docx。
打开:/var/www/html/word/vendor/phpoffice/phpword/src/PhpWord/Writer/Word2007.php
//关闭文件if($ objZip-> close()=== false){throw new Exception(“无法关闭zip文件$ filename”); }