Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Smarty to create MS Word documents.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
maw
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Wellington, New Zealand

PostPosted: Thu Feb 26, 2004 11:39 pm    Post subject: Smarty to create MS Word documents. Reply with quote

The idea came from a MS KB article (sorry - lost the reference) for using ASP Wink 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
View user's profile Send private message
terryjohnson
Smarty Rookie


Joined: 03 Jan 2004
Posts: 9
Location: Chatham, ON, Canada

PostPosted: Sun May 16, 2004 1:37 am    Post subject: Reply with quote

Very Happy This is a great trick.

Does anyone know whether it can be made to work for PDFs?
Back to top
View user's profile Send private message Visit poster's website
jholland
Smarty Rookie


Joined: 28 Sep 2003
Posts: 6

PostPosted: Fri May 28, 2004 4:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
terryjohnson
Smarty Rookie


Joined: 03 Jan 2004
Posts: 9
Location: Chatham, ON, Canada

PostPosted: Fri May 28, 2004 7:37 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
maw
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Wellington, New Zealand

PostPosted: Tue Jun 01, 2004 11:32 am    Post subject: In response to the above ... Reply with quote

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
View user's profile Send private message
mpilar
Smarty n00b


Joined: 01 Feb 2005
Posts: 3
Location: San José, Costa Rica

PostPosted: Tue Feb 01, 2005 4:12 pm    Post subject: Reply with quote

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! Very Happy

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. Crying or Very sad

Any suggestions?

Thanks!
Back to top
View user's profile Send private message
maw
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Wellington, New Zealand

PostPosted: Tue Feb 08, 2005 7:42 am    Post subject: Reply with quote

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
View user's profile Send private message
mpilar
Smarty n00b


Joined: 01 Feb 2005
Posts: 3
Location: San José, Costa Rica

PostPosted: Tue Feb 08, 2005 6:58 pm    Post subject: Reply with quote

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! Smile ) 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!!! Sad

Any suggestions? And thanks again!
Back to top
View user's profile Send private message
maw
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Wellington, New Zealand

PostPosted: Thu Feb 10, 2005 8:52 am    Post subject: Reply with quote

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
View user's profile Send private message
mpilar
Smarty n00b


Joined: 01 Feb 2005
Posts: 3
Location: San José, Costa Rica

PostPosted: Thu Feb 10, 2005 2:22 pm    Post subject: Reply with quote

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
View user's profile Send private message
jaey
Smarty Regular


Joined: 25 May 2004
Posts: 36
Location: Louisville, Kentucky, USA

PostPosted: Thu Feb 10, 2005 11:58 pm    Post subject: Reply with quote

Reading through the comments of the following PHP documentation pages might provide some helpful information to resolve your issue:

http://www.php.net/function.readfile
http://www.php.net/manual/en/function.session-cache-limiter.php
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP