View previous topic :: View next topic |
Author |
Message |
Initiator Smarty n00b
Joined: 10 Aug 2004 Posts: 3
|
Posted: Tue Aug 10, 2004 8:42 pm Post subject: New outputfilter : pdml2pdf |
|
|
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 |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Aug 10, 2004 8:52 pm Post subject: |
|
|
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 |
|
Initiator Smarty n00b
Joined: 10 Aug 2004 Posts: 3
|
Posted: Tue Aug 10, 2004 9:01 pm Post subject: |
|
|
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 |
|
Back to top |
|
rd42 Smarty Rookie
Joined: 27 Aug 2004 Posts: 5
|
Posted: Sun Sep 05, 2004 2:52 pm Post subject: help on pdml usage |
|
|
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 |
|
kills Smarty Elite
Joined: 28 May 2004 Posts: 493
|
Posted: Mon Sep 06, 2004 6:47 am Post subject: Re: help on pdml usage |
|
|
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 |
|
rd42 Smarty Rookie
Joined: 27 Aug 2004 Posts: 5
|
Posted: Mon Sep 06, 2004 1:58 pm Post subject: Great Thanks |
|
|
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 |
|
Initiator Smarty n00b
Joined: 10 Aug 2004 Posts: 3
|
Posted: Mon Sep 06, 2004 3:39 pm Post subject: |
|
|
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 |
|
rd42 Smarty Rookie
Joined: 27 Aug 2004 Posts: 5
|
Posted: Mon Sep 06, 2004 6:04 pm Post subject: So close.. |
|
|
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 |
|
rd42 Smarty Rookie
Joined: 27 Aug 2004 Posts: 5
|
Posted: Mon Sep 06, 2004 8:48 pm Post subject: A Clue |
|
|
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 |
|
pt2002 Smarty Regular
Joined: 05 May 2003 Posts: 89 Location: Porto, Portugal
|
Posted: Wed Nov 17, 2004 3:31 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 17, 2004 4:27 pm Post subject: |
|
|
Hmm. Try removing the Content-disposition line of the Header. |
|
Back to top |
|
pt2002 Smarty Regular
Joined: 05 May 2003 Posts: 89 Location: Porto, Portugal
|
Posted: Wed Nov 17, 2004 4:30 pm Post subject: |
|
|
Hi boots.
Still get an empty page in firefox and a popou to download or open pdf1_php in IE
greetings |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
pt2002 Smarty Regular
Joined: 05 May 2003 Posts: 89 Location: Porto, Portugal
|
Posted: Wed Nov 17, 2004 5:16 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 17, 2004 5:19 pm Post subject: |
|
|
I take it you got it working? The link opens a pdf file (blank) for me in both IE and Firefox. |
|
Back to top |
|
|