简体   繁体   中英

How to change paper size in PHPWord

How to Change Paper Size in phpword?

I Want to Change the paper size to Legal paper (8.5 in. by 14 in). I cannot find the option in the documentation. I am not sure on what to apply the rule, and no ''paper'' or ''size'' seems related to this in the documentation.

( https://phpword.readthedocs.io/en/latest/ )

Does anybody knows if it is possible to set the desired paper size on a document?

You can set the paper style size property. Please refer to the documentation .

By default, section element has the orientation property in the style array. It is set to landscape or portrait, and these are default values for the page width and height. However, you can set them how you want.

orientation: Page orientation (portrait, which is default, or landscape).

pageSizeH. Page height in twip. Implicitly defined by orientation option. Any changes are discouraged.

pageSizeW. Page width in twip. Implicitly defined by orientation option. Any changes are discouraged.

Here is a link to it:

https://phpword.readthedocs.io/en/latest/styles.html

Now, here is an example of how I used it in my code in order to make it work:

<?php
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection([
    'pageSizeH' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(11),
    'pageSizeW' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(8.5)
]);

You have the static method in the converter class to calculate the ''Twip'' unit, which is helpful.

Here's how: (EDIT, added namespace for class)

use PhpOffice\PhpWord\Style\Paper;

$paper = new Paper();
$paper->setSize('Letter');  // or 'Legal', 'A4' ...

$section = $doc->addSection([
    'pageSizeW' => $paper->getWidth(),
    'pageSizeH' => $paper->getHeight(),
]);

I agree it seems to be missing from the docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM