record as PDF

A place for users and developers of the Xataface to discuss and receive support.

Postby maddin » Tue Jun 26, 2007 5:02 pm

hi Steve
I am currently working on a way to print some cells of records neatly formatted as pdf.
I am doing this via the (open source)class fpdf. This is done by a php script which expects the id of the record as a post parameter.
So my plan was, to modify the main action.ini like this:

------------------snip----------------------
[print_this]
label = print
description = print this record
url = "{$dataface_url}pdf.php?-action=print_this&-table={$table}&-record={$record}"
accessKey = p
category = table_actions
icon = "{$dataface_url}/images/printer-orange.gif"
mode = print
;;template = HelloWorld.html
permission = view
order=5


-----------------snap-------------------------

but... action.ini seems not to know which record i mean ;)
it was just a try ,unfortunately without luck.
but maybe you have a better idea on how to implement this without modifing the dataface core files

cheers
martin
maddin
 
Posts: 58
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Tue Jun 26, 2007 5:33 pm

Hi Martin,

Are you trying to point to a dataface action that you defined (e.g. a script named print_this.php inside the actions directory), or a separate script that is outside of dataface?) Both are possible, but probably better to do it inside Dataface (first option).

I'll assume you're doing the first option. Let me know if you're not.

You're very close. The best strategy here is to use Dataface's notion of context to know which record you mean. In the url field in the actions.ini file you can access the application object using $this. The Application class has a url() method that will generate a url but maintain query parameters.

e.g.
url = "{$this->url('-action=print_this')}"

If you needed to append other parameters you could do either:
url = "{$this->url('-action=print_this&-myparam=myvalue')}"
or
url = "{$this->url('-action=print_this')}&-myparam=myvalue"

There would be very little difference.

You'll notice that the URL will maintain the query parameters and table information for the link in your action.

Now, inside your action you can access everything you need from the application object:
e.g.
$app =& Dataface_Application::getInstance();
$record =& $app->getRecord(); // Dataface_Record object -- the current record
$query =& $app->getQuery(); // Associative array of query parameters (e.g. array('-table'=>'foo', '-action'=>'print_this',...)

Now you do have to be a little careful when you start to work with the $record object because this action might be used by more than one table, and they may have different columns.

There are 2 ways to ensure that an action only appears in one table:
1. Place the action in an actions.ini file inside that table's folder. e.g. tables/foo/actions.ini
2. Use the condition parameter:
condition = "$query['-table'] == 'foo'"

Alternatively, in modifying what you already have: Your main error is that the $record object is an object and not an id. If you want the id, you would need to do something like {$record->val('id')} (if that record has a field named 'id').

Hope this helps a little.
-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby maddin » Wed Jun 27, 2007 4:57 am

hi steve
thanks for your help
Version 2 (external file) works already so my problem is already solved.
but ... this version has a little flaw , as you already mentioned, this action can be proceeded from every table.

to do it the proper way from within Dataface I now try to build version one.

Here is what I have done so far: (extended version, because this might be interresting for others too)

1.the class fpdf.php lives in the main dataface folder

for those who are interrested,get it here: http://www.fpdf.org/

2.next I put a folder with font files in the main directory as well

3. I created a file named print_this_action.php and saved this in the main actions folder

it loks like this:

----------------------------snip----------------------------------------
Image('../images/logo_header.png',2,1,0);
//Arial bold 15
$this->SetFont('Arial','B',15);
$this->Ln(20);
$this->Ln(20);

//to the right
$this->Cell(-135);
//Title


$this->Cell(0,10,'the TITLE',0,0,'C');
$this->SetX(45);
//pagebreak

$this->Ln(20);

}

//Footer
function Footer()
{
//Position 1,5 cm from bottom
$this->SetY(-15);
//Arial kursiv 8
$this->SetFont('Arial','I',8);
//page number
$this->Cell(0,10,'Seite '.$this->PageNo().'/{nb}',0,0,'C');
}
}
$app =& Dataface_Application::getInstance();
$record =& $app->getRecord();
$query =& $app->getQuery();
//----------------------------------------?????-----did I call the params correctly????--------
$sql = "SELECT
druck
FROM
". $query=>('-table') ."
WHERE
id LIKE " . $query=>('-record'). ";";

$result = mysql_query($sql) OR die(mysql_error());
$pdftext = mysql_fetch_assoc($result);
$pdfoutput = $pdftext['druck'];

$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('arial','',10);
$pdf->SetLeftMargin(15);
$pdf->MultiCell(0,5,$pdfoutput,0,1);

$pdf->Output();
}
?>
---------------------------snap-----------------------------------------

then I created the print action and put it in the table folder (neu_fragen)

---------------------------snip-----------------------------------------
[print_this_action]
label = print
description = print this record

