View previous topic :: View next topic |
Author |
Message |
maw Smarty Rookie
Joined: 18 Apr 2003 Posts: 8 Location: Wellington, New Zealand
|
Posted: Thu Feb 26, 2004 11:39 pm Post subject: Smarty to create MS Word documents. |
|
|
The idea came from a MS KB article (sorry - lost the reference) for using ASP to generate Word documents.
This is for my intranet where some documents are generated from information in our database. The templates are easy to create/edit without using any specialist software or skills. complex formatting and images can be included, the only trick is to make sure that formatting changes don't happen inside of the smarty tags, if formatting changes in a conditional section the results are a bit unpredictable.
The process:
Create a template in Word, save as .rtf
could look like:
Quote: | {$page.title|upper}
This page generated on {$smarty.now|date_format:%d.%m.%Y} |
Use code similar to the following snip to display the template:
[php:1:312c61001e]<?php
// create the smart object and assign values
// then to display ...
if ($type=="rtf") {
header("Content-type: application/msword");
header("Content-Disposition: attachment; filename={$template}.doc");
$smarty->left_delimiter = "\{";
$smarty->right_delimiter = "\}";
$smarty->display("{$template}.rtf");
}
?>[/php:1:312c61001e]
Note that $template and $type are returned from a form |
|
Back to top |
|
terryjohnson Smarty Rookie
Joined: 03 Jan 2004 Posts: 9 Location: Chatham, ON, Canada
|
Posted: Sun May 16, 2004 1:37 am Post subject: |
|
|
This is a great trick.
Does anyone know whether it can be made to work for PDFs? |
|
Back to top |
|
jholland Smarty Rookie
Joined: 28 Sep 2003 Posts: 6
|
Posted: Fri May 28, 2004 4:47 pm Post subject: |
|
|
How do you get around the fact that rtf also uses braces "{}" as delimiters, so smarty and rtf delimiters conflict??
Could you post some sample rtf code?
Also, how do you use php to embed images? I've tried to do this myself, and never could get the format correct. LInking is supported, but embedding is more portable.
Thanks |
|
Back to top |
|
terryjohnson Smarty Rookie
Joined: 03 Jan 2004 Posts: 9 Location: Chatham, ON, Canada
|
Posted: Fri May 28, 2004 7:37 pm Post subject: |
|
|
The code sample in the first article sets the Smarty delimilters to escaped curly braces '\{' and '\}', which is what { and } become when you save the document as RTF.
I think adding an image to an RTF file might be pushing the system too far, but good luck! For grahics-intensice documents, there's always PHP's PDF creation functions if RTF doesn't work out. |
|
Back to top |
|
maw Smarty Rookie
Joined: 18 Apr 2003 Posts: 8 Location: Wellington, New Zealand
|
Posted: Tue Jun 01, 2004 11:32 am Post subject: In response to the above ... |
|
|
The .rtf files are created using Word. We use an image in both the header and the footer no problem, however the image is fixed (static), if you wanted to insert a dynamically created image that would involve some trickyness - however an image can be surrounded by {if} {else} clauses. My aim was to avoid having to hack the rtf so that templates could be created using almost any word processor by almost anyone. This seemed like an elegant solution to that problem.
As for PDF, I also intend to extend the system to enable pdf creation, there is a project on sourceforge for a MS Word/Excel->PDF converter which just uses the Microsoft free Viewers and a pdf printer. I'm thinking of using a similar approach although the ultimate would be to get into some serious programming and use the Open Office API as that can read rtf and output pdf and is cross platform. |
|
Back to top |
|
mpilar Smarty n00b
Joined: 01 Feb 2005 Posts: 3 Location: San José, Costa Rica
|
Posted: Tue Feb 01, 2005 4:12 pm Post subject: |
|
|
Hi! I've been needing to generate MS Word documents based on data from MySQL database, and this seems a great way to do it, if I can get it to work!
I have had Smarty installed for a while, although I've never tried to display anything other than a .tpl file.
I followed the instructions and have so far tried with three different .rtf files, the last of which had only text on it, to see if it would work.
I'm no longer getting errors, but I have a question: where will the generated document be displayed? I ask because the only thing I get is the raw rtf code in an IE window, no MS Word pluggin or anything, so the document is not even readable.
Any suggestions?
Thanks! |
|
Back to top |
|
maw Smarty Rookie
Joined: 18 Apr 2003 Posts: 8 Location: Wellington, New Zealand
|
Posted: Tue Feb 08, 2005 7:42 am Post subject: |
|
|
It is very important that the first output from your php script is the headers as in the sample above. If there is even a single space or return before the opening <?php of your script the headers that will be sent will be the default which tell the browser to expect text or html. Alternately you can use $smarty->fetch() to generate the rtf file and then save it onto disk somewhere, this is useful for debugging. I have used this feature to run a mail merge where we wanted to use scanned signatures, since you can't insert images into a Word mail merge and you can't conditionally include images in Word. Worked a treat. |
|
Back to top |
|
mpilar Smarty n00b
Joined: 01 Feb 2005 Posts: 3 Location: San José, Costa Rica
|
Posted: Tue Feb 08, 2005 6:58 pm Post subject: |
|
|
Thanks for the help! However, I still have the problem. This is the code I'm using:
[php:1:4975ffd1e0]<?php
session_start();
include "inc/connect.inc";
include "inc/funcs.inc";
require('Smarty.class.php');
$smarty = new Smarty;
$smarty->template_dir = templates;
$file = 'test';
$refnum = 'CU-D-05-01-35';
$date = date('d/m/Y');
$smarty->assign('refnum', $refnum);
$smarty->assign('date', $date);
$smarty->assign('user', 'mpilar');
$smarty->assign('name', 'Norberto Rivera Romero');
$smarty->assign('post', 'Chief');
$smarty->assign('dept', 'Council');
$smarty->left_delimiter = "\{";
$smarty->right_delimiter = "\}";
// Set 1
//header("Content-type: application/msword");
//header('Content-Disposition: attachment; filename="test.doc"');
//$smarty->display("docs/{$file}.rtf");
// Set 2
//header("Content-type: application/msword");
//header("Content-Disposition: attachment; filename=test.doc");
//$doc = $smarty->fetch("docs/{$file}.rtf");
//echo $doc;
?>[/php:1:4975ffd1e0]
I have tried both Set 1 and Set 2 (separately of course! ) and they both seem to give the correct response, but both show up on the browser instead of either opening Word or giving the option to save.
I know the process works because in a different script I have the following:
[php:1:4975ffd1e0]header('Content-type: application/octetstream');
header('Content-Length: $len');
header('Content-Disposition: attachment; filename="counters.csv"');
echo $output;[/php:1:4975ffd1e0]
and it works perfectly, so I'm at a loss as to why I can't get it to work properly.
At one point I added the 'Content-Length' header after fetching the rtf. In that case, I get a blank screen in the browser... and nothing else!!!
Any suggestions? And thanks again! |
|
Back to top |
|
maw Smarty Rookie
Joined: 18 Apr 2003 Posts: 8 Location: Wellington, New Zealand
|
Posted: Thu Feb 10, 2005 8:52 am Post subject: |
|
|
Does the session_start() command issue headers? You could use something like the LiveHTTPheaders extension for Firefox to check that you are sending the correct headers. |
|
Back to top |
|
mpilar Smarty n00b
Joined: 01 Feb 2005 Posts: 3 Location: San José, Costa Rica
|
Posted: Thu Feb 10, 2005 2:22 pm Post subject: |
|
|
It doesn't issue any headers. The other piece of code I mentioned, the one that's allowing me to export data to Excel, has the very same lines at the beginning. The only difference between the two is the data I'm trying to display and the type of document... |
|
Back to top |
|
jaey Smarty Regular
Joined: 25 May 2004 Posts: 36 Location: Louisville, Kentucky, USA
|
|
Back to top |
|
|