【问题标题】:How insert text into word and convert it into pdf [closed]如何将文本插入word并将其转换为pdf [关闭]
【发布时间】:2021-06-30 06:08:41
【问题描述】:
| First Name |
Last Name |
File Location |
| Andy |
Lee |
|
| Jason |
Hulce |
|
| Ibrahim |
Muhammad |
|
我正在尝试为表中列出的所有用户制作电子证书。我已将数据导入我的数据库(phpmyadmin)。
我设计了一个 Microsoft Word 证书,只是想知道是否可以在文件中插入参与者的姓名并将其转换为 pdf?大约有 500 名参与者,如果我要逐字更新的话,很费时间。
我正在尝试用 PHP 和 JS 来实现。你们对如何使这项工作有任何其他想法吗?
【问题讨论】:
-
创建动态 html 并使用 mpdf 将其转换为 pdf
标签:
javascript
php
pdf
ms-word
【解决方案1】:
是的,您可以使用名为 ZipArchive 的类来做到这一点。
我正在添加示例代码。您可以根据需要进行更改。
<?php
$template_file_name = 'certificate.docx';
$rand_no = rand(111111, 999999);
$fileName = $rand_no . ".docx";
$folder = "results";
$full_path = $folder . '/' . $fileName;
try
{
if (!file_exists($folder))
{
mkdir($folder);
}
//Copy the Template file to the Result Directory
copy($template_file_name, $full_path);
// add calss Zip Archive
$zip_val = new ZipArchive;
//Docx file is nothing but a zip file. Open this Zip File
if($zip_val->open($full_path) == true)
{
// In the Open XML Wordprocessing format content is stored.
// In the document.xml file located in the word directory.
$key_file_name = 'word/document.xml';
$message = $zip_val->getFromName($key_file_name);
$timestamp = date('d-M-Y H:i:s');
// this data Replace the placeholders with actual values
$message = str_replace("full_name", "John Doe", $message);
$message = str_replace("email_address", "abc@email.com", $message);
//Replace the content with the new content created above.
$zip_val->addFromString($key_file_name, $message);
$zip_val->close();
}
}
catch (Exception $exc)
{
$error_message = "Error creating the Word Document";
var_dump($exc);
}
?>