Also, the Word document must be able to contain dynamically generated images.. PHPLiveDocx http://www.phplivedocx.org, PHPWord http://phpword.codeplex.com/ – involves some complications:
Trang 1The problem
I needed to generate a Word document from PHP (more exactly, from CodeIgniter)
The Word document acts as a template which receives values from PHP and adds them to predefined placeholders. Also, the Word document must be able to contain dynamically generated images
Candidate solutions:
using an “export to .doc(x)” library (e.g. PHPLiveDocx (http://www.phplivedocx.org), PHPWord
(http://phpword.codeplex.com/)) – involves some complications: integration with CodeIgniter issues,
the need to express the Word document markup using PHP (not good for a 10 page document, nor for template changes);
1
generating the Word document using COM (http://www.phpbuilder.com/columns
/yunus20031124.php3?page=2) – while promising, it involves requirements that aren’t always
achievable: a Windows server with Microsoft Word installed on it; also, as Microsoft explains
(http://support.microsoft.com/kb/257757), this solution may cause slow processing on the server, due
to the fact that Office wasn’t intended to be a server‐side solution;
2
exporting to RTF (http://www.phpclasses.org/browse/file/7158.html) – RTF is a documented format
(http://www.microsoft.com/downloads/details.aspx?FamilyId=DD422B8D‐FF06‐4207‐
B476‐6B5396A18A2B&displaylang=en) (in some >270 pages), so it should play nicely with PHP. After
looking at some RTF documents containing images, I found out that (1) their size was increasing quite
fast as new images were added and (2) the image format wasn’t quite easy to represent;
3
outputting (X)HTML, with Word headers and extension (http://www.phpclasses.org/package/2631‐
PHP‐Create‐Microsoft‐Word‐document‐without‐COM‐objects.html) – a nice hack that gave me some
hope. I used this idea in a different manner: I created a well formed Word document and then saved it
as “Web Page, Filtered”. All went well until I bumped into this solution’s major drawback: inability to
embed images – only links to them. Obviously, this affects the generated documents’ portability. Of
course, I tried using Base64 encoded images (http://dean.edwards.name/weblog/2005/06/base64‐ie/);
they were visible when I opened the document in a browser, but Word wasn’t able to display them
(http://social.msdn.microsoft.com/Forums/en/innovateonoffice/thread/9d0c628b‐d6e4‐4ab8‐8f6c‐
c4c05b7f01e3). So I tried other hacks (like exporting to MHTML, or “Single File Web Page”), and
serving that with Word headers and extension, but the result wasn’t good either
4
My solution
About these ads (http://en.wordpress.com/about‐these‐ads/)
Trang 2While looking for solutions, I tried Microsoft’s Word 2003 XML format (http://en.wikipedia.org
/wiki/Microsoft_Office_2003_XML_formats) (also, check out the XML schemas (schemata?)
(http://www.microsoft.com/downloads/details.aspx?FamilyId=FE118952‐3547‐420A‐
A412‐00A2662442D9&displaylang=en) for this format). I followed the same pattern:
‐ create a nicely formatted document,
‐ export it to Word 2003 XML,
‐ serve it from PHP with special headers and .doc extension
It was pure bliss to find out that in this format the images are saved in a familiar base64 encoding (easily
doable using PHP’s base64_encode() (http://php.net/manual/en/function.base64‐encode.php)). Problem
(almost) solved! “Almost” because now I have to generate the proper XML tags surrounding the
embedded images – it only requires some research
So, with only plain text you can have a properly formatted Word document with embedded images! … and PHP
can handle plain text quite easily…
I’ve tried to open the Word 2003 XML document in OpenOffice.org 3.1. Unfortunately, Writer wasn’t
fooled by the .doc extension and opened the document as plain text. Only after changing the document’s
extension to .xml, the editor opened it correctly. So, the documents are portable after all
How this looks in my CodeIgniter context?
The View
First, I took the generated XML document, prepared it a bit and placed it into the application’s views
folder. The “preparation” consisted in:
‐ changing the file’s extension from .xml to .php;
‐ making sure the file format is UTF‐8 without BOM (http://www.w3.org/International/questions
/qa‐utf8‐bom); the line ending I chose is CRLF, but you may try the non‐Windows alternatives (CR or LF);
‐ reformatting the XML from a huge one‐liner to a more readable form (Microsoft Expression Web
(http://www.microsoft.com/expression/default.aspx) does a great job at formatting XML/(X)HTML
documents);
‐ removing the tab characters (\t) from the resulting document, so that they don’t mess the output;
‐ changing the XML headings for them to be interpreted as intended:
from:
to:
‐ putting the appropriate PHP variables into their view placeholders:
You may also try to use {templates} (just remember to load the template parser library
(http://codeigniter.com/user_guide/libraries/parser.html) beforehand: $this‐>load‐>library(‘parser’);)
1
2
<?xml version="1.0" encoding="utf‐8" standalone="yes"?>
<?mso‐application progid="Word.Document"?>
1
2
<?php echo '<?xml version="1.0" encoding="utf‐8" standalone="yes"?>
<?mso‐application progid="Word.Document"?>'; ?>
1 <?php echo $report_date;?>
Trang 3I prepared the data to be sent to the view as usual (http://codeigniter.com/user_guide/general
/views.html). Before loading the view I added several headers, just to make sure everything goes OK:
I hope this helps. Oh, did I mention that the same idea can be used to obtain formatted Excel
spreadsheets (see xls‐sample.xls (http://klewos.files.wordpress.com/2010/04/xls‐sample.xls))?
P.S. A more maintainable and proper solution would be to use XML and XSLT (something like this
(http://msdn.microsoft.com/en‐us/library/ee840137(office.12).aspx), albeit the example refers to the Office
Open XML format), but this might not be as quick as simply filling placeholders in a view
Update (5 July 2010)
See a trivial demo here (http://mydemo.ueuo.com/). Use it to generate a sample document; then drag the
document to Notepad or another plain text editor to observe the xml
Citește și:
Running Multiple MySQL Scripts from a Batch File – the Quick and Dirty Way
(http://klewos.wordpress.com/2010/03/30/running‐multiple‐mysql‐scripts‐from‐a‐batch‐file‐the‐quick‐
and‐dirty‐way/)
Reinventez roata (http://klewos.wordpress.com/2010/03/20/reinventez‐roata/)
Un hack ordinar (http://klewos.wordpress.com/2010/01/17/un‐hack‐ordinar/)
CRUD în CodeIgniter (http://klewos.wordpress.com/2010/03/02/crud‐in‐codeigniter/)
/click?wylz843osj_mSYioeBKwPwAAAAAAAPA_5kmIqHgSsD_DKXPzjeiyP6SGw5PuBoVAlSTggiPmrg53 gDrPAAAy30AAgUCAQIAAIIADCbvOgAAAAA./cnd=%218wV‐NAiT2VUQ1pnJAhjF2gggAA /re word‐document‐quick‐tip%2F/clickenc=http%3A%2F%2Flp.kingtranslate.com%2F%3Fappid%3D133 This entry was posted in Calculatoare and tagged CodeIgniter, file format, Microsoft Word, PHP, XML
1
2
3
4
5
6
7
8
9
#$doc_data is the array that is sent to the view
$filename = "report".date('dmY‐his').".doc";
header("Content‐Type: application/xml; charset=UTF‐8");
header("Expires: 0");
header("Cache‐Control: must‐revalidate, post‐check=0, pre‐check=0");
header("content‐disposition: attachment;filename=$filename");
$this‐>load‐>view('templates/default', $doc_data);
return; //needed so that any redirect() after this line doesn't mess up things,
Trang 411 comentarii
akash spune:
31 august 2010 la 11:27
Hi all,
In exporting MS word documents using codeigniter I am applying the following code and working
fine, But I need to format the MS word document for page setup, such as height width and landscape
or portrait etc
Any idea or tutorial to do this please?
$data = “My Data”;
$html= $this‐>load‐>view(‘myviewfile’, $data, true);
header(“Content‐Type: application/vnd.ms‐word”);
header(“Expires: 0″);
header(“Cache‐Control: must‐revalidate, post‐check=0, pre‐check=0″);
header(“Content‐disposition: attachment; filename=\”mydocumentname.doc\”ʺ);
echo $html;
exit;
Thanks in advance
akash
Alin spune:
31 august 2010 la 11:42
Hello,
First you need to design your document (including formatting and page setup) using Word. After that
you export the document to .xml (Word 2003 XML) and you do just as I described in this article. It’s
pretty straightforward
If you want to edit document properties afterwards look for the document properties in the .xml,
they’re quite easy to figure out
akash spune:
31 august 2010 la 13:15
Hello,
If I want exporting it directly to .doc format then where I shall change? And what will be the change in
my view file?
Thanks
Akash
Alin spune:
31 august 2010 la 13:27
Sorry, but have you read and tried the approaches mentioned in the article? It was all about saving
Word documents in a special xml format with a .doc extension. These documents can be opened by
Trang 5MS Word and OpenOffice.org. The alternatives are also mentioned in the article, so take your time,
explore them, find out their strengths and weaknesses, and choose an appropriate solution for your
concrete needs. I’m afraid that’s about all I can do for you
YOGESH spune:
13 noiembrie 2010 la 07:29
While creating .doc file on Fly as mentioned above how can i put an image into my documents ( Not
using as hyper link or online images )
As inserting Logo or header or various other images displayed on web pages
Alin spune:
13 noiembrie 2010 la 12:26
First, you need to study a bit the way Word XML 2003 handles images. You’d have to create a simple
document in this format, in which you should include an image. See how it’s nested in the XML
hierarchy (i.e. tags like w:pict, w:binData, v:shape, v:imagedata). Then you need to output the image’s
content in a base64 encoded form and to convert pixels to points for the appropriate attributes (height, width). Again, after a bit of studying there should be no problem
Here’s a glimpse of what I mean:
Of course, you can use a loop to add a number of images in succession
HTH
3rdbit spune:
15 decembrie 2010 la 17:02
I had a problem with the encoding of special chars, for example with the ä (Umlaut, used in german)
MS Office didn’t opened the affected documents, so i had to do some conversions. It don’t work with
your demo neither
// Convert possibly specialchars containing string to UTF‐8
$content =utf8_encode( $content )
// Inform the browser that it's UTF‐8 encoded content
header('Content‐Type: application/xml; charset=UTF‐8');
btw. for a better browser compatibility you should use the Content‐Type “application/xml” instead of
“text/xml”
http://www.grauw.nl/blog/entry/489
Alin spune:
1
2
3
4
5
6
7
8
//
<w:pict>
<w:binData w:name="wordml://<?php echo $file_name;?>" xml:space="preserve"><?
<v:shape id="img<?php echo $i.$j;?>" type="#_x0000_t75" style="width:<?php ec
<v:imagedata src="wordml://<?php echo $file_name;?>" o:title="application_vie
</v:shape>
</w:pict>
//
Trang 6Sorry for answering so late and thank you for pointing out this encoding issue. The demo was set up
only to show how the “trick” described above works and didn’t go all the way to handle encodings and whatnot
karthick spune:
24 mai 2011 la 09:17
Hi anyone can give me the whole structure of coding…. i am little bit confused how to start and where
to finish the coding…
Eduardo Ramos spune:
22 februarie 2012 la 10:51
have you tried phpdocx: http://www.phpdocx.com
Bloguiește pe WordPress.com. | Theme: Dusk To Dawn by Automattic
Follow
Powered by WordPress.com