This topic is locked

Create invoice Word document using phpword

9/27/2018 4:21:25 PM
PHPRunner Tips and Tricks
fhumanes author


This is an example of how a DOCX document can be obtained from an application made in PHPRunner.

In the example I made a simple invoice and I hope it serves as an example for any other DOCX document that needs to be obtained.
The PHPOffice/PHPWord library is used and this example is only a small case of everything that is possible to do with this library.
The specific code is:



<?php

include_once 'PHPWord/Sample_Header.php';

$idfactura= $_SESSION['idfactura'] ; // identificación de factura a obtener

// Template processor instance creation

$template_word = __DIR__.'/PlantillaFactura.docx';

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_word);

// -------------------- ^ cabecera necesaria para las plantillas de Word ------------------

$sql="SELECT Nif, NombreRazonSocial, Domicilio, RestoDomicilio, FechaFactura, TotalFactura FROM factura where idfactura = $idfactura";

$resql=db_query($sql,$conn);

$data=db_fetch_array($resql);

// Variables on different parts of document

$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'];

$sql="SELECT count(*) Lineas FROM linea_factura where factura_idfactura= $idfactura";

$resql=db_query($sql,$conn);

$data=db_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 where factura_idfactura= $idfactura ";

$rsSql=db_query($sql,$conn);

$countLines=0;

while ($data2 = db_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

$date = DateTime::createFromFormat('Y-m-d', $FechaFactura);

$formatter = new IntlDateFormatter('es_ES', IntlDateFormatter::LONG, IntlDateFormatter::LONG);

$formatter->setPattern("d 'de' MMMM 'de' yyyy");

$mydate = $formatter->format($date);

$templateProcessor->setValue('FechaDeHoyCompleta', $mydate);

// -------------------- v pie para salvar el nuevo documento Word ------------------

$temp_file = tempnam(sys_get_temp_dir(), 'Word');

$templateProcessor->saveAS($temp_file);

// ------------------ Operation with file result -------------------------------------------

$documento = file_get_contents($temp_file);

header("Content-Disposition: attachment; filename= factura.docx");

header('Content-Type: application/word');

echo $documento;

?>


However, in my BLOG there is more explanation and the complete example in PHPRunnr 9.8 so that you can run it on your machines.
Greetings,

fhumanes author 9/28/2018

You can temporarily execute it in: Demo

G
Grdimitris 9/29/2018

excellent and it is very very fast.

Now i`m trying to fit invoice in one page if has to many rows.
Thank you.