Saving PHPWord_Template to the client

I downloaded the PHPWord_0.6.2_Beta.zip package from this site

There are 2 references: Examples of both PHPWord and 1 file: PHPWord.php
I put the PHPWord directory and PHPWord.php in application / third_party and created a new library called "Word.php" in the application / libraries as described in this article

Here is the new Word.php library code

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH."/third_party/PHPWord.php"; 

class Word extends PHPWord { 
    public function __construct() { 
        parent::__construct(); 
    } 
}
?>

      


Now we can easily use PHPWord as CI library

I tried a text example from the directory Example in the downloaded package, here is the source code

<?php
require_once '../PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(2);

$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);

$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');



// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
?>

      


And I tried to implement it in my CI controller, here is the PHPWord_Text.php code

<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class PHPWord_Text extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('word');
    }
    function index() {
        $PHPWord = $this->word; // New Word Document
        $section = $PHPWord->createSection(); // New portrait section
        // Add text elements
        $section->addText('Hello World!');
        $section->addTextBreak(2);
        $section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
        $section->addTextBreak(2);
        $PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
        $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
        $section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
        $section->addText('I have only a paragraph style definition.', null, 'pStyle');
        // Save File / Download (Download dialog, prompt user to save or simply open it)
        $filename='just_some_random_name.docx'; //save our document as this file name
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
        header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what the file name
        header('Cache-Control: max-age=0'); //no cache
        $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
        $objWriter->save('php://output');
    }
}
?>

      


Access to it in

http://localhost/codeigniter/index.php/PHPWord_Text

      


Foilaa !!! my code works! But ... I am a little confused when I tried to translate the example template into the Examples directory from the downloaded package to the new CI controller, here is the source code of Template.php

<?php
require_once '../PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Template.docx');

$document->setValue('Value1', 'Sun');
$document->setValue('Value2', 'Mercury');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');

$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));

$document->save('Solarsystem.docx');
?>

      


Can anyone help me with this problem? you are welcome. T_T
NOTE : I don't want it to be saved on the server, I want it to show the download dialog prompt to save it or open it, works like some code here

// Save File / Download (Download dialog, prompt user to save or simply open it)
$filename='just_some_random_name.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');

      

+3


source to share


5 answers


I think you may run into several problems with Template.php.

First, you can try to figure out what it does.

In other examples, x.php produces x.docx. In the case of Template.php, it uses Template.docx to create Solarsystem.docx.

Variables Value1, Value2, Value3, etc. all are defined in Template.docx as $ {Value1}, $ {Value2}, $ {Value3} etc. If you change the values ​​on the right in Template.php it will change the corresponding values ​​in Solarsystem.docx.



Second, you seem to be worried about getting out.

What works for me:

$filename = 'nameOfFile.docx';
$document->save($filename);
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);

      

This method still saves it to the server, but downloads it as well. If I use both $document->save();

and $objWriter->save();

, I get a blank document when I try to use readfile();

in a file saved by objWriter.

+2


source


I believe this is correct:



 $filename = 'nameOfFile.docx';
    $document->save($filename);
    header('Content-Description: File Transfer');
    header('Content-type: application/force-download');
    header('Content-Disposition: attachment; filename='.basename($filename));
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($filename));
    readfile($filename);
    unlink($filename);

      

+1


source


The save()

class method Template

returns the filename temp file, so you just give it to the user with readfile($tempName)

;

0


source


I'm not sure if I understood your question correctly, so excuse me if I'm wrong.

In the fourth block of code, you have the line

$document->save('Solarsystem.docx');

      

If you don't want to save the file to your local drive, but instead send it to the client, you just need to remove this line and add the code from the fifth block of code:

// Save File / Download (Download dialog, prompt user to save or simply open it)
$filename='just_some_random_name.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-        officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');

      

-1


source


<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once APPPATH . '/src/PhpWord/Autoloader.php';

use PhpOffice\PhpWord\Autoloader as Autoloader;
Autoloader::register();

      

-1


source







All Articles