EzDevInfo.com

PHPWord

A pure PHP library for reading and writing word processing documents

MS Word document file generate index

I use phpword to generate a MS Word document, is there any way help me to generate an index at the end of the generated file ?

one way I think may work, is to read the generated MS Word file and locate where each word is (e.g. page number in the MS Word file) and then regenerate the index in a separate MS Word file.

Is there any better method ?
Example of the required file :

A 
Animal 51,98 
Apple 11,54,99 

B
Basket 55  
...
..
etc

Source: (StackOverflow)

Convert HTML code to doc using PHP and PHPWord

I am using PHPWord to load a docx template and replace tags like {test}. This is working perfectly fine.

But I want to replace a value with html code. Directly replacing it into the template is not possible. There is now way to do this using PHPWord, as far as I know.

I looked at htmltodocx. But it seams it will not work either, is it posible to transform a peace of code like <p>Test<b>test</b><br>test</p> to a working doc markup? I only need the basic code, no styleing. but Linebreaks have to work.


Source: (StackOverflow)

Advertisements

Add chart on phpoffice/phpword

I know how to add charts on PHPExcel, but I also need to insert charts on a docx file. Is it possible to manipulate charts with phpoffice/phpword?

If it's not possible, do you know a good library for the job?


Source: (StackOverflow)

PHPWord fixed cell width

I am trying to create a table with cells width a fixed width.
Let's say I have this code:

$table->addCell(300)->addText('first');
$table->addCell(300)->addText('text that is very looooooooong');

The first cell's 300 width is ignored and is crushed against the left side of the page like this:

f
i
r
s
t

My second cell is going to contain large texts, but I want that cell to maintain it's width.
I couldn't find it anywhere on the web so I am asking if somebody knows what I will have to do here.

Thanks


Source: (StackOverflow)

Auto download the file attachment using PHP Word

I'm trying to use PHPWord to generate word documents. And the document can be generated successfully. But there is a problem where my generated word document will be saved on the server. How can I make it available to download straight away?

Sample:

$PHPWord = new PHPWord();
//Searching for values to replace
$document = $PHPWord->loadTemplate('doc/Temp1.docx');
$document->setValue('Name', $Name);
$document->setValue('No', $No);
$document->save('php://output'); //it auto save into my 'doc' directory.

How can i link to the header to download it as follows:

header("Content-Disposition: attachment; filename='php://output'"); //not sure how to link this filename to the php://output..

Kindly advise.


Source: (StackOverflow)

Add image in a .docx with PHPWord using TemplateProcessor

I don't know if is possible to add an image in a template with PHPWord using TemplateProcessor.

I have a document (form.docx). This document is a template. I read the fields (${name} for example) and replace it with a text.

$template = new TemplateProcessor('form.docx');
$template->setValue('name', $name);

But now, I want to put an image but I don't know how I do it. I try with this:

$img='img.jpg';
$template->setValue('image',$img);

Doesn't works. I try other form, creating a section and add this section to template but this fails.

$phpWord = new PhpWord();
$section = $phpWord->createSection();   

$section->addImage('img.jpg', array('width'=>210, 'height'=>210, 'align'=>'center'));
$template = new TemplateProcessor('form.docx'); 
$template->setValue('image',$section).

Anyone know how to put an image in a .docx using a template?

Thanks.


Source: (StackOverflow)

manipulating template in PHPWord

I am using a word document generator for PHP for the reports module of the web-app i am developing. I choose PHPWord because the free version of PHPDocX has very limited functionality plus it has a footer that it is only a free version. I have a template given by the client. What I want is I want to load the template and add dynamic elements to it like additional text or tables. My code is here:

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

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Template.docx');
$document->setValue('Value1', 'Great');

$section = $PHPWord->createSection();
$section->addText('Hello World!');
$section->addTextBreak(2);

$document->setValue('Value2', $section);

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

I tried to create a new section and tried to assign it to one variable in the template(Value2) but this error appeared:

[28-Jan-2013 10:36:37 UTC] PHP Warning:  utf8_encode() expects parameter 1 to be string, object given in /Users/admin/localhost/PHPWord_0.6.2_Beta/PHPWord/Template.php on line 99

Source: (StackOverflow)

How to align text and image side by side in phpWord?

