0

我正在尝试使用 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'));
}
4

3 回答 3

2

试试这门课

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

然后在您的控制器中执行以下操作:

return new WordResponse($phpWord, "filename.docx"); 
于 2017-05-15T12:41:13.430 回答
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;
于 2015-05-15T07:38:13.647 回答
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 的更多信息,请参阅文档

2 . 忽略 Symfony 并直接通过 PHPWord 操作提供服务

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

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

于 2015-05-13T23:53:18.763 回答