How can I download a word file with PhpWord and Ajax

0

I have a problem to download files with Ajax and PHP, when I open directly my file .php if I download the file, but when I call it from Ajax it only generates the file and does not download it,

My Ajax function:

$.ajax({ url:"php/word_calendario.php", method:"POST", data:{ "desde": desde, "hasta": hasta }, success: function (data) { $('#calendar').fullCalendar('refetchEvents'); UIkit.modal("#modal-reportes", {bgclose: false, keyboard:true}).hide(); UIkit.notification( { message: data, status: 'success', pos: 'top-center', timeout: 5000 } ); }, complete: function(data){ } })

and my PHP:

<?php
require_once '../vendor/phpoffice/phpword/bootstrap.php';
use PhpOffice\PhpWord\Style\Language;
// nuevo documento
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('TEXTO DE PRUEBA');
$myTextElement->setFontStyle($fontStyle);

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

header("Content-Disposition:attachment;filename='helloWorld.docx'");
echo file_get_contents('helloWorld.docx');
?>
    
asked by arglez35 13.11.2018 в 00:08
source

0 answers