jjzjj

php - XML 解析错误; PHPWord

coder 2024-04-24 原文

我在我的 Laravel 应用程序中使用 PHPOffice/PHPWord。它用于生成包含表中结果的 .docx 文档。这对于包含 6 行的 3 个表的文档非常有用,但是当生成更多行时,文档会生成,但在打开它时会出现以下错误:

We're sorry, We can't open (documentname) because we found a problem with its contents.

Details: XML parsing error Location: Part:/word/document.xml, Line: 2, Column 14349. 

现在,我已经开始处理另一个结果页面,我还想在其中生成一个 .docx 文档。这将包含 5 个表,但有 3 行我得到相同的 XML 解析错误但在不同的位置(位置:部分:/word/document.xml,行:4,列:2888)。有人可以向我解释这是我的代码错误还是 phpword/words 错误?

我通过删除所有内容并慢慢添加新行来完成一些故障排除。我发现了错误,但我该如何解决。前两张表都生成好了..

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

    $section = $phpWord->addSection();
    $section->addImage('../public/img/2.jpg', array('width' => 230, 'height' => 65, 'alignment' => 'left'));
    $section->addText('Project IDs:' . $parameter);
    $header =$section->addHeader();
    $header->addText('Results Summary');

    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $tableName = 'rStyle';
    $phpWord->addFontStyle($tableName, array('italic' => true, 'size' => 12));
    $thName = 'tStyle';
    $phpWord->addFontStyle($thName, array('bold' => true, 'size' => 9));

    $section->addText('General Information Table', $tableName);
    $fancyTableStyle = array('borderSize' => 6, 'borderColor' => '999999');
    $spanTableStyleName = 'Overview tables';
    $phpWord->addTableStyle($spanTableStyleName, $fancyTableStyle);
    $table = $section->addTable($spanTableStyleName);
    $table->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
    $table->addCell(1750)->addText('Project ID',$thName);
    $table->addCell(1750)->addText('Description',$thName);
    $table->addCell(1750)->addText('Notes',$thName);
    foreach ($id_array_explode as $char) {
        $table->addRow();
        $singlenumber = (int)$char;
        $cursor = $collection->find(array("id" => $singlenumber));
        foreach ($cursor as $document) {
                    $table->addCell(1750)->addText($document["project_id"]);
                    $table->addCell(1750)->addText($document["description"]);
                    $table->addCell(1750)->addText($document["notes"]);
        }
    }
    $section->addText('        
');
    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $section->addText('Input Table', $tableName);
    $table1 = $section->addTable($spanTableStyleName);
    $table1->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
    $table1->addCell(1750)->addText('Project ID',$thName);
    $table1->addCell(1750)->addText('#',$thName);
    foreach ($id_array_explode as $char) {
        $table1->addRow();
        $singlenumber = (int)$char;
        $cursor = $collection->find(array("id" => $singlenumber));
        foreach ($cursor as $document) {
            if (is_array($document['input'])) {
                foreach ($document['input'] as $samples) {
                    $table1->addCell(1750)->addText($document["project_id"]);
                    $table1->addCell(1750)->addText($samples['nr']);
                }
            }
        }
    }
    $section->addText('        
');
    $section->addLine(
        array(
            'width'       => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(16),
            'height'      => \PhpOffice\PhpWord\Shared\Converter::cmToPixel(0),
            'positioning' => 'absolute',
        )
    );
    $section->addText('Output Table', $tableName);
    $table2 = $section->addTable($spanTableStyleName);
//// THIS IS WHERE THE ERROR OCCURS!! 
        $table2->addRow(null, array('tblHeader' => true, 'cantSplit' => true));
        $table2->addCell(1750)->addText('ID',$thName);

谢谢!

解决方案 好的,所以我删除了整个文档并分别添加了每个句子以查看错误发生的位置。这导致看到错误来 self 得到的数据。它无法处理“>”和“&”符号!

因此,如果您遇到此错误,请检查您正在打印的数据!

最佳答案

更好的解决方案是在对 word 文档执行任何操作之前添加以下代码行:

PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);

这将自动转义任何有问题的字符。

关于php - XML 解析错误; PHPWord,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40301312/

有关php - XML 解析错误; PHPWord的更多相关文章

  1. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  5. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  10. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

随机推荐