Я пытаюсь создать docFiles на моем webapp. Сначала я думал об использовании какого-либо инструмента в Frontend, например, в инструменте jquery.wordexport.js от Markswindolls. Но поскольку функций, таких как настройка заголовка или нижнего колонтитула или выравнивания, не так много, я начал работать с «phpword».phpword send generated doc to frontend
Моя проблема в том, что файл docFile сохраняется на сервере. Есть ли возможность отправить файл через ajax в Frontend, чтобы пользователь мог получить файл после нажатия на кнопку «загрузить как .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