php - 使用 blob 文件作为 docx 模板
问题描述
我编写了一个从本地文件生成模板的代码。可以正常工作。但现在我想使用存储在数据库中的模板作为 blob。谁能帮我?
这是我的代码:
原始脚本:
<?php
include_once 'PHPWord/Sample_Header.php';
$template_word = __DIR__.'/PlantillaFactura.docx';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_word);
$conn = mysql_connect("localhost","root","") or die("Error Connect to Database");
$objDB = mysql_select_db("word");
$strSQL = "SELECT Nif, NombreRazonSocial, Domicilio, RestoDomicilio, FechaFactura, TotalFactura FROM factura";
$objQuery = mysql_query($strSQL,$conn);
$data = mysql_fetch_array($objQuery);
$templateProcessor->setValue('NIF', $data['Nif']);
$templateProcessor->setValue('Nombre', $data['NombreRazonSocial']);
$templateProcessor->setValue('Direccion1', $data['Domicilio']);
$templateProcessor->setValue('Direccion2', $data['RestoDomicilio']);
$templateProcessor->setValue('Total', $data['TotalFactura']);
$FechaFactura=$data['FechaFactura'];
$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
$objDB = mysql_select_db("word");
$sql="SELECT count(*) Lineas FROM linea_factura";
$resql=mysql_query($sql);
if(!$resql){
die('No records founds');
}
$data=mysql_fetch_array($resql);
$NumeroLineas= $data['Lineas']; // número de líneaas de factura
// Simple table
$templateProcessor->cloneRow('rowArticulo', $NumeroLineas);
$sql="SELECT Nombre, Precio, Cantidad, Valor FROM linea_factura";
$rsSql=mysql_query($sql);
if(!$rsSql){
die('No records found');
}
$countLines=0;
while ($data2 = mysql_fetch_array($rsSql)){
$countLines=$countLines+1;
$templateProcessor->setValue('rowArticulo#'.$countLines, $data2['Nombre']);
$templateProcessor->setValue('rowPrecio#'.$countLines, $data2['Precio']);
$templateProcessor->setValue('rowCantidad#'.$countLines, $data2['Cantidad']);
$templateProcessor->setValue('rowValor#'.$countLines, $data2['Valor']);
}
// Date Local completed
$templateProcessor->setValue('FechaDeHoyCompleta');
// -------------------- v pie para salvar el nuevo documento Word ------------------
$temp_file = tempnam(sys_get_temp_dir(), 'Word');
$templateProcessor->saveAS($temp_file);
$documento = file_get_contents($temp_file);
header("Content-Disposition: attachment; filename= result.docx");
header('Content-Type: application/word');
echo $documento;
?>
我想用 blob 字段替换 $templat_word。它会是这样的:
<?php
$dbh = new PDO("mysql:host=localhost;dbname=file", "root", "");
$id = isset($_GET['id'])? $_GET['id'] : "";
$stat = $dbh->prepare("select * from files where id=?");
$stat->bindParam(1, $id);
$stat->execute();
$row = $stat->fetch();
header('Content-Type:'.$row['mime']);
$template_word = $row['data'];
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_word);
但这给了我一个带有消息的空白文件:
<br />
<b>Warning</b>: copy() expects parameter 1 to be a valid path, string given in <b>C:\xampp\htdocs\multi\PHPWord\vendor\phpoffice\phpword\src\PhpWord\TemplateProcessor.php</b> on line <b>82</b><br />
<br />
<b>Fatal error</b>: Uncaught exception 'PhpOffice\PhpWord\Exception\Exception' with message 'Can not clone row, template variable not found or variable contains markup.' in C:\xampp\htdocs\multi\PHPWord\vendor\phpoffice\phpword\src\PhpWord\TemplateProcessor.php:272
Stack trace:
#0 C:\xampp\htdocs\multi\generate_template.php(44): PhpOffice\PhpWord\TemplateProcessor->cloneRow('rowArticulo', '3')
#1 {main}
thrown in <b>C:\xampp\htdocs\multi\PHPWord\vendor\phpoffice\phpword\src\PhpWord\TemplateProcessor.php</b> on line <b>272</b><br />
谁能帮我?
解决方案
推荐阅读
- ansible - 如何修复 Ansible 错误:“冲突的操作语句:包含,文件”
- php - 使用 PHP 使用 LIMIT 调用参数化 MariaDB SP 时出现问题
- reactjs - 如何保护 Firebase 托管中的第三方 API 密钥?
- c# - 为什么 RabbitMQ 连接在 Linux 上失败,但在 Windows 上有效?
- javascript - 将 HTML 转换为 PDFmake 语法
- javascript - How To disable zoom in/out webview but increase font-size on Pinch
- java - 如何从动态 JCheckBox 中获取价值?
- email - DMARC 失败仅适用于linkedin
- css - 如何使用 id 属性以角度导入样式表?
- regex - 如何从php数组中grep值