doucao1066 2019-04-29 22:29
浏览 175

使用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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题