0
我想在我的webapp上生成docFiles。首先,我想在前端使用一些工具,如Markswindolls的jquery.wordexport.js工具。但由于没有太多的功能,如设置页眉或页脚或对齐,我开始使用'phpword'。phpword发送生成的文档到前端
我现在的问题是,docFile保存在服务器上。是否有可能通过ajax将文件发送到前端,以便用户在推送“下载为.doc”按钮后可以获取文件?
任何其他建议也欢迎。
的jQuery:
$('#word-button').on('click', function() {
$.ajax({
type: "POST",
url: "phpWORD/gendocx.php",
success: function (msg, string, jpXHR) {
console.log('AJAX SUCCESS');
},
complete : function(data, textStatus, jqXHR){
console.log('AJAX COMPLETE');
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
})
gendocx.php:
<?php
require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
// Create a new PHPWord Object
$PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();
// 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 putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello me!');
// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');
?>
感谢您的快速响应。我对此很陌生,所以我不知道从哪里开始或像你一样解决问题。 – moody
嗨,你的目标是将文档保存到php输出流而不是文件,然后将该流捕获到变量中。我用简化版本更新了答案 – Auris