【问题标题】:PHPWord_Template save to clientPHPWord_Template 保存到客户端
【发布时间】:2013-01-11 10:03:10
【问题描述】:

我已经从this site 下载了 PHPWord_0.6.2_Beta.zip 包

有 2 个目录:Examples 和 PHPWord,以及 1 个文件:PHPWord.php
我把 PHPWord 目录和 PHPWord。如this article中所述,在应用程序/第三方中创建名为“Word.php”的新库

这是新库代码 Word.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH."/third_party/PHPWord.php"; 

class Word extends PHPWord { 
    public function __construct() { 
        parent::__construct(); 
    } 
}
?>


现在我们可以轻松地使用 PHPWord 作为 CI 库

我已经尝试了下载包中目录示例中的文本示例,这里是原始代码

<?php
require_once '../PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Add text elements
$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');



// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
?>


我尝试在我的 CI 控制器中实现它,这里是代码 PHPWord_Text.php

<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class PHPWord_Text extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('word');
    }
    function index() {
        $PHPWord = $this->word; // New Word Document
        $section = $PHPWord->createSection(); // New portrait section
        // Add text elements
        $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');
        // Save File / Download (Download dialog, prompt user to save or simply open it)
        $filename='just_some_random_name.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');
    }
}
?>


在

中访问它
http://localhost/codeigniter/index.php/PHPWord_Text


Foilaa!!!我的代码有效!但是...当我尝试将下载包中的目录示例中的模板示例转换为新的 CI 控制器时,我有点困惑,这里是 Template.php 的原始代码

<?php
require_once '../PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Template.docx');

$document->setValue('Value1', 'Sun');
$document->setValue('Value2', 'Mercury');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');

$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));

$document->save('Solarsystem.docx');
?>


谁能帮我解决这个问题?请.. T_T
注意:我不希望它保存在服务器中,我希望它显示下载对话框提示用户是否保存或打开它,就像下面的一些代码一样工作

// Save File / Download (Download dialog, prompt user to save or simply open it)
$filename='just_some_random_name.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');

【问题讨论】:

  • 到底是什么问题?我看到您从 github 上给出的示例中复制并粘贴了代码,您没有解释问题所在,也没有提供您将用来检查它的模板。
  • 请重新检查我的问题,我已经修改了它,希望你现在明白我的问题^_^
  • 所以问题是“您不希望它保存在服务器上”或其他什么?请提供您在此示例中使用的模板文件(上传到某处)。

标签: codeigniter phpword


【解决方案1】:

我认为您可能遇到不止一个 Template.php 问题。

首先,您可能想弄清楚它在做什么。

使用其他示例 x.php 生成 x.docx。对于 Template.php,它使用 Template.docx 来生成 Solarsystem.docx。

变量Value1、Value2、Value3等都在Template.docx中定义为${Value1}、${Value2}、${Value3}等。如果在Template.php中更改右侧的值,它会改变Solarsystem.docx中的对应值。

第二,你似乎关心输出。

对我有用的是:

$filename = 'nameOfFile.docx';
$document->save($filename);
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);

这种方式仍然将其保存在服务器上,但也会下载它。如果我同时使用$document-&gt;save(); 和$objWriter-&gt;save();,当我尝试在objWriter 保存的文件上使用readfile(); 时,我会得到一个空白文档。

【讨论】:

    【解决方案2】:

    我认为这是正确的:

     $filename = 'nameOfFile.docx';
        $document->save($filename);
        header('Content-Description: File Transfer');
        header('Content-type: application/force-download');
        header('Content-Disposition: attachment; filename='.basename($filename));
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($filename));
        readfile($filename);
        unlink($filename);
    

    【讨论】:

      【解决方案3】:

      Template 类的方法save() 返回临时文件文档的名称,所以你只需将它提供给readfile($tempName) 的用户;

      【讨论】:

        【解决方案4】:

        我不确定我是否正确地回答了你的问题,如果我错了,请原谅。

        在您的第 4 个代码块中,您有一行

        $document->save('Solarsystem.docx');
        

        如果您不想将文件保存到本地磁盘,而是将其发送到客户端,则只需删除该行并添加第 5 个代码块中的代码:

        // Save File / Download (Download dialog, prompt user to save or simply open it)
        $filename='just_some_random_name.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');
        

        【讨论】:

          【解决方案5】:
          <?php
          
          if (!defined('BASEPATH'))
              exit('No direct script access allowed');
          
          require_once APPPATH . '/src/PhpWord/Autoloader.php';
          
          use PhpOffice\PhpWord\Autoloader as Autoloader;
          Autoloader::register();
          

          【讨论】:

          • 请详细说明这是如何解决问题的
          猜你喜欢
          • 2016-01-01
          • 2013-07-15
          • 1970-01-01
          • 1970-01-01
          • 2017-08-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-29
          相关资源
          最近更新 更多