export word / pdf templates

hi, i’m using phpword to export word templates but with little success, the problem is that i can’t get the document to download to the browse but i’ll save it on the server. can someone tell me where am i wrong?Code:

function Page_Exporting()
{
    //$this->ExportDoc->Text = "my header"; // Export header
    //return false; // Return false to skip default export and use Row_Export event
    return false; // Return true to use default export and skip Row_Export event
}

function Row_Export($rs)
{
    
    $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('templates/test.docx');
    $ordine = $rs["ordine"];
     $templateProcessor->setValue('ordine', $ordine);
     $templateProcessor->saveAs('php://output');

}

furthermore I would like to convert the word model into pdf, is it possible according to you want or should I use a library only for pdf models? and if so which one?

I haven’t used PHPWord to create custom Export to Word file so far.

But I have already used PhpSpreadsheet to create custom Export to Excel file and end-users will be able to download the file.

You may see the following logic in order to compare with PHPWord (don’t forget, Google is also your friend):

  function exportToExcel($okay = false) {
	if (Get("export") == "excel" && $okay == true) {
		$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
		$spreadsheet = $reader->loadFromString($_SESSION["HTML_String"]);
		$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
		ob_end_clean();
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment; filename="MyFilename_' . CurrentDateTime() . '.xls"');
		$writer->save('php://output');
		unset($_SESSION["HTML_String"]);
		exit();
	}
  }

As you can see, I stored the content into $_SESSION[“HTML_String”] variable. Make sure also to create the content header (see the 2 lines code of it), before saving it to the output file. To call the function, simply use: exportToExcel(true);.

Hopefully this would help you.

https://discourse.hkvstore.com/t/custom-word-pdf-export/6120/1