【问题标题】:Issues Installing PHPword安装 PHPword 的问题
【发布时间】:2016-07-15 19:51:22
【问题描述】:

过去 4 小时我一直在尝试安装 PHPWord,但没有成功。我已经尝试了所有安装方法,包括作曲家以及下载文件夹本身。每当我运行 php 文件时,它总是返回错误“致命错误:第 9 行的 /home/ubuntu/workspace/hello-world.php 中找不到类 'PhpWord'”此外,似乎每当我附加 phpoffice /phpword 在 composer.json 文件中,它一直给我一个安装错误,说没有指定版本。顺便说一句,我在云托管网站 (C9.io) 上运行这些文件。

我已经附上了我的 composer.json 以及 hello_world.php

非常感谢任何帮助。

hello_world.php

<?php
require_once('PHPWord-master/Autoloader.php');
\PhpOffice\PhpWord\Autoloader::register();

//use PhpOffice\PhpWord\PhpWord;
//use PhpOffice\PhpWord\Style\Font;
// Creating the new document...
$phpWord = new PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */


Note: it's possible to customize font style of the Text element you add in     three ways:
- inline;
- using named font style (new font style object will be implicitly created);
- using explicitly created font style object. */
// Adding Text element with font customized inline...
$section->addText(
htmlspecialchars(
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)'
),
array('name' => 'Tahoma', 'size' => 10)
);

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
$fontStyleName,
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
htmlspecialchars(
'"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)'
),
$fontStyleName
);

// Adding Text element with font customized using explicitly created font      style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText(
htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor    Roosevelt)')
);
$myTextElement->setFontStyle($fontStyle);

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
 $objWriter->save('helloWorld.odt');

// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');

/* Note: we skip RTF, because it's not XML-based and requires a different     example. /
/ Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */
?> 

这是 composer.json 需要的部分

"require": 
{
"php": ">=5.3.3",
"ext-xml": "*",
"phpoffice/phpword":"dev-master"
},

【问题讨论】:

    标签: php json


    【解决方案1】:

    看起来它已正确安装,因为您已通过自动加载器语句。

    问题是PhpWord 不是类,它应该是\PhpOffice\PhpWord\PhpWord,因为它是命名空间的。

    所以你可以试试:

    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    

    或将use 语句添加回被注释掉的脚本顶部:

    use PhpOffice\PhpWord\PhpWord;
    

    这将允许您将类称为PhpWord,而不是完全命名空间的版本。

    编辑:

    试试这个:

    从命令行(您可能已经这样做了):

    composer require phpoffice/phpword
    

    创建包含内容的文件 test.php:

    <?php
    
    require_once 'vendor/autoload.php';
    
    $phpword = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpword->addSection();
    
    $section->addText("Hello World!");
    
    $phpword->save('./hello.docx', 'Word2007');
    

    应该可以的。

    【讨论】:

    • 当我尝试了这两种方法时,它返回了相同的错误:致命错误:第 9 行的 /home/ubuntu/workspace/hello-world.php 中找不到 Class 'PhpOffice\PhpWord\PhpWord'
    • 好的,现在它运行了,但是当我运行它时,页面只是空白(但是没有错误)。我看到它正在创建一个新文档,但是如何让它在页面上将数据显示为文档文件?并且每次运行 php 文件时都不让它创建一个新的供应商小节?
    • 而且似乎每当我调用诸如 getDocInfo(); 这样的函数时或 addFontStyle();它给了我错误致命错误:调用非对象上的成员函数______
    • 在非对象上调用成员函数将表明您使用了错误的变量,或者它不再是对象并且是标量(字符串或整数)。至于在页面上显示文档,请尝试header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); $phpword-&gt;save('php://output'); exit;
    • 但是它说 $phpword 是一个非对象,那么如果它说 $phpword 本身是一个非对象,我该怎么做所有其他功能呢?
    猜你喜欢
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2021-05-31
    • 2016-12-13
    • 1970-01-01
    • 2017-03-25
    • 2020-10-12
    • 2022-01-06
    相关资源
    最近更新 更多