如何设置正确的缩进phpword

时间:2017-12-12 23:39:23

标签: php codeigniter phpword

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

enter image description here

  

我将文档导出时的情况如何

enter image description here

  

问题:在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();
}

1 个答案:

答案 0 :(得分: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();
}

enter image description here