This topic is locked

Running FPDF from Custom Button Server Code

10/26/2016 10:07:51 AM
PHPRunner General questions
K
kelgate author

Hi All.
I'm a relative newbie to PHP and PHPR, however I have been using it successfully to do the normal, straightforward stuff.
However, I now need to create bespoke PDF files from selected records in list pages - using PHPR's inbuilt report construction doesn't fit the need to create paper letters for sending to customers.
I tried doing this via a custom button using the very simplest "Helo World" code from the FPDF tutorials (minus the <?php ?> tags of course), but for some reason nothing happens. I have tried outputting $result debugging info, but execution doesn't seem to get as far as Client After. The same code functions perfectly when placed into a separate .php file.
I'm rather puzzled - can anyone point this grey-haired ex-Access VBA coder in the right direction please.
Many thanks.
Kelvin

Admin 10/26/2016

The main difference between standalone PHP file and buttons code is that button's PHP code is executed behind the scene via AJAX. Any output you do there will be lost unless you pass it to ClientAfter event using technique described in the manual:

http://xlinesoft.com/phprunner/docs/inserting_button.htm#passing_data

K
kelgate author 10/26/2016



The main difference between standalone PHP file and buttons code is that button's PHP code is executed behind the scene via AJAX. Any output you do there will be lost unless you pass it to ClientAfter event using technique described in the manual:

http://xlinesoft.com/phprunner/docs/inserting_button.htm#passing_data


Many thanks for your response and for reminding me to RTFM (such a lot to take in for a Newbie!). However, although I'm even more flummoxed now. I'm not even getting the default "Sending message to server..." part of the result message. See the button code below...
Client before:

// Put your code here.

params["txt"] = "Hello";

ctrl.setMessage("Sending request to server...");

alert("this is client before");

// Uncomment the following line to prevent execution of "Server" and "Client After" events.

// return false;
Server:

// Put your code here.

$result["txt"] = $params["txt"]." world!";
Client After:

// Put your code here.

alert("this is client AFTER");

var message = result["txt"] + " !!!";

ctrl.setMessage(message);
You'll see that I even added an alert from the example in the manual, but I'm not getting anything back - no message and no alert.
One further clarification please... When you say "button's PHP code is executed behind the scene via AJAX. Any output you do there will be lost unless you pass it to ClientAfter event" does this mean that attempting to generate a PDF via FPDF in the Server code part of the button will fail?
Thanks.
Kelvin

S
stiven 10/27/2016

Hello,
here is how I achieve this.



// Client BEFORE

// I wanted to make sure only one record is selected to generate PDF.

var total=$('input[name="selection[]"]:checked').length;
if(total == 0){

alert('Please select at least one record');

return false;

}

if(total > 1){

alert('Please select one record at a time');

return false;

}
// SERVER SIDE

$record = $button->getNextSelectedRecord();
// Put your code here.

/// this is the record id that will be passed to client after

$result["id"] = $record['your_record_id'];
// CLIENT AFTER
// redirect the user to download the PDF

var url = 'download_report.php?id='+result["id"];

window.location = url;
K
kelgate author 10/28/2016



Hello,
here is how I achieve this.



// Client BEFORE

// I wanted to make sure only one record is selected to generate PDF.

var total=$('input[name="selection[]"]:checked').length;
if(total == 0){

alert('Please select at least one record');

return false;

}

if(total > 1){

alert('Please select one record at a time');

return false;

}
// SERVER SIDE

$record = $button->getNextSelectedRecord();
// Put your code here.

/// this is the record id that will be passed to client after

$result["id"] = $record['your_record_id'];
// CLIENT AFTER
// redirect the user to download the PDF

var url = 'download_report.php?id='+result["id"];

window.location = url;



Hi.
Many thanks for this suggestion. I worked out a way which requires an indicator column in the appropriate tables that signify whether the record is to be printed (when set to True)and I then fire off a separate PHP file which reads through the database and builds an array from the selected records before running FPDF. However, your method is much neater and eliminates the additional record edit/save step.
Brilliant! (At least it looks brilliant to a newbie like me :-) )
Thanks.
Kelvin
PS - What happens to PDFs that are generated on the server? Do they hang around forever or is there some "flushing" action I should take every so often. Tks. K

S
stiven 10/28/2016

Glad you were able to work it out. I'm not sure how you are generating the PDF but you can check out this article about deleting files on the server with php
http://php.net/manual/en/function.unlink.php

lefty 11/1/2016



Glad you were able to work it out. I'm not sure how you are generating the PDF but you can check out this article about deleting files on the server with php
http://php.net/manual/en/function.unlink.php


I would like to see your fpdf code as I have been trying to this on the fly in an after added event and send the pdf as an attachment. Any ideas?

A
Abul 11/1/2016



I would like to see your fpdf code as I have been trying to this on the fly in an after added event and send the pdf as an attachment. Any ideas?


I had made successful in creating PDF file on the file with fetching data from the query using FPDF. We have to change huge code on FPDF file. I had made that in 2014 for a Genetical Testing Hospital. Idea was Doctor will enter order info for patient and finally click on a custom button which will fireup the custom script to collect data from the db and fetch on a template to make PDF file on the fly. Unfortunately, I cannot post here my whole custom code. For some reason, forum throw error. I think it is due to long script. Sorry!