Hello All,
I am in the process of creating a module that shows a custom form which when submitted returns a PDF document that is generated on the fly. The document has content that is merged from additional tables I have created in the Drupal DB. What I have not been able to figure out is how to respond directly to the sumbit request by sending back the generated PDF document.
Using drupal_goto to send back a URL to the PDF generating script (PHP) is not an attractive option - I would not have easy access to Drupal user data from an independent PHP script. (Not sure that is clear).
Briefly what I want is this
a. User clicks on Submit
b. Validate sumbitted form values, pick up user information from my extra Drupal DB tables.
c. Generate the merged PDF
d. Squirt back the PDF as the response to the submit
I'd much appreciate any help.
Comments
node_api
Hi,
I think your looking for the node api. For example, I have a module that needs to run some file functions depending on if a node
is added, updated etc.
file_sync mod:
I think you would want something similar to 'function sync_nodeapi' that hooks into the submit and calls a function.
For reference, see http://api.drupal.org/api/function/hook_nodeapi/5
Thank you. I'll look at
Thank you. I'll look at nodeapi. It does seem strange to me that Drupal does not offer an easy way to send back all kinds of data - images, pdfs etc - in response to a form submit. Can't be. I am looking in the wrong place?
The solution
Hmmm... . I didn't get much help on this forum after all! Anyway, I figured out a solution so here it is for anyone looking to do the same thing. Code the hook_form_submit
function myModule_submit($form_id, $form_values)
{
/*If you have not already validated your form data in hook_form_validate you should do it here now. In a real implementation
you would get the PDF (or whatever) file some other way.*/
$filepath = 'file.pdf';
$data = fopen($filepath,'rb');
@ini_set('zlib.output_compression', 'Off');
$http_headers = array (
'Pragma: no-cache',
'Expires: Mon, 01 Jan 1999 05:00:00 GMT', // Need way in the past
'Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT',
'Cache-control: must-revalidate, no-store, no-cache, post-check=0, pre-check=0',
'Content-Transfer-Encoding: binary',
'Content-Length:' . filesize($filepath),
'Content-Type: application/pdf',
'Content-Disposition: attachment; filename="file.pdf"',
'Content-Type: application/download'
);
foreach ($http_headers as $header) {
$header = preg_replace('/\r?\n(?!\t| )/', '', $header);
drupal_set_header($header);
}
while (!feof($data)) {
print fread($data, 8096);
}
fclose($data);
}