phpword将生成的doc发送到前端

时间:2016-11-25 11:04:48

标签: javascript php phpword

我正在尝试在我的webapp上生成docFiles。首先我想在前端使用一些工具,比如Markswindolls' jquery.wordexport.js'工具。但由于没有太多功能,如设置页眉或页脚或对齐,我开始使用' 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');


?>

1 个答案:

答案 0 :(得分:0)

这是我用于PHP Excel的内容,对于碎片感到抱歉,但这是来自大型服务文件:

$this->generateXls();
$this->writeToVariable();
if ($response instanceof Response) {
    $this->setHeaders($response->getHeaders());
}
$response->setContent($this->getXlsContent());

这是将文件内容存储到变量而不是文件的位置:

protected function writeToVariable()
{
    ob_start();
    $this->getWriter()->save('php://output');
    $xlsContent = ob_get_clean();
    $this->setXlsContent($xlsContent);
}

只需在返回回复之前设置标题

protected function setHeaders(Headers $headers)
{
    $headers->addHeaderLine('Content-Type', $this->getFileMimeType());
    $headers->addHeaderLine('Content-Disposition', "attachment; filename=\"{$this->getFullFileName()}\"");
    $headers->addHeaderLine('Accept-Ranges', 'bytes');
    $headers->addHeaderLine('Content-Length', strlen($this->getXlsContent()));
}

这是在zf2中完成的,因此标题会被注入到响应对象中,但在您的情况下,只需将它们添加到生成输出的任何内容中。

哦,顺便说一下,excel的mime类型看起来像这样:

$this->setFileMimeType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

因此,尝试找到要输出的ms字格式的格式,即格式。

对于简化的非OO approch:

$writer = \PHPExcel_IOFactory::createWriter($this->getWorkbook(), $type);
//<---GENERATE YOUR DOCUMENT HERE
ob_start();
$writer->save('php://output');
$document = ob_get_clean();
//<---SET OUTPUT HEADERS LIKE FOR ANY OTHER FILE TYPE HERE
echo $document;
die;
相关问题