url = "{$this->url('-action=print_this_action')}&-table={$table}&-record={$record->val('id')"
accessKey = p
category = table_actions
icon = "{$dataface_url}/images/printer-orange.gif"
mode = print
;;template = HelloWorld.html<--------------------------do I need a template for this action???
permission = view
order=5
condition = "$query['-table'] == 'neu_fragen'"
---------------------------snap-----------------------------------------

unfortunately this produces the following error:

Fatal error: Fehler bei der Analyse des Ausdrucks '{$this->url('-action=print_this_action')}&-table={$table}&-record={$record->val('id')'.On line 1110 of file /data/my_server/my_url/www/html/dataface-0.7.1/Dataface/Application.php in function printStackTrace()
On line 234 of file /data/my_server/my_url/www/html/dataface-0.7.1/Dataface/ActionTool.php in function parseString({$this->url('-action=print_this_action')}&-table={$table}&-record={$record->val('id'),array(neu_fragen))
On line 1583 of file /data/my_server/my_url/www/html/dataface-0.7.1/Dataface/Table.php in function getActions(array(neu_fragen))
On line 79 of file /data/my_server/my_url/www/html/dataface-0.7.1/Dataface/ActionTool.php in function getActions(array(neu_fragen))
On line 92 of file /data/my_server/my_url/www/html/dataface-0.7.1/Dataface/ActionTool.php in function _loadTableActions(neu_fragen)
On line 776 of file /data/my_server/my_url/www/html/dataface-0.7.1/Dataface/Applica in /data/my_server/my_url/www/html/dataface-0.7.1/Dataface/Application.php on line 1110

----------------------------------------end error--------------------------------

so .. here i got stuck
maddin
 
Posts: 58
Joined: Wed Dec 31, 1969 5:00 pm

Postby maddin » Wed Jun 27, 2007 5:04 am

mmhh..
this part of the code has been removed by the forum software:

place this on top of the print_this_action.php

if($_REQUEST['-action']=="print_this")
//$table = $_REQUEST['-table'];
//$rec = $_REQUEST['-record'];


{
define('FPDF_FONTPATH','../font/');
require('../fpdf.php');
class PDF extends FPDF
{
//pdf header
function Header()
{
//Logo


--------------------------end code----------------

cheers
martin
maddin
 
Posts: 58
Joined: Wed Dec 31, 1969 5:00 pm

Postby shannah » Wed Jun 27, 2007 11:23 pm

The fatal error is likely due to the missing '}' at the end of the url.

url = "{$this->url('-action=print_this_action')}&-table={$table}&-record={$record->val('id')"

should be

url = "{$this->url('-action=print_this_action')}&-table={$table}&-record={$record->val('id')}"

however, the extra -table will be redundant because this has been filled in by $this->url('...')

so you could do

url = "{$this->url('-action=print_this_action')}&-record={$record->val('id')}"

However, refining this further, you could just have:

url = "{$this->url('-action=print_this_action')}"

And then in your action you would have:

$app =& Dataface_Application::getInstance();
$record =& $app->getRecord();
$pdfoutput = $record->val('druck');

(Notice that we removed all the explicit SQL stuff. Dataface took care of loading the record given the query parameters.)

And further... it looks like, you are sort of creating a "half" action here. Actions should be placed in a class using the proper naming convention, and all of the logic that is to be performed by the action should be placed inside the handle() method.

If you place the action in the dataface actions directory then the class name should be:
dataface_actions_%action_name%
in this case
dataface_actions_print_this_action

If you place it inside the application's actions directory, the action would be called:
actions_print_this_action

So you would have in actions/print_this_action.php
import('path/to/PDF.php'); // Place your custom PDF class in another file and import it here
class actions_print_this_action {
function handle(&$params){
$app =& Dataface_Application::getInstance();
record =& $app->getRecord();

$pdfoutput = $record->val('druck');
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('arial','',10);
$pdf->SetLeftMargin(15);
$pdf->MultiCell(0,5,$pdfoutput,0,1);
$pdf->Output();

}
}

Then you don't need to deal with:
if($_REQUEST['-action']=="print_this")
...

because Dataface handles this. You don't have to check for the action at all because Dataface will only call this action if this action is requested and the user has permission.

Finally, it is apparent that this action is only meant to work on one table. The
condition = "$query['-table'] == 'neu_fragen'"
line of the actions.ini file is sufficient for making this action only available for that table.

So your actions.ini file entry would be like:
[print_this_action]
label = print
description = print this record
url = "{$this->url('-action=print_this_action')}"
accessKey = p
category = table_actions
icon = "{$dataface_url}/images/printer-orange.gif"
mode = print
permission = view
order=5
condition = "$query['-table'] == 'neu_fragen'"

Now the table_actions category shows up as a tab. This may be misleading since it will result in a file download. A more appropriate category might be record_actions, which will make it appear as a link below the record tabs.

Hope this helps.

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby maddin » Thu Jun 28, 2007 7:20 am

hi Steve
oh wow.. now i have the perfect application
i followed your suggestions step by step and my pdf's now are generated the proper way..
sincere thanks for your night shift (saw the time of your posting)

I assume like most programmers you dont drink alcohol, so I send you this alcohol free virtual beer from Berlin
---------------

|__|
|% |]
|__|
---------------
cheers
Martin
maddin
 
Posts: 58
Joined: Wed Dec 31, 1969 5:00 pm

Print to PDF

Postby sophistry » Fri Apr 03, 2009 4:33 pm

Hi Martin or Steve,
I used this post to set up a print function, and the setup went great. Clicking the Print button properly uses the action and creates a PDF ... the only problem is that I'm trying to get it to work with printing a list table, instead of a single record (Hopefully both if possible). Either way, what comes up is my nicely formatted, logo'd PDF with the page number at the bottom, but no data.

For the life of me, I can't figure out how to format the $pdfoutput in print_this_action.php ... I can't figure out what Martin's "$pdfoutput = $record->val('druck'); " means for my database. Druck apparently means 'print', but I don't have that anywhere in my files, and of course I have no 'druck' or 'print' in my table...

Any insight into what's going wrong would be appreciated. Here are my following files...

print_this_action.php:
Code: Select all
<?php
define('FPDF_FONTPATH','actions/font/');
require('actions/fpdf.php');
import('actions/pdf.php');
class dataface_actions_print_this_action {
   function handle(&$params){
      $app =& Dataface_Application::getInstance();
      $query = $app->getQuery();
      $query['-limit'] = 9999999;
      $table =& Dataface_Table::loadTable($query['-table']);
      $record =& $app->getRecord();

      $pdfoutput = $record->val('print');
      $pdf=new PDF();
      $pdf->AliasNbPages();
      $pdf->AddPage();
      $pdf->SetFont('arial','',10);
      $pdf->SetLeftMargin(15);
      $pdf->MultiCell(0,5,$pdfoutput,0,1);
      $pdf->Output();

   }
}
?>


Everything looks great to me, just the $pdfoutput line - I've tried a lot of different things to get it to just spit out the current table list.

Entry in actions.ini:
Code: Select all
[print_this]
   label = print
   description = Print This Report
   url = "{$this->url('-action=print_this_action')}"
   accessKey = "p"
   category = table_actions
   icon = "{$dataface_url}/images/print.gif"
   mode = print
   permission = view
   order=5
   condition = "$query['-table'] == 'Proposal__ClientSubmit'"


And, I have a seperate pdf.php to define the header and footer of the PDF, as was mentioned above. That part works great!

Thanks for taking a look.

Rory
sophistry
 
Posts: 27
Joined: Mon May 19, 2008 11:20 am

Postby Martin Pruss » Mon Apr 06, 2009 3:46 am

Hi Rory

Code: Select all
$pdfoutput = $record->val('druck');

was in my case a field in my Record callded "druck".
It was a blob field with lots of Text in it.

if you change this just for testing to a field name of your record, it should output that field in your pdf.

on: http://www.fpdf.org/ (tutorials) is a tutorial on how to output a table
nicely formatted

cheers
martin
Martin Pruss
 
Posts: 61
Joined: Tue Oct 23, 2007 2:22 pm
Location: Berlin

Postby sophistry » Mon Apr 06, 2009 1:59 pm

Hey Martin,
I'm now able to get my PDF's coming out with data (Thanks!), I took some tips from a few different posted scripts. However, I'm still having a big problem getting long strings (text) to wrap properly when posting up to the PDF ... the text runs under the next cell and off the page.

I've been trying to add a posted WordWrap script, but the text just doesn't want to wrap around. Did you have to deal with this at all in your PDF output?

Thanks,
Rory
sophistry
 
Posts: 27
Joined: Mon May 19, 2008 11:20 am

Postby Martin Pruss » Tue Apr 07, 2009 12:11 am

Hi Rory
In my case everything was rendered fine ...
no matter on how much text was in the blob.

I sent you a download link of my files via pm ...to be placed in the dataface directory or you have to edit the classpaths..
I can't really support you with these files because i have almost forgotten everything related to "fpdf"
cheers
martin
Martin Pruss
 
Posts: 61
Joined: Tue Oct 23, 2007 2:22 pm
Location: Berlin

Postby sophistry » Tue Apr 07, 2009 5:24 am

Thanks so much Martin!

I started playing around with MultiCell and different scripts, but couldn't quite get the wordwrap and cell height to work. I figure it has something to do with how it's calculating the field height, but I'll take a look at your scripts and see what might be amiss.

Thanks again, I really appreciate it!

Rory
sophistry
 
Posts: 27
Joined: Mon May 19, 2008 11:20 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 4 guests

Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved