HansElsen
HansElsen

Reputation: 1753

PHPWord horizontal line (<hr />)

I am currently using PHPWord to generate my documents, but I want to add an horizontal line in the document. Just like an


in HMTL. In word you can do this by typing three underscores en enter, but I want to use it in my generated document.

Does anybody have more information about this feature?

Thank you!

Upvotes: 4

Views: 14652

Answers (5)

Isuru Ranawaka
Isuru Ranawaka

Reputation: 105

You can add lines using the addLine method.

Quoted from : https://phpword.readthedocs.io/en/latest/elements.html?highlight=line#line

$lineStyle = array('weight' => 1, 'width' => 100, 'height' => 0, 'color' => '38c172');
$section->addLine($lineStyle);

Available line style attributes:

  • weight. Line width in twip.
  • color. Defines the color of stroke. hex value without # mark
  • dash. Line types: dash, rounddot, squaredot, dashdot, longdash, longdashdot, longdashdotdot.
  • beginArrow. Start type of arrow: block, open, classic, diamond, oval.
  • endArrow. End type of arrow: block, open, classic, diamond, oval.
  • width. Line-object width in pt.
  • height. Line-object height in pt.
  • flip. Flip the line element: true, false.

Upvotes: 1

Dragon
Dragon

Reputation: 189

I decompressed the '.docx' file, found closest xml tag is 'w:pBdr'.

<w:pBdr><w:bottom w:val="single" w:sz="6" w:space="0" w:color="auto"/></w:pBdr>

So, I insert a horizontal line by ParagraphStyle.

$section->addText('', [], ['borderBottomSize' => 6]);

Upvotes: 8

Ortho
Ortho

Reputation: 919

I think closest to the <hr> is the border attribute on a paragraph:

$phpWord->addParagraphStyle('myBorderStyle', array(
    'borderSize' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(1), 
    'borderColor' => 'FF0000',
    'borderBottomSize' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(4),
    'borderTopColor' => '00FF00'
));

Notable:

  • Sizes are in TWIPS.
  • You can set all directions (top, right, bottom, left) individually.
  • You cannot set the type of border (like dashed).

Upvotes: 1

bjrn
bjrn

Reputation: 126

It's also possible to add a horizontal line to a section, instead of adding a border:

$section->addLine(['weight' => 1, 'width' => 600, 'height' => 0]);

Note that the width is in pixels, which is the main disadvantage of this method. You need to know what the width of your page (minus the margins) is in pixels. If you set it to some large number then the line will just continue through to the right side of your page, ignoring the margin.

Upvotes: 4

Krish R
Krish R

Reputation: 22741

Can you try using to add table and applying border instead of to add <hr> using phpword

     $styleTable = array('borderSize'=>1, 'borderColor'=>'006699');
     $styleFirstRow = array('borderBottomSize'=>1, 'borderBottomColor'=>'0000FF');
     $this->word->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
     // Add table
     $table = $section->addTable('myOwnTableStyle');

Ref: http://www.ahowto.net/php/creating-ms-word-document-using-codeigniter-and-phpword

Upvotes: 0

Related Questions