首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony2 PHPWord响应

Symfony2 PHPWord响应
EN

Stack Overflow用户
提问于 2015-05-13 07:44:45
回答 3查看 2.4K关注 0票数 0

我试图使用Symfony2包在PHPWord上生成一个docx文档。

在我的控制器中,我成功地返回了一个docx文件,但是它是空的,我认为它来自我错误的响应格式。

代码语言:javascript
复制
public function indexAction($id)
{
    $PHPWord = new PHPWord();
    $section = $PHPWord->addSection();

    $section->addText(htmlspecialchars(
    '"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)'
    ));


  // Saving the document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');

return new Response($objWriter->save('helloWorld.docx'), 200, array('Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'));
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-15 07:38:13

非常感谢你的回答。

我使用第二种方法,在我看来这是最好的方法。我只需要返回一个响应,否则文件就生成了,但是被卡在了web目录中。使用此响应,一切都很好,并出现了一个下载提示,并显示了“完整”文件。

这是我的密码:

代码语言:javascript
复制
$PHPWord = new PHPWord();

$section = $PHPWord->addSection();

$section->addText(htmlspecialchars(
            '"Learn from yesterday, live for today, hope for tomorrow. '
                . 'The important thing is not to stop questioning." '
                . '(Albert Einstein)'
        ));

    // Saving the document
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');     
    $filename="MyAwesomeFile.docx";
    $objWriter->save($filename, 'Word2007', true);

    $path = $this->get('kernel')->getRootDir(). "/../web/" . $filename;
    $content = file_get_contents($path);

    $response = new Response();
    $response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    $response->headers->set('Content-Disposition', 'attachment;filename="'.$filename);
    $response->setContent($content);
    return $response;
票数 1
EN

Stack Overflow用户

发布于 2017-05-15 12:41:13

试试这门课

代码语言:javascript
复制
<?php

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use Symfony\Component\HttpFoundation\Response;

class WordResponse extends Response
{
    /**
     * WordResponse constructor.
     * @param string $name The name of the word file
     * @param PhpWord $word
     */
    public function __construct($name, &$word)
    {
        parent::__construct();

        // Set default zip library.
        if( !class_exists('ZipArchive')){
            Settings::setZipClass(Settings::PCLZIP);
        }

        $writer = IOFactory::createWriter($word, 'Word2007');

        //Set headers.
        $this->headers->set("Content-Disposition", 'attachment; filename="' . $name . '"');
        $this->headers->set("Content-Type", 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        $this->headers->set("Content-Transfer-Encoding", 'binary');
        $this->headers->set("Cache-Control", 'must-revalidate, post-check=0, pre-check=0');
        $this->headers->set("Expires", '0');

        $this->sendHeaders();
        $writer->save('php://output');
    }
}

然后在您的控制器中做:

代码语言:javascript
复制
return new WordResponse($phpWord, "filename.docx"); 
票数 2
EN

Stack Overflow用户

发布于 2015-05-13 23:53:18

PHPWord->save()返回一个真值,这就是不下载文件的原因。使用return new Response(),您将响应的内容设置为true ( save调用的结果),这就是响应为空的原因。

您有两个(可能还有更多的)选项来生成和下载这个文件。

1将您的文件保存到临时文件夹和服务器中

代码语言:javascript
复制
$filename = sprintf(
    '%s%sDoc-Storage%s%s.%s',
    sys_get_temp_dir(),
    DIRECTORY_SEPARATOR,
    DIRECTORY_SEPARATOR,
    uniqid(),
    'docx'
);

$objWriter->save($filename);

$response = new BinaryFileResponse($filename);

有关BinaryFileResponse的更多信息,请参见医生们

2。忽略Symfony,直接通过PHPWord action提供服务。

代码语言:javascript
复制
$objWriter->save($filename, 'Word2007', true);
exit();

->save方法提供了在内部下载生成文件的所有操作(请参阅代码),因此您所需要做的就是将格式和第三个参数设置为true,它将为您处理所有的头文件。当然,它不会返回Symfony响应,但在处理该异常之前,您将退出。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30208666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档