【问题标题】:PHPWord and Codeigniter cant locate output filePHPWord 和 Codeigniter 找不到输出文件
【发布时间】:2013-11-14 16:07:12
【问题描述】:

我已按照 Codeigniter here 中有关安装 PHPWord 的所有说明进行操作

我只是不知道在哪里可以找到我的输出文件 (filename.docx) 以及如何知道它是否正常工作。没有错误信息。

这是我的代码:

  $PHPWord = $this->word;
  $section = $PHPWord->createSection(array('orientation'=>'landscape'));
  $section->addText('Hello World!');
  $section->addTextBreak(2);

  $section->addText('I am inline styled.', array('name'=>'Verdana', >'color'=>'006699'));
  $section->addTextBreak(2);

  $PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, >'size'=>16));
  $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', >'spaceAfter'=>100));
  $section->addText('I am styled by two style definitions.', 'rStyle', >'pStyle');
  $section->addText('I have only a paragraph style definition.', null, >'pStyle');

  $filename='kem.docx'; //save our document as this file name
  header('Content-Type: application/vnd.openxmlformats->officedocument.wordprocessingml.document'); //mime type
  header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell >browser what's the file name
  header('Cache-Control: max-age=0'); //no cache

  $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
  $objWriter->save('php://output');

顺便说一句,我使用的是 ubuntu 12.04 LTS

【问题讨论】:

  • 错误是什么?我认为它位于您的控制器目录中。
  • 没有错误..我找不到任何错误..我只是找不到我的文件..我检查了控制器文件夹,仍然没有 .docx 文件
  • 那么你的脚本没有生成任何东西。当你转储 $objWriter 时它会说什么

标签: codeigniter phpword


【解决方案1】:

代码中有一些错误。在所有样式数组中都有额外的 >s。也许只是复制和粘贴错误...

因为你输出到 php://output 没有文件将被写入它只会被直接发送到浏览器。而是使用类似 $objWriter->save('d:\www\' . $filename);创建输出文件。

使用以下代码一切正常。它会输出到文件并发送到浏览器。

<?php
require_once 'PHPWord.php';

$PHPWord = new PHPWord();
$section = $PHPWord->createSection(array('orientation'=>'landscape'));
$section->addText('Hello World!');
$section->addTextBreak(2);


$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);

$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');

$filename='kem.docx'; //save our document as this file name
header('Content-Type: application/vnd.ms-word'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell >browser what's the file name
header('Cache-Control: max-age=0'); //no cache

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
$objWriter->save('d:\www\\php\\genDoc\\' . $filename);

/* For Unix / Linux

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
$objWriter->save('/home/ubuntu/workspace/uploads/'. $filename);

*/
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 2023-02-03
    • 2019-08-30
    • 2020-01-31
    • 1970-01-01
    相关资源
    最近更新 更多