Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

align text right then lost white spaces at the end #1686

Closed
netmou opened this issue Aug 7, 2019 · 3 comments
Closed

align text right then lost white spaces at the end #1686

netmou opened this issue Aug 7, 2019 · 3 comments

Comments

@netmou
Copy link

netmou commented Aug 7, 2019

Describe the Bug

In China, official documents and letters are usually signed at the bottom right of the text, including the unit and time.

Here's how I did it: first I aligned the sign-off to the right, and then I added a few Spaces on the right side of the sign-off to give it more space from the right edge, but I found that the space on the right side of the sign-off didn't show up in the file generated by PHPWord

在中国,公文、书信等一般在正文的右下方都有落款,落款包含单位和时间。
这里我的实现方式是:首先落款整体右对齐,然后在落款的右侧加几个空格,使其离右边的边界多留出一些空白,但是我发现PHPWord生成的文件中落款右侧的空格没有显示出来

Steps to Reproduce

<?php 
define('PATH',str_replace("\\","/",rtrim(dirname(__FILE__),'/').'/'));
include PATH.'../../../config.inc.php';
include BASE . 'include/mysql.inc.php';
include BASE . 'include/Utility.class.php';
include BASE . 'include/consts.inc.php';
include BASE . 'vendor/autoload.php';

// 将英文数字及特殊字符的样式特殊处理
function addSmartText($container, $text, $fStyle=array(), $pStyle=array()){
  $len = mb_strlen($text);
  $lastType = 0; // 1汉字,2数字
  $writeBuff = '';
  $textrun = $container->addTextRun($pStyle);
  $en_font = $fStyle;
  $en_font['name']='Times New Roman';
  $fs_font = $fStyle;
  $fs_font['name']='仿宋_GB2312';
  for($i=0;$i<$len;$i++){
    $char = mb_substr($text,$i,1);
  //echo $char;
    if($char == '〔' || $char == '〕'){
      if($writeBuff != '' && $lastType == 1){
        $textrun->addText($writeBuff, $fStyle);
        // echo $writeBuff.'1<br>';
        $writeBuff = '';
      }
      if($writeBuff != '' && $lastType == 2){
        $textrun->addText($writeBuff, $en_font);
         // echo $writeBuff.'2<br>';
        $writeBuff = '';
      }
      $textrun->addText($char, $fs_font);
      $lastType = 0;
    }elseif(ord($char) < 128){
      if($lastType != 2 && $writeBuff != ''){
        $textrun->addText($writeBuff, $fStyle);
         // echo $writeBuff.'3<br>';
        $writeBuff = '';
      }
      $writeBuff .= $char;
      $lastType = 2;

    }else{
      if($lastType != 1 && $writeBuff != ''){
        $textrun->addText( $writeBuff, $en_font);
        // echo $writeBuff.'4<br>';
        $writeBuff = '';
      }
      $writeBuff .= $char;
      $lastType = 1;
    }
  }
  if($writeBuff!='' && $lastType == 1){
    $textrun->addText($writeBuff, $fStyle);
     // echo $writeBuff.'5<br>';
  }elseif($writeBuff!='' && $lastType == 2){
    $textrun->addText($writeBuff, $en_font);
    // echo $writeBuff.'6<br>';
  }

}


$id = intval($_GET['id']);
$lwInfo = $mysql->findOneBySql("select * from rzpt_laiwen_content where id='{$id}'");
if(!file_exists('tmp/')){
  mkdir('tmp', 0640);
}

// 去掉固定部分,剩余的文件标题占15%最低且不小于3cm,
// 领导批示和拟办意见相同,按比例分配
$h0 = 16;
$h1 = max($h0*0.15, 3);
$h2 = ($h0 -$h1)/2; //2 times

$fileName = 'nbd-'.$id.'.docx';
$tempFile = tempnam('./tmp',"nbd");

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(array(
  'marginLeft'=>\PhpOffice\PhpWord\Shared\Converter::cmToTwip(3.3),
  'marginRight'=>\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2.3),
  'marginTop'=>\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2.58),
  'marginBottom'=>\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2.5),
));

$pstyle = array(
  'indentation' => array('left' => 240, 'right' => 240),
  'space' => array('before' => 120, 'after' => 120)
  
);


function cm($cm){
  return \PhpOffice\PhpWord\Shared\Converter::cmToTwip($cm);
}


$font= array('name' => '宋体', 'size' => 14);
$font_s= array('name' => '宋体', 'size' => 13);
$font_en= array('name' => 'Times New Roman', 'size' => 14);
$font_en_s= array('name' => 'Times New Roman', 'size' => 13);
$font_fs= array('name' => '仿宋', 'size' => 14);
$vcent = array('valign' => 'center');
$cent = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER);
$right = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::RIGHT);

$section->addText(
  '中共日照市委办公室来文阅办单',
  array('name' => '方正小标宋简体', 'size' => 22),
  array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100)
);
$section->addText('');

$table = $section->addTable(array('borderSize' =>5, 'cellMargin'=>0, 'layout'=>'fixed'));
$table->addRow(cm(1.49));
$cell = $table->addCell(cm(1.46), $vcent);
$cell->addText("来文", $font, $cent);
$cell->addText("单位", $font, $cent);

$cell = $table->addCell(cm(2.93), $vcent);
$cell->addText(str_replace('中共山东', '', $lwInfo['lwdw']), $font, $cent);

$cell = $table->addCell(cm(0.86), $vcent);
$cell->addText('文', $font, $cent);
$cell->addText('号', $font, $cent);

$cell = $table->addCell(cm(3.62), $vcent);

if(strpos($lwInfo['wh_zi'], '通报') !== false){
  $cell->addText($lwInfo['wh_zi'], $font, $cent);
  addSmartText($cell,'〔'.$lwInfo['wh_nian'].'〕第'.$lwInfo['wh_hao'].'期',$font,$cent);
}else{
  $cell->addText($lwInfo['wh_zi'], $font, $cent);
  addSmartText($cell,'〔'.$lwInfo['wh_nian'].'〕'.$lwInfo['wh_hao'].'号',$font,$cent);
  
}



$cell = $table->addCell(cm(1.36), $vcent);
$cell->addText('阅读', $font, $cent);
$cell->addText('级别', $font, $cent);

$cell = $table->addCell(cm(2.66), $vcent);
$cell->addText($lwInfo['ydjb'], $font, $cent);

$cell = $table->addCell(cm(0.86), $vcent);
$cell->addText('密', $font, $cent);
$cell->addText('级', $font, $cent);

$cell = $table->addCell(cm(1.80), $vcent);
if($lwInfo['secret'] == '公开发布'){
  $cell->addText('公开', $font, $cent);
  $cell->addText('发布', $font, $cent);
}else{
  $cell->addText($lwInfo['secret'], $font, $cent);
}

$table->addRow(cm($h1));
$cell = $table->addCell(null, $vcent);
$cell->addText("文件", $font, $cent);
$cell->addText("标题", $font, $cent);

$cell = $table->addCell(null, array('gridSpan' => '7') + $vcent);
$cell->addText($lwInfo['title'], $font, $cent+$pstyle);

$table->addRow(cm($h2));
$cell = $table->addCell(null, $vcent);
$cell->addText('办', $font, $cent);
$cell->addText('理', $font, $cent);
$cell->addText('意', $font, $cent);
$cell->addText('见', $font, $cent);

$cell = $table->addCell(null, array('gridSpan' => '7') + $vcent);




$nbInfo = explode("\n", trim($lwInfo['nbyj']));
foreach($nbInfo as $nb){
  addSmartText($cell,'  '.$nb, $font, $pstyle);
}
//addSmartText($cell, $lwInfo['nbr'].str_repeat(' ', 5), $font, $pstyle + array('spaceBefore'=>100) + $right);


////////////////////////////////////// problem at here  start////////////////////////////////////////
$txtRun = $cell->addTextRun($pstyle + array('spaceBefore'=>100) + $right);
$txtRun->addText($lwInfo['nbr'].str_repeat(' ', 5), $font);
////////////////////////////////////// problem at here end ////////////////////////////////////////




addSmartText($cell, $lwInfo['nbsj'].str_repeat(' ', 1), $font, $pstyle + $right);




$table->addRow(cm(2));
$cell = $table->addCell(null, $vcent);
$cell->addText("批办", $font, $cent);
$cell->addText("意见", $font, $cent);

$cell = $table->addCell(null, array('gridSpan' => '7') + $vcent);
$txtRun = $cell->addTextRun();
$result = $mysql->execute("select * from rzpt_laiwen_user where wen='{$id}' and user!='{$lwInfo['adduid']}' and is_do=1 and lclx=0 order by step asc, done_time asc");
while($stepInfo= mysql_fetch_assoc($result)){
  if($stepInfo['jieguo']==1){
    $txtRun->addImage('../../resource/signiture/'.$stepInfo['user'].'.png', array(
      'marginTop'          => 10,
      'marginLeft'         => 1,
      'width'              => 80,
    ));
  }
}



$table->addRow(cm($h2));
$cell = $table->addCell(null, $vcent);
$cell->addText('领', $font, $cent);
$cell->addText('导', $font, $cent);
$cell->addText('批', $font, $cent);
$cell->addText('示', $font, $cent);
$table->addCell(null, array('gridSpan' => '7') + $vcent);

$table->addRow(cm(0.95));
$cell = $table->addCell(null, $vcent);
$cell->addText('备注', $font_s, $cent);
$table->addCell(null, array('gridSpan' => '7') + $vcent);

$table->addRow(cm(0.9));
$cell = $table->addCell(null, array('gridSpan' => '8') + $vcent);
addSmartText($cell, '注:1、请不要从夹内取出文件;2、请不要横传;3、阅后请签名。', $font_s, $cent);

addSmartText($section,'市委办公室秘书二科                           联系电话:87871032',$font_s, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceBefore'=>100));


// 添加分页
//$section->addPageBreak();

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

// 下载内容
header('Content-Type: application/octet-stream');
header('Cache-Control: private, no-cache');
header('Content-Disposition: attachment; filename="'.basename($fileName).'"');
header('Content-Transfer-Encoding: binary');
echo file_get_contents($tempFile);
// 删除临时文件
unlink($tempFile);

Expected Behavior

A clear and concise description of what you expected to happen
image

Current Behavior

What is the current behavior?
image

Context

Please fill in your environment information:

  • PHP Version: php5.6
  • PHPWord Version: 0.16
@IceCry
Copy link

IceCry commented Nov 5, 2019

哥们,咋解决的?如何插入连续空格?
Man, how did it work out? How to insert consecutive spaces?

@netmou
Copy link
Author

netmou commented Nov 6, 2019

我提交了一个PR:#1688
你可以合并代码基于0.16,我实现了wordwrap 对应的操作界面:
image

@IceCry
Copy link

IceCry commented Nov 8, 2019

谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants