Custom action to export current record data to PDF

878 views
Skip to first unread message

R.C

unread,
Nov 21, 2014, 6:49:59 PM11/21/14
to xata...@googlegroups.com
Hello,
I am a beginner in xataface, love it so far !

I'm trying to make a custom action wich exports current record's data (field values) into a PDF. A sort of invoice. The action is 

I made the action's php file and I get the desired PDF via mPDF but now I want to know how to get the current record's field values into php variables so I can echo them in my HTML wich is used by mPDF. 

My fields are ( user, address, product, price ). I would like to store them in variables like $user, $address, etc.

Here is a snippet of my action ( actions/export_record_pdf.php)

<?php
class dataface_actions_export_pdf {
    function handle(&$params){



        $html = '
<p>User</p>

<span> '.$user.' </span> 

<p>Address</p>

<span> '.$address.' </span> 

<p>Product</p>

<span> '.$product.' </span> 

';
//==============================================================
//==============================================================
//==============================================================
include("mpdf/mpdf.php");
$mpdf=new mPDF('c');
$mpdf->SetDisplayMode('fullpage');
// LOAD a stylesheet

$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
    }
}
?>

Here is my action data in actions.ini

[export_pdf]
label = To PDF
description = "Export in PDF"
category=record_export_actions
mode=list
url = "{$this->url('-action=export_pdf')}"



Thanks !

Steve Hannah

unread,
Nov 21, 2014, 7:13:54 PM11/21/14
to xata...@googlegroups.com
$app = Dataface_Application::getInstance();  // The main application object
$record = $app->getRecord();  // The current record (as specified by the URL for the request).

Access fields using $record->val('fieldname'), $record->strval('fieldname'), $record->display('fieldname'), or $record->htmlValue('fieldname')

On this topic, you might also be interested in checking out the Xataface FDF module that I recently released.

I created a tutorial showing you how to create a PDF form using Open Office that can be automatically filled by Xataface.

Steve

--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at http://groups.google.com/group/xataface.
To view this discussion on the web visit https://groups.google.com/d/msgid/xataface/4f8aed4f-4abd-438c-8673-b07c3d49422f%40googlegroups.com.



--
Steve Hannah
Web Lite Solutions Corp.

R.C

unread,
Nov 22, 2014, 8:22:21 AM11/22/14
to xata...@googlegroups.com
That is exactly what I was looking for ! Awsome ! I'm going to go your way and design the forms in Libre Office myself.

Thank You !

R.C

unread,
Nov 22, 2014, 9:57:35 AM11/22/14
to xata...@googlegroups.com
I'm on shared hosting and it seems that I can't get pdftk installed. Is there another way ?


On Saturday, November 22, 2014 1:49:59 AM UTC+2, R.C wrote:

Steve Hannah

unread,
Nov 22, 2014, 10:10:42 AM11/22/14
to xata...@googlegroups.com

Currently this module requires pdftk.  There are pure php solutions for creating fdf files though.  See this comment for an example
http://php.net/manual/en/ref.fdf.php#46624

--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at http://groups.google.com/group/xataface.

R.C

unread,
Nov 22, 2014, 8:04:22 PM11/22/14
to xata...@googlegroups.com
Ok, I managed to get it working...as a beginner :).

I made use of fpdf, fpdm, a pure PHP fdf generator. 
I generate the FDF and store it in a temporary file then feed that file to fpdm. 

I now have to figure out how to get my record's values and put them in my array of fields > values then feed them to ppdm

Here is the script for anyone who want to use it. 
NOTICE I'm a beginner in this, any suggestion is well-welcomed !

Also made a few comments for beginners like me

<?php

/***************************
  Generate PDFs filled with our data of choice using fpdf and fpdm only, without the need of 
  pdftk, excellent for those on shared hosting. I create my PDF forms in OpenOffice
  WARNING - LibreOffice created forms doesn't seem to work, I don't know why yet.
  Generate your PDF form in OpenOffice writer and give them names like name, phone, email, etc. Go to File
  menu and select export to PDF, choose FDF type under Create PDF form and export it as PDF. Upload it to 
  your template folder, mine is fdf_templates.pdf.
  Roberto-Cristian
****************************/

// the path to the PDF template form, relative to this script's location
$file = 'fdf_templates/template.pdf';


// Fill in text fields using an array (name_of_field => value we want filled with)
$fields = array(
    'serial' => 'EIT1',
    'date' => '21.11.2014',
    'customer' => 'COMPANY Y SPA',
    'vatno' => '01555685',
    'id' => '3056652',
    'address' => 'My city, my state, my province',
    'phone' => '099 865 2221',
    'product' => 'LED Benq WHLD22 / WHL22DL / 36 months',
    'invoice' => 'EIT 1234'

 
  
    );

// Let's generate an FDF output and store it in a temporary file
// this function takes in our PDF form as $pdf_file and the field values as $pdf_data
function output_fdf ($pdf_file, $pdf_data) {
    
    $fdf = "%FDF-1.2\n%âãÏÓ\n";
    $fdf .= "1 0 obj \n<< /FDF ";
    $fdf .= "<< /Fields [\n"; 
    
    foreach ($pdf_data as $key => $val)
        $fdf .= "<< /T ($key) /V ($val) >> \n";
        
    $fdf .= "]\n/F ($pdf_file) >>";
    $fdf .= ">>\nendobj\ntrailer\n<<\n";
    $fdf .= "/Root 1 0 R \n\n>>\n";
    $fdf .= "%%EOF";

    

    // we store the fdf output in a temporary file
    $tempfile = 'tempfdf.fdf';
    file_put_contents($tempfile, $fdf);
    
}

// Let's run the above function to generate our FDF file, it takes in our PDF form as $file and 
// fields > values as $fields
output_fdf($file, $fields);


// Now, we call fpdm to create fill the PDF form template with our data from the temporary FDF file. 
//It should open in the browser as a filled PDF
require('fpdf/fpdm.php');

$pdf = new FPDM('fdf_templates/template.pdf', 'tempfdf.fdf');
$pdf->Merge();
$pdf->Output();
?>

R.C

unread,
Nov 22, 2014, 9:15:03 PM11/22/14
to xata...@googlegroups.com
Having problems !

If I access the script directly as a URL it works perfectly, but when I run it as an action within xataface something breaks fpdm. I get

Warning: implode() [function.implode]: Invalid arguments passed in /home/www/xataface/fpdf/fpdm.php on line 1377
FPDF-Merge Error: getFilter cannot open stream of object because filter '' is not supported, sorry.

In actions.ini I have

[export_pdf]
label = To PDF
description = "Export in PDF"
category=record_export_actions
mode=list
url = "{$this->url('-action=export_pdf')}"

I tried full absolute paths because I thought it could be something related to paths but it doesn't work.

I didn't set up variables from records yet, my script is exactly like the one above, it should work but when ran inside my app it crashes.


On Saturday, November 22, 2014 1:49:59 AM UTC+2, R.C wrote:

R.C

unread,
Nov 25, 2014, 2:56:22 PM11/25/14
to xata...@googlegroups.com
Could I somehow get the records without running it as action ? I can't seem to get around the above error. The only way it works is by direct link.

Steve Hannah

unread,
Nov 26, 2014, 11:10:46 AM11/26/14
to xata...@googlegroups.com
While the answer is "Yes"... that would be harder.   If you can't get it to run inside an action then you're just making a simple mistake.  Can you post the code to your action - I might be able to point you in the right direction.

Steve

--
You received this message because you are subscribed to the Google Groups "Xataface" group.
Visit this group at http://groups.google.com/group/xataface.

R.C

unread,
Nov 26, 2014, 11:56:05 AM11/26/14
to xata...@googlegroups.com
Here is my configuration

For start, I don't retrieve record values from database, I wrote them manually under $fields array.

in my actions folder I created "export_pdf.php" and in it 
<?php
class dataface_actions_export_pdf {
    function handle(&$params){

/***************************
  Generate PDFs filled with our data of choice using fpdf and fpdm only, without the need of 
  pdftk, excellent for those on shared hosting. I create my PDF forms in OpenOffice
  WARNING - LibreOffice created forms doesn't seem to work, I don't know why yet.
  Generate your PDF form in OpenOffice writer and give them names like name, phone, email, etc. Go to File
  menu and select export to PDF, choose FDF type under Create PDF form and export it as PDF. Upload it to 
  your template folder, mine is fdf_templates.pdf.
  

In my actions.ini file I added

[export_pdf]
label = To PDF
description = "Export in PDF"
category=record_export_actions
mode=list
url = "{$this->url('-action=export_pdf')}"


If I run the script in a file test.php inside my root folder it works perfectly, but not when ran as an action so I thought it is something related to paths. This is my tree:

xataface/
 -actions folder/export_pdf.php
 -actions.ini file
 -fpdf folder
 -fdf_templates
 -test.php

I modified the lines in xataface/actions/export_pdf.php with absolute paths but I didn't manage to get it to work. 
The error is 

Warning: implode() [function.implode]: Invalid arguments passed in /home/www/xataface/fpdf/fpdm.php on line 1377
FPDF-Merge Error: getFilter cannot open stream of object because filter '' is not supported, sorry.



Steve Hannah

unread,
Nov 26, 2014, 1:15:03 PM11/26/14
to xata...@googlegroups.com
A couple of things to be aware of here:

1. You have defined the output_fdf() function inside the handle() method.   This won't have the effect you desired.  This should be defined either inside the class (but not another method), or outside the class.  If inside the class, you will call this method with $this->output_fdf(...).  If outside the class, you can still call it the same way: output_fdf(...).

2.  Note that relative file paths will be resolved from the index.php file location, and not the action's php file itself.  Keep this in mind.

Steve

R.C

unread,
Nov 26, 2014, 3:46:57 PM11/26/14
to xata...@googlegroups.com
Hmm,
I tried with output_fdf() function inside and outside the class, same error.

I opened up fpdm.php and went to the block of code where error happens.

it looks like
/**
*Retrieve the list of supported filters
*
*@note Uses $FPDM_FILTERS array built dynamically 
*@param String $sep a separator to merge filter names, default is '|'
*@return String the suported filters
**/
function getFilters($sep="|") {
//---------------------
global $FPDM_FILTERS;
return implode($sep,$FPDM_FILTERS);
}
Is there a way to display these "invalid arguments" and see what exactly is happening when ran as an action, and why it works perfectly fine when ran directly by url ?

Steve Hannah

unread,
Nov 26, 2014, 3:58:21 PM11/26/14
to xata...@googlegroups.com
There are plenty of ways to debug the code as it goes along.  Usually I use echo, var_dump, or print_r statements at various points to see what values are contained in variables.  All I can tell you is that your code won't run the way it is because of the output_fdf() function placement.  The best thing would be to convert it into a method inside the class, then call it using $this->output_fdf().  It is also likely that you need to assess the paths to any files that you include or refer to since relative paths are calculated from the main app directory rather than the action script (vs in a direct script it would be relative to the directory containing the script).

Steve

Reply all
Reply to author
Forward
0 new messages