douwen8118 2016-11-25 11:04
浏览 75

phpword将生成的doc发送到前端

I am trying to generate docFiles on my webapp. First i was thinking of using some tool in the Frontend like Markswindolls 'jquery.wordexport.js' tool. But since there are not much functions like setting Header or Footer or aligning, I started to work with 'phpword'.

My Problem now is, that the docFile is saved on the server. Is there any possibility of sending the file via ajax to the Frontend so the User can get the File after pushing on my 'download as .doc' button?

Any other recommendations are welcome too.

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 + "
Error:" + 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条回答 默认 最新

  • doutongxuan1614 2016-11-25 11:11
    关注

    This is what I use for PHP Excel, sorry about fragmentation, but this is from a large service file:

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

    This is where the file content is stored to a variable instead of a file:

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

    And just set the headers before returning your response

    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()));
    }
    

    This is done in zf2 so headers are injected in to response object but in your case just add them to whatever generates your output.

    Oh, btw, mime type for excel looks like this:

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

    So try to find the one for the ms word format you want to output, that is in this format.

    For a simplified non-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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测