我已经编写了一个从本地文件生成模板的代码。正常工作。但是现在我想使用存储在数据库中的模板作为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 />
有人可以帮助我吗?