Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

New outputfilter : pdml2pdf
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
Initiator
Smarty n00b


Joined: 10 Aug 2004
Posts: 3

PostPosted: Tue Aug 10, 2004 8:42 pm    Post subject: New outputfilter : pdml2pdf Reply with quote

I've made a simple outputfilter so you can use smarty and pdml to generate PDF documents.
Code:

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:    outputfilter
 * Name:    pdml2pdf
 * Version: 0.1
 * Date:    2004-08-10
 * Author:  Joost Smit, j.j.smit at student.utwente.nl
 * Purpose: Convert a smarty-powered pdml template to pdf.
 *          Functions as a wrapper for the pdml class.
 *          Further information on pdml:  http://pdml.sourceforge.net
 *       
 * Requires:pdml.php, fpdf.php, and font dir (all from pdml package)
 *          to be present in a subdir called pdml.
 *          The pdml dir has to be a subdir of the dir where
 *          your script is executed.
 *          I'm planning to make a more elegant solution for this in a later version.
 *
 * Note:    If you want to upgrade pdml for some reason,
 *          make sure to comment ob_start("ob_pdml"); out.
 *                      
 * Install: Drop into the plugin directory, call
 *          $smarty->load_filter('output','pdml2pdf');
 *          from application.
 *         
 * -------------------------------------------------------------
 */
 
 function smarty_outputfilter_pdml2pdf($tpl_output, &$smarty)
{   
    require("pdml/pdml.php");   
    $pdml = new PDML('P','pt','A4');
    $pdml->compress=0;
    $pdml->ParsePDML($tpl_output);
    $s = $pdml->Output("","S");
    Header('Content-Type: application/pdf');
    Header('Content-Length: '.strlen($s));
    Header('Content-disposition: inline; filename=doc.pdf');   
    return $s;
}
?>



You can test it with a template like this :
Code:
<pdml>
<body>
<font face="Arial" size="16pt">{$test}</font>
</body>
</pdml>


This seems to be working. All smarty functions you would use for a normal website should work. You can use the pdml 0.9alpha version which can be found on the pdml homepage
The only thing i've changed was commenting out "ob_start("ob_pdml");" at the end of pdml.php

I want to do the following things :
use a variable to set the dir pdml is located.
use a variable to set the name for the pdf file that's generated.
use a variable to set the parameters for pdml (like a4 / letter)

Are there any methods i can use to pass these things to the outputfilter?
Any suggestions are welcome Wink
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Aug 10, 2004 8:52 pm    Post subject: Reply with quote

Cool, but I'm not sure why you would want to do this as an output filter. Why not just create a normal PHP function that calls Smarty internally? Here's a simple example which is nearly the same as the output filter but instead uses an internal template fetch():

[php:1:1f65fb3403]<?php
function tpl2pdf($template, $filename)
{
require_once("Smarty.class.php");
$smarty = new Smarty; // do whatever setup you need or call your
// custom Smarty sub-class

$tpl_output = $smarty->fetch($template);

require_once("pdml/pdml.php");
$pdml = new PDML('P','pt','A4');
$pdml->compress=0;
$pdml->ParsePDML($tpl_output);
$s = $pdml->Output("","S");
Header('Content-Type: application/pdf');
Header('Content-Length: '.strlen($s));
Header('Content-disposition: inline; filename='.$filename);
echo $s;
}
?>[/php:1:1f65fb3403]
Back to top
View user's profile Send private message
Initiator
Smarty n00b


Joined: 10 Aug 2004
Posts: 3

PostPosted: Tue Aug 10, 2004 9:01 pm    Post subject: Reply with quote

hmm, that sounds like a better solution!
I wasn't quite sure if it had to be an outputfilter, so i'll do it your way ..thanks very much Smile
Back to top
View user's profile Send private message Visit poster's website
rd42
Smarty Rookie


Joined: 27 Aug 2004
Posts: 5

