企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 一般用法[](https://phpword.readthedocs.io/en/latest/general.html#general-usage "永久链接到这个标题") ## 基本的例子[](https://phpword.readthedocs.io/en/latest/general.html#basic-example "永久链接到这个标题") 以下是PHPWord库的基本示例。[样本文件夹](https://github.com/PHPOffice/PHPWord/tree/master/samples/)中提供了更多示例。 ~~~ <?php require_once 'bootstrap.php'; // 创建一个新文档 $phpWord = new \PhpOffice\PhpWord\PhpWord(); /* Note: any element you append to a document must reside inside of a Section. */ // 添加一个章节至phpword文档 $section = $phpWord->addSection(); // Adding Text element to the Section having font styled by default... $section->addText( '"Learn from yesterday, live for today, hope for tomorrow. ' . 'The important thing is not to stop questioning." ' . '(Albert Einstein)' ); /* * Note: it's possible to customize font style of the Text element you add in three ways: * - inline; * - using named font style (new font style object will be implicitly created); * - using explicitly created font style object. */ // Adding Text element with font customized inline... $section->addText( '"Great achievement is usually born of great sacrifice, ' . 'and is never the result of selfishness." ' . '(Napoleon Hill)', array('name' => 'Tahoma', 'size' => 10) ); // 添加自定义字体样式文本元素 $fontStyleName = 'oneUserDefinedStyle'; $phpWord->addFontStyle( $fontStyleName, array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true) ); $section->addText( '"The greatest accomplishment is not in never falling, ' . 'but in rising again after you fall." ' . '(Vince Lombardi)', $fontStyleName ); // Adding Text element with font customized using explicitly created font style object... $fontStyle = new \PhpOffice\PhpWord\Style\Font(); $fontStyle->setBold(true); $fontStyle->setName('Tahoma'); $fontStyle->setSize(13); $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)'); $myTextElement->setFontStyle($fontStyle); // Saving the document as OOXML file... $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('helloWorld.docx'); // Saving the document as ODF file... $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText'); $objWriter->save('helloWorld.odt'); // Saving the document as HTML file... $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); $objWriter->save('helloWorld.html'); /* Note: we skip RTF, because it's not XML-based and requires a different example. */ /* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */ ~~~ ## PHPWord设置[](https://phpword.readthedocs.io/en/latest/general.html#phpword-settings "永久链接到这个标题") 本`PhpOffice\PhpWord\Settings`类提供了一些选项,这将影响PHPWord的行为。以下是选项。 ### XML Writer兼容性[](https://phpword.readthedocs.io/en/latest/general.html#xml-writer-compatibility "永久链接到这个标题") 此选项设置[XMLWriter :: setIndent](http://www.php.net/manual/en/function.xmlwriter-set-indent.php)和[XMLWriter :: setIndentString](http://www.php.net/manual/en/function.xmlwriter-set-indent-string.php)。此选项的默认值为`true`(兼容),这是[OpenOffice](https://github.com/PHPOffice/PHPWord/issues/103)正确呈现OOXML文档所[必需的](https://github.com/PHPOffice/PHPWord/issues/103)。您可以`false`在开发期间将此选项设置为使得生成的XML文件打开更兼容。 ~~~ \PhpOffice\PhpWord\Settings::setCompatibility(false); ~~~ ### Zip类[](https://phpword.readthedocs.io/en/latest/general.html#zip-class "永久链接到这个标题") 默认情况下,PHPWord使用[Zip扩展](http://php.net/manual/en/book.zip.php)来处理ZIP压缩存档和其中的文件。如果您的服务器上没有安装Zip扩展,则可以使用[PHPWord](http://www.phpconcept.net/pclzip/)中包含的纯PHP库替代PclZip。 ~~~ \PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP); ~~~ ### 输出转义[](https://phpword.readthedocs.io/en/latest/general.html#output-escaping "永久链接到这个标题") 编写某些格式的文档,尤其是基于XML的文档,需要正确的输出转义。如果没有它,当您在其中添加“&”符号,引号和其他字符时,您的文档可能会被破坏。 转义可以通过两种方式执行:软件开发人员在库外部,内置机制在库内部。默认情况下,禁用内置机制以向后兼容v0.13.0之前的版本。要在PHPWord配置文件中打开set`outputEscapingEnabled`选项,`true`或在运行时使用以下指令: ~~~ \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); ~~~ ### 默认字体[](https://phpword.readthedocs.io/en/latest/general.html#default-font "永久链接到这个标题") 默认情况下,每个文本都显示在Arial 10磅中。您可以使用以下两个函数更改默认字体: ~~~ $phpWord->setDefaultFontName('Times New Roman'); $phpWord->setDefaultFontSize(12); ~~~ ## 文件设定[](https://phpword.readthedocs.io/en/latest/general.html#document-settings "永久链接到这个标题") 可以使用设置生成的文档的设置`$phpWord->getSettings()` ### 放大设定[](https://phpword.readthedocs.io/en/latest/general.html#magnification-setting "永久链接到这个标题") 默认缩放值为100%。这可以更改为另一个百分比 ~~~ $phpWord->getSettings()->setZoom(75); ~~~ 或预定义的值`fullPage`,`bestFit`,`textFit` ~~~ $phpWord->getSettings()->setZoom(Zoom::BEST_FIT); ~~~ ### 镜像页边距[](https://phpword.readthedocs.io/en/latest/general.html#mirroring-the-page-margins "永久链接到这个标题") 使用镜像边距为双面文档(如书籍或杂志)设置对开页面。 ~~~ $phpWord->getSettings()->setMirrorMargins(true); ~~~ ### 拼写和语法检查[](https://phpword.readthedocs.io/en/latest/general.html#spelling-and-grammatical-checks "永久链接到这个标题") 默认情况下,只要打开word文档,就会显示拼写和语法错误。对于大文档,这可能会减慢文档的打开速度。您可以隐藏拼写和/或语法错误: ~~~ $phpWord->getSettings()->setHideGrammaticalErrors(true); $phpWord->getSettings()->setHideSpellingErrors(true); ~~~ 您还可以指定拼写和语法检查的状态,标记拼写或语法,因为脏将在打开文档时强制重新检查。 ~~~ $proofState = new ProofState(); $proofState->setGrammar(ProofState::CLEAN); $proofState->setSpelling(ProofState::DIRTY); $phpWord->getSettings()->setProofState(proofState); ~~~ ### 跟踪修订[](https://phpword.readthedocs.io/en/latest/general.html#track-revisions "永久链接到这个标题") 可以使用激活跟踪更改`setTrackRevisions`,您可以进一步指定 * 不使用移动语法,而是移动的项目将被视为在一个地方删除并添加到另一个地方 * 不跟踪格式修订 ~~~ $phpWord->getSettings()->setTrackRevisions(true); $phpWord->getSettings()->setDoNotTrackMoves(true); $phpWord->getSettings()->setDoNotTrackFormatting(true); ~~~ ### 十进制符号[](https://phpword.readthedocs.io/en/latest/general.html#decimal-symbol "永久链接到这个标题") 表示小数的默认符号是`.`英文。在法语中,您可能希望将其更改`,`为例如。 ~~~ $phpWord->getSettings()->setDecimalSymbol(','); ~~~ ### 文件语言[](https://phpword.readthedocs.io/en/latest/general.html#document-language "永久链接到这个标题") 可以使用以下内容更改文档的默认语言。 ~~~ $phpWord->getSettings()->setThemeFontLang(new Language(Language::FR_BE)); ~~~ `Language`有3个参数,一个用于拉丁语言,一个用于东亚语言,一个用于复杂(双向)语言。`PhpOffice\PhpWord\ComplexType\Language`类中提供了几种语言代码,但可以使用任何有效的代码/ ID。 如果您要生成RTF文档,则需要以不同方式设置语言。 ~~~ $lang = new Language(); $lang->setLangId(Language::EN_GB_ID); $phpWord->getSettings()->setThemeFontLang($lang); ~~~ ## 文件信息[](https://phpword.readthedocs.io/en/latest/general.html#document-information "永久链接到这个标题") 您可以设置文档信息,例如标题,创建者和公司名称。使用以下功能: ~~~ $properties = $phpWord->getDocInfo(); $properties->setCreator('My name'); $properties->setCompany('My factory'); $properties->setTitle('My title'); $properties->setDescription('My description'); $properties->setCategory('My category'); $properties->setLastModifiedBy('My name'); $properties->setCreated(mktime(0, 0, 0, 3, 12, 2014)); $properties->setModified(mktime(0, 0, 0, 3, 14, 2014)); $properties->setSubject('My subject'); $properties->setKeywords('my, key, word'); ~~~ ## 测量单位[](https://phpword.readthedocs.io/en/latest/general.html#measurement-units "永久链接到这个标题") Open Office XML中的基本长度单位是twip。缇意味着“英寸点的第二十”,即1缇= 1/1440英寸。 您可以使用PHPWord辅助函数将英寸,厘米或点转换为twip。 ~~~ // Paragraph with 6 points space after $phpWord->addParagraphStyle('My Style', array( 'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6)) ); $section = $phpWord->addSection(); $sectionStyle = $section->getStyle(); // half inch left margin $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); // 2 cm right margin $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); ~~~ ## 文件保护[](https://phpword.readthedocs.io/en/latest/general.html#document-protection "永久链接到这个标题") 文档(或其中的一部分)可以受密码保护。 ~~~ $documentProtection = $phpWord->getSettings()->getDocumentProtection(); $documentProtection->setEditing(DocProtect::READ_ONLY); $documentProtection->setPassword('myPassword'); ~~~ ## 打开时自动重新计算字段[](https://phpword.readthedocs.io/en/latest/general.html#automatically-recalculate-fields-on-open "永久链接到这个标题") 要强制更新文档中存在的字段,请将updateFields设置为true ~~~ $phpWord->getSettings()->setUpdateFields(true); ~~~ ## 连字[](https://phpword.readthedocs.io/en/latest/general.html#hyphenation "永久链接到这个标题") 连字符描述了用连字符打破单词的过程。有几种控制连字符的选项。 ### 自动连字[](https://phpword.readthedocs.io/en/latest/general.html#auto-hyphenation "永久链接到这个标题") 自动连接文本设置`autoHyphenation`为`true`。 ~~~ $phpWord->getSettings()->setAutoHyphenation(true); ~~~ ### 连续连字符限制[](https://phpword.readthedocs.io/en/latest/general.html#consecutive-hyphen-limit "永久链接到这个标题") 可以通过`consecutiveHyphenLimit`选项控制以连字符结尾的连续文本行的最大数量。如果未设置选项或提供的值,则没有限制`0`。 ~~~ $phpWord->getSettings()->setConsecutiveHyphenLimit(2); ~~~ ### 连字区[](https://phpword.readthedocs.io/en/latest/general.html#hyphenation-zone "永久链接到这个标题") 连字符区域(以*twip为单位*)是在应用连字符之前允许的空白量。连字区越小,连字越多。或者换句话说,连字区域越宽,连词越少。 ~~~ $phpWord->getSettings()->setHyphenationZone(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(1)); ~~~ ### 连字帽[](https://phpword.readthedocs.io/en/latest/general.html#hyphenate-caps "永久链接到这个标题") 要控制是否所有大写字母的单词都应使用连字符,请使用doNotHyphenateCaps选项。 ~~~ $phpWord->getSettings()->setDoNotHyphenateCaps(true); ~~~ [下一条【容器官方说明】](https://phpword.readthedocs.io/en/latest/containers.html "容器")[上一条【安装/配置】](https://phpword.readthedocs.io/en/latest/installing.html "安装/配置")