使用blob文件作为docx模板

I have written a code that generate a template from local file. that works correctly. But now i want to use a template stored in the database as blob. Can anyone help me?

Here is my code:

The originial script:

<?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;
?>

I want to replace $templat_word with the blob field. It will be something like this:

    <?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);

But this gave me a blank file with the message:

<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-&gt;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 />

Can anyone help me?

php
dqfsbvd43312
dqfsbvd43312 是的,我理解。但是你在将它存储在数据库之前就这样做了吗?不是吗?您从数据库获取的数据是Word还是PDF文件?
一年多之前 回复
dpgjci27392
dpgjci27392 我使用phpOffice将其转换为pdf!
一年多之前 回复
dpf56454
dpf56454 逻辑上,如果您在数据库中保存了二进制文件,则在输出时不需要使用PhpOffice。只需要在脚本中输入最后两行,就需要echo$row['data];出口;
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问