PostPosted: Sun Sep 05, 2004 2:52 pm    Post subject: help on pdml usage Reply with quote

How do I get the data that is suppose to go into the smarty template and the smarty in to the function?

I using Smarty inside of xoops, so I set up the arrays to send to smarty and then I call the function tpl2pdf.

tpl2pdf("templates/invoice_close.html, test.pdf");

all I'm getting is stuff like this:
Quote:
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Length 30>>
stream
2 J
0.57 w
BT /F1 10.00 Tf ET
[/quote]
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Mon Sep 06, 2004 6:47 am    Post subject: Re: help on pdml usage Reply with quote

rd42 wrote:
How do I get the data that is suppose to go into the smarty template and the smarty in to the function?

I using Smarty inside of xoops, so I set up the arrays to send to smarty and then I call the function tpl2pdf.

tpl2pdf("templates/invoice_close.html, test.pdf");

all I'm getting is stuff like this:
Quote:
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Length 30>>
stream
2 J
0.57 w
BT /F1 10.00 Tf ET
[/quote]

there`s a mistake in your function call:
[php:1:758a37c114]
tpl2pdf("templates/invoice_close.html", "test.pdf");
[/php:1:758a37c114]
Back to top
View user's profile Send private message
rd42
Smarty Rookie


Joined: 27 Aug 2004
Posts: 5

PostPosted: Mon Sep 06, 2004 1:58 pm    Post subject: Great Thanks Reply with quote

Well the fixing the typo, helped, thanks.

It doesn't seem to be going through my entire smarty template.

Few questions:

Is there any error reporting I can turn on, because now pdf is showing up?

How does the content make it into the function to make it the template?

Thanks again.
Back to top
View user's profile Send private message
Initiator
Smarty n00b


Joined: 10 Aug 2004
Posts: 3

PostPosted: Mon Sep 06, 2004 3:39 pm    Post subject: Reply with quote

You can use it at exactly the same way you would construct a normal webpage. I've taken the smarty setup out of the function, and construct the smarty object at the normal way.

Code:

<?php
function pdml2pdf($template, $filename)
{   
    global $smarty;
    $tpl_output = $smarty->fetch($template);
    require_once("pdml/pdml.php");   
    $pdml = new PDML('P','pt','A4');
    $pdml->compress=0;
    $pdml->ParsePDML($tpl_output);
    $s = $pdml->Output("","S");
    Header('Content-Type: application/pdf');
    Header('Content-Length: '.strlen($s));
    Header('Content-disposition: inline; filename='.$filename);
    echo $s;
}
$smarty = new Smarty;
// setup like normal


// assign variables
$smarty->assign("test", "Hello World!");
pdml2pdf("test.tpl","output.pdf");
?>


I think this is a little bit easier to use. The content is sent to the template at the way you would sent it to construct a webpage. The only difference is in the template (you'll need to use pdml instead of html) and you call the pdml2pdf function at the end of your file, instead of something like $smarty->display...
Back to top
View user's profile Send private message Visit poster's website
rd42
Smarty Rookie


Joined: 27 Aug 2004
Posts: 5

PostPosted: Mon Sep 06, 2004 6:04 pm    Post subject: So close.. Reply with quote

Thanks for all the responses. I'm missing something simple. When I run the script, a Internet Explorer shows a blank pdf. So I'm almost there:

PHP Script - pdftester.php:
Code:
<?php
function pdml2pdf($template, $filename)
{   
    global $smarty;
    $tpl_output = $smarty->fetch($template);
    require_once("../includes/pdml/pdml.php");   
    $pdml = new PDML('P','pt','A4');
    $pdml->compress=0;
    $pdml->ParsePDML($tpl_output);
    $s = $pdml->Output("","S");
    Header('Content-Type: application/pdf');
    Header('Content-Length: '.strlen($s));
    Header('Content-disposition: inline; filename='.$filename);
    echo $s;
}

require_once("../../../class/smarty/Smarty.class.php");
$smarty = new Smarty;   
// setup like normal


// assign variables
$smarty->assign("test", "Hello World!");
pdml2pdf("test.html","output.pdf");
?>


Template test.html:
Code:

<pdml>
<body>
<font face="Arial" size="16pt">{$test}<BR>I made it to the template</font>
</body>
</pdml>


Since I'm using the xoops smarty I have tried both {$test} and <{$test}>
with the same result. I also tried with $test all together and still a blank pdf, maybe my template or the xoops smarty ?

Thank you all again for all the responses.
Back to top
View user's profile Send private message
rd42
Smarty Rookie


Joined: 27 Aug 2004
Posts: 5

PostPosted: Mon Sep 06, 2004 8:48 pm    Post subject: A Clue Reply with quote

I added
Code:
$smarty->display ('test02.html');


to the end of the script and started getting an error message in the pdf.


Quote:

Warning: Smarty error: unable to read template resource: "test02.html" in ..../class/smarty/Smarty.class.php on line 1042.
Back to top
View user's profile Send private message
pt2002
Smarty Regular


Joined: 05 May 2003
Posts: 89
Location: Porto, Portugal

PostPosted: Wed Nov 17, 2004 3:31 pm    Post subject: Reply with quote

Hello

I'm trying to use smarty and pdml. I copy & paste the code above, made some changes (paths) but it doesn't work.
My php file to test is pdf1.php.
In IE it ask to download pdf1_php. In Firefox 1.0 it shows an empty pdf.
The code runs in a Linux FC2, apache 1.3.32, php 5.1.dev.

The code - pdf1.php

Code:

<?php

include '/www/lib/smarty/Smarty.class.php';

function pdml2pdf($template, $filename)
{
    global $smarty;
    $tpl_output = $smarty->fetch($template);
    require_once("/www/lib/pdml/pdml.php");
    $pdml = new PDML('P','pt','A4');
    $pdml->compress=0;
    $pdml->ParsePDML($tpl_output);
    $s = $pdml->Output("","S");
    Header('Content-Type: application/pdf');
    Header('Content-Length: '.strlen($s));
    Header('Content-disposition: inline; filename='.$filename);
    echo $s;
}
$smarty = & new Smarty;

$smarty->assign("test", "Hello World!");
pdml2pdf("pdf1.tpl", "test.pdf");

?>


The template pdf1.tpl
Code:

<pdml>
<body>
<font face="Arial" size="16pt">{$test}<BR>This is a test</font>
</body>
</pdml>


Greetings
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Nov 17, 2004 4:27 pm    Post subject: Reply with quote

Hmm. Try removing the Content-disposition line of the Header.
Back to top
View user's profile Send private message
pt2002
Smarty Regular


Joined: 05 May 2003
Posts: 89
Location: Porto, Portugal

PostPosted: Wed Nov 17, 2004 4:30 pm    Post subject: Reply with quote

Hi boots.

Still get an empty page in firefox and a popou to download or open pdf1_php in IE

greetings
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Nov 17, 2004 4:34 pm    Post subject: Reply with quote

hi pt2002 -- try this topic http://www.phpinsider.com/smarty-forum/viewtopic.php?t=1541

Intead of removing it, it may have had to be "Content-Disposition". I need coffee Smile
Back to top
View user's profile Send private message
pt2002
Smarty Regular


Joined: 05 May 2003
Posts: 89
Location: Porto, Portugal

PostPosted: Wed Nov 17, 2004 5:16 pm    Post subject: Reply with quote

Hi again Boots

I put the code in another machine. I will try to search for more info on Header()

You can see the code running

[url]
http://carbono.homedns.org/pdf/pdf1.php
[/url]

Thank you
pt2002
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Nov 17, 2004 5:19 pm    Post subject: Reply with quote

I take it you got it working? The link opens a pdf file (blank) for me in both IE and Firefox.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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