I'm aware this might be more a question of understanding PHP Namespaces,etc...but I still can't get my head around the following:
I have an MVC controller with the below usages:
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style\Paper;
use PhpOffice\PhpWord\Style\Language;
...and the following function:
public function word(){
$phpword= new PhpWord();
$settings=$phpword->getSettings();
//$languageFrFr = new Language('FR_FR');
//$settings->setThemeFontLang($languageFrFr);
$docInfo=$phpword->getDocInfo();
$docInfo->setCreator('JMB');
$docInfo->setCreated(date("d-m-Y"));
$paper = new Paper();
$paper->setSize( 'A4' );
$section = $phpword->addSection();
$section->addText('An example dummy text...');
$file = 'test.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpword, 'Word2007');
$xmlWriter->save("php://output");
This works perfectly (eg the WORD file & it's content are output correctly...
UNTIL...I uncomment & try to set the 'ThemeFontLang' : function hangs when I load:
$languageFrFr = new Language('FR_FR');
Any clue why this is happening, and how I could avoid / bypass the long paths\namespaces definitions inside my function ?
Many thanks,
JMB
Related
I have this code:
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\TemplateProcessor as Tpl;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\Table;
$file = '/var/www/html/writable/docs/incarico.docx';
$download = '/var/www/html/writable/docs/new.docx';
$tpl = new Tpl($file);
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$table = $section->addTable('table');
$table->addRow(); # <--- generate the error
$table->addCell(1750)->addText(htmlspecialchars("test 1"));
$table->addCell(1750)->addText(htmlspecialchars("test 2"));
$tpl->setComplexValue('riferimenti_pratica', $table);
$tpl->saveAs($download);
header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type($download));
header('Content-Disposition: attachment; filename="' . basename($download) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($download));
readfile($download);
exit;
And this code works well in openoffice and some online viewers, but I cant' open it in microsoft word.
The problem is with addRow() method.
It works if I comment these lines (of course nothing happen but at least I can open it with microsoft word):
$table->addRow();
$table->addCell(1750)->addText(htmlspecialchars("test 1"));
$table->addCell(1750)->addText(htmlspecialchars("test 2"));
The version of phpword is 0.18.3 and the version of php is 8.1.7
I have installed PHPWord on my computer with Composer and Wamp64.
using a template, on my computer the template is well modified, and the recording of the file is perfect.
On the infomaniak server, I have a message that the file is corrupted and that there are some parts missing.
I can't find the reason. here is my code.
Please help me find the source of the problem
<?php
$docModele = 'print/modele_cdd_as_2.docx';
require_once '../PhpWord/bootstrap.php';
use\PhpOffice\PhpWord\PhpWord;
// Creating the new document...
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
header('Content-type: application/json; charset=UTF-8');
/* Note: any element you append to a document must reside inside of a Section. */
$document = $PHPWord->loadTemplate($docModele);
$document->setValue('date_debut', $date_debut);
$document->setValue('identite', $identite.' ');
$document->setValue('date_naissance', $date_naissance);
$document->setValue('lieu_naissance', $lieu_naissance);
$document->setValue('dpt_naissance', $dpt_naissance);
$document->setValue('adresse1', $adresse1);
$document->setValue('cp', $cp);
$document->setValue('ville', $ville);
$document->setValue('no_ss', $no_ss);
$document->setValue('jour_debut', $jour_debut);
$document->setValue('date_fin', $date_fin);
$document->setValue('remplace', $remplace);
$document->setValue('motif_txt', $motif_txt);
$document->setValue('essai', $essai);
$document->setValue('indice', $indice);
$document->setValue('valeur_point', $valeur_point);
$document->setValue('brut', $brut);
$document->setValue('majoration', $majoration);
$document->setValue('date_contrat', $date_contrat);
//XML Writer compatibility
\PhpOffice\PhpWord\Settings::setCompatibility(false);
//#####################################################
// Save File
//#####################################################
//#####################################################
ob_clean();
$filename = 'nom_du_fichier.docx';
header("Content-Disposition: attachment; filename=$filename");
$document->saveAs('php://output');
//#####################################################
//#####################################################
exit;
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
// création du fichier
$objWriter->save($filename);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
?>
My code create Word template and instantly download when i run php file.
How to create a button that allow me to create and download word file when i click on it?
Thank you!
<?php
require_once 'vendor/autoload.php';
$phpword = new \PhpOffice\PhpWord\PhpWord();
$ran = time();
$section = $phpword->addSection();
$section->getStyle()
->setPaperSize('Letter')
->setLandscape()
;
$section->addText("Hello World!");
$file = 'test.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$phpword->save("php://output");
?>
You can make a curl request on a php file who create, save the word and return the path, then use js to make a auto-click on a hidden link who you have set the "href=" with the path and have the attribute "download=your_file_name".
Create a tmp folder the same directory youhave the code and add this
objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('tmp/helloWorld.docx');
$file = 'tmp/helloWorld.docx';
if (file_exists($file)) {`enter code here`
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
I can generate download a word document generated from scratch using PHPWord:
Create the PhpWord object:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
add sections and rows (ommitted) and create the header:
$datetime = date('Y_M_d_G_i');
$filename = 'receipt_' . $datetime . '.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
create a writer out of the phpWord object:
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
write to output:
$xmlWriter->save("php://output");
What I have failed to do is download a document from a template on the server:
Creating the TemplateProcessor works: No errors and PHP recognizes $templateProcessor as an object of that class:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor(asset_url() . 'templates/receipt_template.docx');
But I cannot work out how to write to output. If I could generate a PhpWord object then I could use the IOFactory:: createWriter method, but TemplateProcessor does not have methods that return a PhpWord object.
The closest I can get is to attempt to create a $phpWord document out of the IOFactory::load. But this just creates a blank document
$phpWord = \PhpOffice\PhpWord\IOFactory::load($templateProcessor->save());
<?
require_once "../include/PHPWord-develop/bootstrap.php";
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('contrato01.docx');
$templateProcessor->setValue('variable01', 'Sun');
$templateProcessor->setValue('variable02', 'Mercury');
//#####################################################
// Save File
//#####################################################
//#####################################################
header("Content-Disposition: attachment; filename='ejemplo.docx'");
$templateProcessor->saveAs('php://output');
//#####################################################
//#####################################################
?>
I am using PHPWord to download docx files in php. But nothing gets printed in the file if I try to download it. But the contents get displayed in the file which gets saved on the server. Below is the code which I have used. Can anyone please tell me what the issue is.
<?php
include "../includes/config.inc.php";
include '../PHPWord.php';
// Create a new PHPWord Object
$PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();
$section->addText('CANDIDATES DETAILS');
$filename='test';
$file=$filename.'.docx';
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('/form_doc/'.$filename.'.docx');
//download the file
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
Thanks in advance.
Regards,
Neha
Your should set path to file to readfile() and filesize() functions:
$file = $filename.'.docx';
$filepath = '/form_doc/' . $file;
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save($filepath);
//download the file
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);