【问题标题】:How to set correct indent phpword如何设置正确的缩进 phpword
【发布时间】:2018-05-26 18:28:39
【问题描述】:

我正在使用 codeigniter 和 phpword 我试图获得如下图所示的间距。

当我导出文档时,我的感觉如何

问题:在 PHPWord 中如何设置正确的间距,我想让我的输出与第一张图像相同。

public function export() {
    $phpWord = new \PhpOffice\PhpWord\PhpWord();

    $section = $phpWord->addSection();


    $results = $this->get_events_for_export();

    foreach ($results as $result) {

        $date = strtotime($result['event_date']);

        $line = date('M', $date) .'  '. date('d', $date) .'  '. date('D', $date) .'  '. htmlentities($result['event_title']);

        $section->addText($line . "\n");
    }

    // Saving the document as OOXML file...
    $filename = 'date1.docx';

    header("Content-Description: File Transfer");
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');

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

    exit();
}

【问题讨论】:

标签: php codeigniter phpword


【解决方案1】:

在阅读了几个小时后,现在使用 addParagraphStyle() 解决了一些问题

public function export() {
    $phpWord = new \PhpOffice\PhpWord\PhpWord();

    $leftTabStyleName = 'centerTab';
    $phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));

    $section->addTextBreak();

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

    // Add listitem elements
    $fontStyle = new \PhpOffice\PhpWord\Style\Font();
    $fontStyle->setBold(false);
    $fontStyle->setName('Tahoma');
    $fontStyle->setSize(16);
    $section->addText("\tClub Program " . date('Y') .' / ' . date('Y', strtotime('+1 year')), $fontStyle, $leftTabStyleName);

    $multipleTabsStyleName = 'multipleTab';

    $phpWord->addParagraphStyle(
        $multipleTabsStyleName,
        array(
            'tabs' => array(
                new \PhpOffice\PhpWord\Style\Tab('left', 1000),
                new \PhpOffice\PhpWord\Style\Tab('center', 1000),
                new \PhpOffice\PhpWord\Style\Tab('right', 1000),
            )
        )
    );

    $results = $this->get_events_for_export();

    foreach ($results as $result) {

        $date = strtotime($result['event_date']);

        $section->addText(date('M', $date) ."\t". date('d', $date) ."\t". date('D', $date) ."\t". htmlentities($result['event_title']), null, $multipleTabsStyleName);

    }

    $filename = 'club_program-' . time() . '.docx';

    header("Content-Description: File Transfer");
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');

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

    exit();
}

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2019-04-26
    • 2014-08-18
    • 1970-01-01
    • 2016-11-30
    • 2021-06-25
    • 1970-01-01
    • 2010-09-09
    • 2011-09-14
    相关资源
    最近更新 更多