Symfony2 PHPWord响应

时间:2015-05-13 07:44:46

标签: symfony phpword

我正在尝试使用PHPWord包在Symfony2上生成docx文档。

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

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

3 个答案:

答案 0 :(得分:1)

非常感谢您的回答。

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

这是我的代码:

$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 :(得分:1)

试试这个课程

a <- c(315, 149, 128, 97, 68, 49, 38, 0)
par(las=1)
par(mgp=c(4.5,1,0))
par(mar=c(5,6,4,3)+0.05)
barplot(a, horiz = TRUE, col="darkolivegreen3", 
main="Average Occupancy",
    ylab = "Hours",
    names.arg = c("0-1h", "1-2h", "2-3h", "3-4h", "4-5h", "5-6h", "6-7h", "7-8h"))
mtext(side=1, text="Minutes", line=2.5)

然后在你的控制器中执行:

<?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');
    }
}

答案 2 :(得分:0)

PHPWord->save()返回一个真值,这就是为什么你的文件没有下载的原因。使用return new Response(),您将回复的内容设置为true(save来电的结果),这就是您的回复为空的原因。

您有2个(可能还有我想不到的)生成和下载此文件的选项..

<强> 1 即可。 将文件保存到临时文件夹和服务器

$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的更多信息,请参阅the docs

<强> 2 即可。 忽略Symfony并直接通过PHPWord操作提供

$objWriter->save($filename, 'Word2007', true);
exit();

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