【问题标题】:Yii2 PHPWord addListItem not working. What am I doing wrong?Yii2 PHPWord addListItem 不工作。我究竟做错了什么?
【发布时间】:2021-07-17 04:59:50
【问题描述】:

我在 Yii2 中使用 PhpWord 扩展。 使用 addListItem 时出现问题。是不是不行。打印清空此块。其他印刷不错。

我的代码:

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

// Define styles
    $fontStyleName = 'myOwnStyle';
    $phpWord->addFontStyle($fontStyleName, array('color' => 'FF0000'));

    $paragraphStyleName = 'P-Style';
    $phpWord->addParagraphStyle($paragraphStyleName, array('spaceAfter' => 95));

    $multilevelNumberingStyleName = 'multilevel';
    $phpWord->addNumberingStyle(
        $multilevelNumberingStyleName,
        array(
            'type'   => 'multilevel',
            'levels' => array(
                array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360),
                array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720),
            ),
        )
    );

    $predefinedMultilevelStyle = array('listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER_NESTED);

// New section
        $section = $phpWord->addSection();

// Lists
        $section->addText('Multilevel list.');
        $section->addListItem('List Item I', 0, null, $multilevelNumberingStyleName);
        $section->addListItem('List Item I.a', 1, null, $multilevelNumberingStyleName);
        $section->addListItem('List Item I.b', 1, null, $multilevelNumberingStyleName);
        $section->addListItem('List Item II', 0, null, $multilevelNumberingStyleName);
        $section->addListItem('List Item II.a', 1, null, $multilevelNumberingStyleName);
        $section->addListItem('List Item III', 0, null, $multilevelNumberingStyleName);
        $section->addTextBreak(2);

// saving
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="first.docx"');
        header('Content-Type: application/msword');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
        $objWriter->save("php://output");

请帮忙。我将非常感谢您的帮助。我做错了什么?

【问题讨论】:

    标签: php yii2 phpword


    【解决方案1】:

    一切都变得非常简单,更加美丽。 输出需要使用标准函数 Yii,并且在下载保存文件到系统之前。

    $objWriter = IOFactory::createWriter($phpWord);
    $objWriter->save('uploads/doc.docx');
    
    Yii::$app->response->sendFile('uploads/doc.docx');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 2019-12-23
      • 2016-05-27
      相关资源
      最近更新 更多