Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Using php to open a pdf

Featured Replies

Ok, so I assume that this is fairly simple but I don't have enough programming experience to get it to work correctly.

 

I have a html form that I have made to generate a fdf file and save it on the server. This file should then be opened using a pdf also on the server and insert the form details into the correct part of the pdf. It works fine but not in one simple step. I need to add something to my php such that when the form is submitted, the fdf file is saved and then immediately opened and displayed to the user. As it currently stands, the file needs opening manually. I've been messing about with fdf_open but without success!?

 

  <?php
   // check that a form was submitted
   if(isset($_POST) && is_array($_POST) && count($_POST)){
       // we will use this array to pass to the createFDF function
       $data=array();

       if(isset($_POST['Text2'])){
           // the name field was submitted
           $pat='`[^a-z0-9\s]+$`i';
           if(empty($_POST['Text2']) || preg_match($pat,$_POST['Text2'])){
               // no value was submitted or something other than a
               // number, letter or space was included
               die('Invalid input for Text2 field.');
           }else{
               $data['Text2']=$_POST['Text2'];
           }

           if(!isset($_POST['Text3'])){
               die('You did not submit the correct form.');
           }

           $data['Text3']=$_POST['Text3'];
           $data['Text4']=$_POST['Text4'];
           $data['Text5']=$_POST['Text5'];

           // Date
           $data['Text1']=date('d/m/Y');

           // FDF file - function definition
           require_once 'createFDF.php';

           // file name .fdf
           $fdf_file=$_POST['Text2'].time().'.fdf';

           // the directory to write the result in
           $fdf_dir=dirname(__FILE__).'/results';

           // pdf file for data to go into
           $pdf_doc='../voucher_online.pdf';

           // generate the file content
           $fdf_data=createFDF($pdf_doc,$data);

       }
   }else{
       echo 'You did not submit a form.';
   }
?> 

If I've understood correctly, you cannot do what you want - that is to say, you cannot return the PDF data to the client (so it loads automatically in the browser) and then tell the PDF viewer to load the FDF data into the displayed document (this last action is a manual one).

 

Instead you'll need to look at a solution which can put the FDF data into the PDF on the server, and save the result as a regular PDF which can then be returned as the response to the client. Of course they won't be able to edit the data in the PDF, but that shouldn't be a problem for you (otherwise why not just stick with HTML forms?).

 

This process is usually called "merging" or "flattening" the PDF template+form into a single uneditable PDF document. Try a Google search for "pdf fdf flatten" and "pdf fdf merge" to see the available toolkits. There may not be a PHP-native way to do this however.

  • Author

now that would explain why I could not for the life of me make it work!

Thanks, I will have a look and inevitably come crawling back to the forum in a couple of hours! :)

I agree, have the file saved as a PDF file, then do something like this to display it in the browser:

 

<?php

header('Content-type: application/pdf');
header('Content-Disposition:inline; filename="original.pdf"');
readfile('original.pdf');

?>

 

Or have the user download the PDF:

 

<?php

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="original.pdf"');
readfile('original.pdf');

?>

 

You will probably need to edit the above to work but that's I think along the lines of what you need to do (it's a saturday morning so my mind is still half asleep)!

  • Author

Yeah that works perfectly for opening the file, now I just need to find a way of turning my fdf into a pdf. Not had much success in making sense of anything I have found thus far.

Thanks for that tho....another step closer! :-)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.