我正在为我正在开发的web-app的报告模块使用PHP文档生成器.我选择PHPWord是因为PHPDocX的免费版本功能非常有限,而且它有一个页脚,它只是一个免费版本.我有一个客户提供的模板.我想要的是我想加载模板并添加动态元素,如附加文本或表格.我的代码在这里:
<?php
require_once '../PHPWord.php';
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('Template.docx');
$document->setValue('Value1', 'Great');
$section = $PHPWord->createSection();
$section->addText('Hello World!');
$section->addTextBreak(2);
$document->setValue('Value2', $section);
$document->save('test.docx');
?>
Run Code Online (Sandbox Code Playgroud)
我尝试创建一个新的部分,并尝试将其分配给模板中的一个变量(Value2),但出现此错误:
[28-Jan-2013 10:36:37 UTC] PHP Warning: utf8_encode() expects parameter 1 to be string, object given in /Users/admin/localhost/PHPWord_0.6.2_Beta/PHPWord/Template.php on line 99
Run Code Online (Sandbox Code Playgroud) 是否可以使用PHPWord对Word文档中的文本进行简单的查找和替换?从我所看到的,你可以得到的最接近的只是添加文本到一个部分,你不能操纵现有的文本除了字体等.如果没有,有什么免费的,我可以用来做到这一点?
我正在使用PHPWord模板处理器在模板中插入一些文本.
单词模板格式为LTR,因为所有标签都是英文.
这是单词模板中的行:
User Name: ${name}
Run Code Online (Sandbox Code Playgroud)
以下是替换值的PHP行:
$template->setValue('name', $user->name);
Run Code Online (Sandbox Code Playgroud)
这句话是双向的.标签为英文(LTR),用户名始终为阿拉伯语(RTL).
解压缩word文件后,这是document.xml中生成的代码行:
<w:r><w:rPr><w:b/><w:bCs/><w:lang w:val="en-US" w:bidi="ar-EG"/></w:rPr><w:t>User Name:</w:t></w:r><w:r><w:rPr><w:lang w:val="en-US" w:bidi="ar-EG"/></w:rPr><w:t xml:space="preserve"> ???? ????</w:t><w:tab/></w:r>
Run Code Online (Sandbox Code Playgroud)
替换的文本在LibreOffice中正确显示RTL,但在Microsoft Word中显示为LTR(反转).
我该怎么做才能使它在Microsoft Word中正确显示(RTL)?
我正在尝试使用PHPWord生成word文档.并且可以成功生成文档.但是有一个问题是我生成的word文档将保存在服务器上.我怎样才能立即下载?
样品:
$PHPWord = new PHPWord();
//Searching for values to replace
$document = $PHPWord->loadTemplate('doc/Temp1.docx');
$document->setValue('Name', $Name);
$document->setValue('No', $No);
$document->save('php://output'); //it auto save into my 'doc' directory.
Run Code Online (Sandbox Code Playgroud)
我如何链接到标题下载它如下:
header("Content-Disposition: attachment; filename='php://output'"); //not sure how to link this filename to the php://output..
Run Code Online (Sandbox Code Playgroud)
好心提醒.
我有一个HTML字符串,我想以与HTML中相同的格式和样式在word文档中打印.我正在使用PHPWord
当我给我的HTML字符串说:
$htmlval ="<h6><div style="text-align: center;"><b style=\\"font-size: x-large;\\">OFFER LETTER</b></div><font size=\\"3\\"><div style="text-align: center;"><span style=\\"font-size: medium;\\">(This goes as an email copy)</span></div>";
这是带有break和div的HTML标签.
至:
$section->addText($htmlval);
Run Code Online (Sandbox Code Playgroud)
它打印所有HTML标签,但我希望内容具有HTML中指定的格式.
我在PHPWord上有一个模板实例.是否可以替换或添加图像?有点像setImageValue吗?
$phpWord = new \PhpOffice\PhpWord\Template('a.docx');
$phpWord->setImageValue('IMAGE_PLACEHOLDER', 'a.jpg');
$phpWord->saveAs('b.docx');
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?
我正在.docx使用模板创建文件PHPWord.它工作正常,但现在我想将生成的文件转换为PDF.
首先我尝试tcpdf结合使用PHPWord
$wordPdf = \PhpOffice\PhpWord\IOFactory::load($filename.".docx");
\PhpOffice\PhpWord\Settings::setPdfRendererPath(dirname(__FILE__)."/../../Office/tcpdf");
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF');
if (file_exists($filename.".pdf")) unlink($filename.".pdf");
$pdfWriter->save($filename.".pdf");
Run Code Online (Sandbox Code Playgroud)
但是当我尝试加载文件以将其转换为PDF我在加载文件时遇到以下异常
致命错误:未捕获的异常'BadMethodCallException',消息'无法在Section中添加PreserveText.'
经过一些研究后我发现其他一些人也有这个bug(phpWord - 无法在Section中添加PreserveText)
编辑
在尝试了一些后,我发现,
Exception只有当我mail merge fields在我的文档中有一些时才会发生.一旦我删除它们Exception就不再出现了,但转换后的
我想过用另一种方法生成PDF,但我只能找到4种方法:
PHPWord和更好tcpdf,但仍然不能用作图像丢失,以及大多数(不是全部!)的样式有没有第五种方法来生成PDF?或者是否有任何解决方案可以使生成的PDF文档看起来不错?
基于Laravel的应用程序将文档(.doc,.docx,.pdf,.png,.otd,html等)转换为PDF,以便它们可以合并到一个主PDF文档中.它使用PHPWord和DOMPDF Wrapper等插件的组合来进行文件加载和创建.每隔一段时间,该进程由于Word文件而遇到错误.
错误:PhpOffice\PhpWord\Exception\InvalidImageException:图片无效:zip:// ... #word/media/image2.emf
该错误是由文档中的图像背景引起的,其作用类似于水印.错误输出的PHPWord部分是PhpOffice\PhpWord\Element\Image->checkImage()方法,但是在尝试加载文件时发生.
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
$pdfWord = IOFactory::load(storage_path() . '/app/uploads/randomfile.docx', 'Word2007');
Run Code Online (Sandbox Code Playgroud)
应用程序如何将嵌入了EMF图像的Word文档转换为PDF?
有关如何重新创建错误的更多代码/信息,Github PHPWord库中存在一些问题.
环境相关信息:
编辑:
我也试图从不同的角度来看这个,但没有用.我试图使用PHP的ZipArchive解压的docx文件,从文件(删除EMF图像ZipArchive::deleteName()),除去在该参考EMF图像[Content_Types].xml(ZipArchive::getFromName()),然后将压缩文件的docx备份,但没有奏效.我可以打开新的docx文件并看到图像消失,但PHPWord错误仍然存在于应用程序中.
我有一个docx文件,我需要替换一些文本.这是在codeigniter框架内完成的; 这是代码:
$this->load->library('word');
$template = $this->word->loadTemplate($_SERVER['DOCUMENT_ROOT'].'/doc/assets/doc3.docx');
$template->setValue('replacename', 'new');
$template->save($_SERVER['DOCUMENT_ROOT'].'/doc/assets/helloWorld.docx');
Run Code Online (Sandbox Code Playgroud)
当我打开新文件时,我仍然得到"new"的"replacename"instad."replacename"格式为Verdana字体,9pt字体大小(无下划线或粗体).为什么它不起作用?从setValue函数(以及doc文件)中删除$ {}它可以正常工作
我使用laravel为我的应用程序和dompdf找到:
../vendor/dompdf/dompdf
我想要实现的是将我的.docx文件(Microsoft Word)转换为.pdf文件.docx文件是由phpWord通过加载模板文件并替换值生成的.
这是片段:
// Get the absolute path of the template file
$wordTemplatePath = $this->getDocumentTemplatePath('resignation.docx');
// Load the template file
$document = $this->phpWord->loadTemplate($wordTemplatePath);
// This will be filled with data
$wordData = []
.... Process to fill the data .....
// Replace value to actual word document
$this->setTemplateValues($wordData, $document);
// Generate its filename
$file = $this->generateFileName('Retirement-Certificate.docx', $id);
// Fetch the absolute path to save the document
$filepath = $this->getSavePath($file);
// Save the word document
$document->saveAs( $filepath );
Run Code Online (Sandbox Code Playgroud)
之后,将生成.docx文件.我也想制作一个PDF版本.所以我搜索并找到了这段代码片段并添加到我的代码中: …