I need to align a logo and text side by side. But when generating the word document, the text always comes beneath the logo image.I tried the solution pointed out here: http://phpword.codeplex.com/discussions/232684

But it didn't work for me. Here's what i tried (not the solution mentioned above)

$section = $PHPWord->createSection();
$image = $section->addImage('_mars.jpg',array ('width'=>100));
// Add table
$table = $section->addTable(); 
for($r = 1; $r <= 1; $r++) { // Loop through rows
    // Add row
    $table->addRow();
    for($c = 1; $c <= 1; $c++) { // Loop through cells
      // Add Cell
     //I tried adding image in this line.
     $table->addCell(1750)->addText("Row $r,Cell ".
$section->addImage('_mars.jpg',array('width'=>100)));
    }
}

and I'm getting this error in

$section->addImage() partCatchable fatal error: Object of class PHPWord_Section_Image could not be converted to string in 

Can anyone tell me how I can add image in table cell ?


Source: (StackOverflow)

PHPWord: Creating an Arabic right to left word document

I'm trying to use PHPWord to create a word document that will include dynamic data pulled out from a MySQL database. The database has MySQL charset: UTF-8 Unicode (utf8) MySQL connection collation: utf8_unicode_ci and so does the table fields.

Data is stored and previewed fine in HTML, however when creating the document with the arabic variables, the output in Word looks like أحÙد Ùبار٠اÙÙرÙ.

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('templates/.../wtvr.docx');
$document->setValue('name', $name);
$document->setValue('overall_percent_100', $overall_percent_100);
$document->save('Individual Report - ' . $name . '.docx');

Is there anyway to fix that?


Source: (StackOverflow)

How to change PHPWord table cell height?

When im creating a simple table with PHPTable the rows seems to be a bit too height. I would like them to be the same heights as the font, and with no padding/spacing below or above the text in the cells.. but can't get it to work without any "padding"...

Code:

$styleTable = array('borderSize'=>0, 
                    'borderColor'=>'eeeeee',
                    'cellMargin'=>0, 
                    'spaceBefore' => 0, 
                    'spaceAfter' => 0,
                    'spacing' => 0);

$PHPWord->addTableStyle('myOwnTableStyle', $styleTable);

$table = $section->addTable('myOwnTableStyle');

foreach($aryData['json_data'] as $data) {
  $table->addRow();
  $table->addCell(4000)->addText($data['label'] . ':', array('bold'=>true));
  $table->addCell(8000)->addText($data['value']);
}

Source: (StackOverflow)

How to convert .docx file into .doc file with PHP?

I want to read the text from a .docx file line by line and keep the each line data in an array, since .docx is a zipped file i want to convert it into a .doc file so that I can read the file using @fopen($filename, 'r');.

Below is the code I tried using PHPWord to conver from .docx to .doc ,

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

$PHPWord = new PHPWord();

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

// Save File

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

$objWriter->save('BasicTable4.doc');
?>

and this creates an erroneous .doc file.


Source: (StackOverflow)

Nothing gets printed in the doc using PHPWord

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


Source: (StackOverflow)

Finding and replacing text with PHPWord

Is it possible to just do a simple find and replace for text on a Word document using PHPWord? From what I've seen, the closest you can get is just ADDING text to a section, and you can't manipulate existing text except for font, etc. If not, is there anything free that I can use to do this?


Source: (StackOverflow)

PHPWord horizontal line (
)

I am currently using PHPWord to generate my documents, but I want to add an horizontal line in the document. Just like an


in HMTL. In word you can do this by typing three underscores en enter, but I want to use it in my generated document.

Does anybody have more information about this feature?

Thank you!


Source: (StackOverflow)

PhpWord doesn't replace text

I have a docx file and I need to replace some text. This is done inside codeigniter framework; here is the code:

$this->load->library('word');       
$template = $this->word->loadTemplate($_SERVER['DOCUMENT_ROOT'].'/doc/assets/doc3.docx');
$template->setValue('replacename', 'new');
$template->save($_SERVER['DOCUMENT_ROOT'].'/doc/assets/helloWorld.docx');

When I open the new file I still get "replacename" instad of "new". "replacename" is formatted with Verdana font, 9pt font size (no underline or bold). Why it doesn't work? Removing ${ } from setValue function (and from doc file) it works


Source: (StackOverflow)