【问题标题】:PHPWord : Is it possible to replace placeholder text in a document with an image (at the same position where the text was)?PHPWord:是否可以用图像替换文档中的占位符文本(在文本所在的相同位置)?
【发布时间】:2021-11-03 12:10:25
【问题描述】:

作为电子签名功能实现的一部分。我需要在 Word 文档中的特定位置添加签名。然而,该位置可能因一份文件而异。唯一可能的解决方案似乎是使用签名占位符,一旦文档被签名,该占位符就会被 signature.png 替换。

PHPWord 包可以做到这一点吗?如果没有,是否有任何软件包可以做到这一点?

【问题讨论】:

  • 从未使用过PHPword,但根据this question,您可以替换图像源。所以也许可以在模板中添加一个图像(可能是一个 1x1 的空白 .png)并稍后替换源。

标签: php phpword electronic-signature


【解决方案1】:

根据他们的documentation,这是使用 phpword 的方法:

首先,word文件需要有一个占位符:

${CompanyLogo}
${UserLogo:50:50} ${Name} - ${City} - ${Street}
${Signature:50:50}

然后用php,我们可以像这样替换占位符:

$templateProcessor = new TemplateProcessor('Template.docx');
$templateProcessor->setValue('Name', 'John Doe');
$templateProcessor->setValue(array('City', 'Street'), array('Detroit', '12th Street'));

$templateProcessor->setImageValue('CompanyLogo', 'path/to/company/logo.png');
$templateProcessor->setImageValue('UserLogo', array('path' => 'path/to/logo.png', 'width' => 100, 'height' => 100, 'ratio' => false));
$templateProcessor->setImageValue('Signature', function () {
    // Closure will only be executed if the replacement tag is found in the template
    return array('path' => 'path/to/signature.png', 'width' => 100, 'height' => 100, 'ratio' => false);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 2021-11-06
    • 2010-09-15
    相关资源
    最近更新 更多