Link to home
Start Free TrialLog in
Avatar of udara22
udara22Flag for Australia

asked on

How to send PDF data to SQL database on clicking a submit button

Hi. This is my situation.

I have 1000 customers. Each of them have their own customer ID. I want to send a pdf to everyone with 5 questions, and once they submit the form, ideally the data comes straight to my SQL database. I also want to track who's who, so I want to send the customer ID to the PDF form with the link I send to my customers.

Not sure how to do it, tried sending the ID through GET method, so its www.mysite.com/questions.pdf?id=123 then Managed to read the ID and put it into a text field in the PDF form, but it doesn't work on Chrome browser just on mozilla and IE.

So I would like to know another method to send the ID to the PDF first, and then once the customer fills the form and submits it, the ID and answers to the questions come to my database. What's the best way to do this or other solutions to achieve the same thing?
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Chrome has it's own PDF viewer built in, the other browsers use Adobe Reader (or Acrobat if installed). The built-in viewer behaves quite a bit differently than Reader.

I would send a FDF file instead of the PDF file, and let the FDF file load the PDF file from the server. The FDF file can contain the user ID, so you don't have to do any tricks to get that information into the form. Once you submit the form, submit it just like a regular HTML form (that's one of the options when creating the submit button), and then let your server handle the communication with the SQL server.

Have you ever worked with FDF files before. Do you know what an FDF file is?
Avatar of Eddie Shipman
What is your server-side development language?
To be honest, this is better left to HTML forms and not using PDF to do this.
There is nothing wrong with PDF forms for this application. If it's important that the form's design is the same as e.g. a printed form, or for other form fidelity reasons, you can do whatever you could with HTML forms also with PDF forms.
If It's just 5 questions, don't you think the PDF is a little overkill?
If he's using ASP, then this article shows a great way to do this with PDF:
http://63.236.73.62/issue/010822.htm

The FDF toolkit is really not needed except to build the FDF and you can use Acrobat to do that.

I've done this in ASP and PHP.
We don't know what else is on the form, so I go with the asker's plan to use PDF.
You are right, the FDFtoolkit is not needed - it's very easy to create the contents of a FDF file with any web technology.
Hi!

If your website is building the pdf files on the fly then its easy to put
the ID into a query-string and use it to build a form that holds the customer ID.
This can be done either with HTML forms or PDF or matter of fact any web language.
The only thing you have to have in mind how the workflow is in your case.

Let's say you are sending each customer a link to this webpage that builds your question form. Then you need to provide the url the customerid something like this
www.mywebsite.com/customerquery.pdf?custid=1001 
and then you pass the custid parameter and value to your form and then use that along with the answers and insert the data to required tables.

Regards,
    Tomas Helgi
Avatar of udara22

ASKER

Hello everyone, thanks a lot for the advices.

The company wants to give the staff members the ability to create forms so it takes the work load from web developers, and PDFs can be downloaded, and filled at a later stage and submitted once done. If the PDF data collecting works fine, the company might use this technology for all their forms (some of them are big). Thats the reason for using PDFs. I suggested HTML forms too, but they are really keen to use this technology.

I tried passing the I'd through the url, it works on Mozilla and ie, but not in chrome, for some reason in chrome it says "Undefined". And on mac it pops up the 'save to location' popup so cannot pass through URL

I haven't worked with PDFs before. Very new to me. I thought of using ajax to post the data to server, I put the below code in the "button's" javascript (I got this from the internet)

$.ajax({
    type : 'POST',
    url : 'http://www.domain.com/pdf/process.php',
    dataType : 'json',
    data: {
        "questionone" : this.getField("questionone").value
    }
    ,
    success : function(data){

    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
                               
    }
});

and put this in the process.php file

<?php
$questionone= $_POST['questionone'];
 
$to = "mail@email.com";
$subject = "Test mail";
$message = $questionone;
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

I thought this might send me and email once the data goes to the process.php file. But I didn't receive and email with $questionone.

I have a feeeling just ajax post might do the trick, but really cant get it to work and driving me nuts. Is there anything wrong with the ajax code or whats the best way to test it from the process.php page?
I can work up something based on the 15seconds article tonight, including sending the UID.
Here is a small problem with your idea: If your clients/customers/employees do not all have the full version of Acrobat (not the free Reader), you will have a problem - PDF forms cannot be saved by Reader in a half filled state and later submitted. If you do have the user ID in the PDF file as ThomasHelgi suggested, this may not be a problem, but if you are planning on sending the ID via a FDF file, you are partially filling the form with the ID, and then the user would have to fill the complete form in that session.

There are ways around that: You can reader-enable your document, and give it more rights when it gets opened in Reader. Saving a form is one of the options. You can use Acrobat to create such forms, but the EULA you accepted when you installed Acrobat limits you to 500 forms (either sent out or received), so your 1000 customers would fall well outside that limit. You can also use a server based solution to reader enable those forms, but that has a much higher price tag than Acrobat. You need to talk to somebody at Adobe to find out exactly how much that server solution would cost for your application.
No, No, No. read the article I posted. By using the FDF, they aren't actually "saving" the form data but going to be sending tit to the corresponding script that takes the values and saves them to DB.
Avatar of udara22

ASKER

@EddieShipman : Thanks for the link Eddie, I had a good look, but unfortunately I am using php and my asp skills are nil. Also my server side development language is PHP. I've seen the technique of using CGI-BIN to pass the information, but not sure how it really works  and my server doesn't have a cgi-bin folder? Also eddy is it possible to use the link you send on a php server?

@khkremer : I tried saving the form in Reader and it works fine. Also in regards to the adobe form limit; I though this limit wont affect the form at all if I put a javascript submit button to submit the forms, not the submit button you see at the top of the Acrobat Toolbar?

@EddieShipman and khkremer : Can you guys please explain how the FDF works? In the worst case, I might get the customers to put the customer_id manually, maybe put the customer id in the email and ask them to copy and paste, not the ideal solution, but I have no idea how the FDF solution can be integrated on to this form and pass the customer Id.Passing the id through the url was an amazing, easy solution, but blooyd chrome doesn't like it.  I won't be able to generate the PDF on the fly because the company wants the staff to create the form and me to put the javascript codes on to the PDF so it interacts with the external PHP file on my server and the PHP file writes the data to the SQL database..
The 500 copy limit only applies when you reader-enable the form, e.g. so that you can save the form locally with the form data. Without reader-enabling a form, you can still save the form in Reader, but it will only save the blank form. So if you can create your form without the need to reader-enable it, then the limit does not come into play. However, it does not matter if you use the submit button at the top of the window, or your own button that calls the Javascript submit function, or any other way you can think off. Technically, even printing such a file and sending in the printout would probably count as submission.

The problem is that if the user ID is provided via the FDF file (which stands for forms data file), then the form is partially filled (with the user ID), and when you then save it in Reader, it will not save that information, you will end up with a blank form again.

You can create your own FDF sample file in Acrobat. Just load your form, fill in some information and then save the form data. How you do that depends on which version of Acrobat you are running. In Acrobat X you'll find this on the Tools pane at Forms>More Forms Options>Manage Form Data>Export. I'll try to whip something up to demonstrate what FDF files can do.
Go to http://www.khk.net/eeform/form.fdf and see what happens - you should get a form with some data filled in. You can see both the forma and the FDF file when you select the directory http://www.khk.net/eeform

The submit button is not doing anything in this sample, but you can take the two files, change the two instances of the URL in the FDF file and try it on your own server.
Avatar of udara22

ASKER

Hi khkremer, Thank you very much for your time. It worked fine on mozilla (pc) but not on chrome(pc) and mozilla and chrome(mac). Also can you tell me how to grab the code in the fdf?
Avatar of udara22

ASKER

@khkremer : also when i open http://www.khk.net/eeform/form.fdf on mac firefox, it asked me to save the fdf and didnt really direct me to pdf with the id in it?
Avatar of udara22

ASKER

Guys, I used FPDF to generate the pdf and added the id through a GET variable and passed the ID through the URL. Now, it grabs the ID on any browser and generates the PDF with the ID in it, which is awesome. So i might use fpdf to solve the ID issue, ;

But now the issue is , is it possible to chuck my existing pre-designed pdf with input fields in to this fpdf process so it has all the fields + the customer ID?
The way it works in the article is you have your PDF already designed with the form fields. Then you create an FDF to match the form fields. I was not able to work on the demo last night but will try to get to it today or this evening..

Now, you can pass the ID into the script that creates the FDF and have it pre-populate a field in the PDF with the CustomerID, I'll put that in the demo.
As I said, chrome has it's own PDF viewer and will always be a problem. You have to turn off it's viewer via about:plugins. I don't know about the Firefox problems. It's supposed to work...

I don't think that FPDF allows you to merge existing PDF files.

You could merge the FDF and the PDF on the server. A simple and free tool to do that is pdftk (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) This way, the PDF is already prefilled, and you avoid the problems that a downloaded FDF would have.
Ok, I've been working on this for a while and can't get the form to pre-populate nor submit.
I did, however, find a great tutorial on this subject that you may be interested in:
http://koivi.com/fill-pdf-form-fields/
Code:
http://koivi.com/fill-pdf-form-fields.zip


Check it out and see if it helps you.
Avatar of udara22

ASKER

@khkremer : Yes FPDF needs FPDI to merge existing PDFs and it's merged as an image so I lose the input fields. So I guess FPDF isnt the way to go with this.

@EddieShipman : Eddie, Thanks heaps, koivi.com tutorial looks like a real deal, I guess I just need to reverse the process from HTML - > PDF to PDF -> HTML. Not easy but i'll give it a go.

@khkremer and EddieShipman : I also found this article, will sit down and read it now, what do you guys this about this? http://phpbootcamp.com/articles/fdf.htm
The problem I ran into is that the browsers would not serve the PDF, that the FDF referenced, from the web. I can go ahead and post the code converted from ASP to PHP and you can play with it to see if you can get it working.
SOLUTION
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of udara22

ASKER

Sorry guys, couldn't reply, got the bloody winter flue..

@khkremer,  Thanks heaps, then I wont go ahead with that old article. Again, I made the FDF right, and pdftk looks good too, but I'm wondering how pdftk works on the server because its an .exe file? Sorry if I sound stupid, but what am I doing wrong?

So, If i need to pass the UserID to the PDF file, should I pass it through FDF file ? if so how can that be done, do I need to create new FDF file per customer?

Also, would you mind having a look at EddieShipmans ZIP file, it looks pretty damn close but having trouble with passing and importing data. :)

@EddieShipman, Thanks a lot man. I think we are almost there, which is awesome.

Few things tho,

[1]. I figured out why importing data doesn't work. When the PDF opens up, at the top it has a button called "Options", If you click on it , is gives 2 options; 1. Trust this document, 2. Block this document. If you click Trust this Document, it populates the fields, otherwise it doesn't. But customers might miss this step like we did, then it wont grab the customer ID, which might be a problem.

[2]. Also if I open the FormTestImport.php?UserID=123 on chrome, it downloads the FDF file and nothing happens from there. :(

[3] For receiving UserID, I think GET is the way to go as we have at the moment so  I can pass the id through the URL, and what if we use "POST" for passing the other information to the FormTestSubmit.php file? Because some user input data might be very long to pass through the URL. I tried changing to POST, but no luck.

If we can pass the POST data to FormTestSubmit.php, we are almost there. Not sure why posting data doesn't work.What if we use ajax on the submit button to POST to the URL instead of the 'Submit Form' action? I tried using ajax too, didnt work. Maybe I missed something because PDF APIs are different to standard javascript. :S
Avatar of udara22

ASKER

EUREKAAA... Alright lets cool down here. I managed to pass the data from PDF to FormTestSubmit.php. Its so simple, what we have to do is, just put the code below on to the top of the FormTestSubmit.php and it shows the array of data we receive from the PDF file.

 echo '<pre>'; print_r($_POST); echo '</pre><br><br>'; 

Open in new window


and in the table cell, it should look like this

<? echo  $strFirstName ?>

Open in new window


Now, I can receive the user input data, and can be written to a database. Which is awesome. The only issue is passing the UserID to the pdf. PHP to FDF to PDF doesnt work on Chrome and also on MAC. Can you guys think of a way around it? The worst case I might get the user to input the UserID too, but it might be a security and reliability issue and my boss wont like it either.. :S
One thing you could do is have a HIDDEN field in the PDF, and thus the FDF that gets populated by from the script. Pass it in the URL and populate it in the FormTestImport script.
It would also then be available in the FormTestSubmit script just as another form field.

You can tell Chrome to use Adobe's reader:
1. Install Adobe Reader or Adobe Acrobat if it is not already installed
2. Open Google Chrome
3. In the address bar, type . . .
      about:plugins (that’s the word about  a colon (:), then plugins
4. The Plug-ins Tab will open
5. Scroll down until you see either Adobe Acrobat or Reader.
6. Click the Enable link

You will need to notify the users of Chrome that they have to do that to be able to fill your form.
You should be able to call the .exe file from your PHP script. If you don't want to figure out how, you may want to take a look at the pdftk-php project: https://github.com/andrewheiss/pdftk-php
It creates a FDF file on the fly, calls pdftk and outputs the resulting PDF file to the browser. All you need to do is change the path to pdftk and the options it uses to merge the FDF and PDF (it also flattens the file, which you don't want to do, because it strips all other interactive form fields from your PDF).

... or you could just use the source of pdftk-php as a model for your own implementation.
Avatar of udara22

ASKER

Thanks eddy.  Yes, that's what I tried doing, pass the id through url to FormImportTest.php and populate the PDF. But it doeant work on mac. Mac browsers just download the fdf and nothing happends after that.
But if you make the UserID part of the PDF/FDF as a form field that is not visible, you should be able to parse it into the FDF using PHP just like any other field in the FormTestImport script.

Do any of the PDF fields get populated on the Mac?
Avatar of udara22

ASKER

@khkremer : thanks. I'll try it and let you know how it goes.

@eddyshipman : eddy, if the hidden field gets populated with out any user interactivity, that's all I need. Let's see. I'll try it when I get to the office in 1 hour.

Then what I might do is, force the PDF download so no browsers can affect PDF. I wondering whether the hidden field value will be there when the users download the pdf.?
None of the form fields will save data when PDF is saved.
Avatar of udara22

ASKER

@khkremer : Thanks for the pdftk-php link. Im getting there with it. But one problem, I'm not sure how to link the PDF Tool kit downloaded files with pdftk-php.php on line 71. I downloaded PDF tool kit files and put the files on to a folder on the server, and I put the absolute path to that folder, but it throws an error when I try to see the submitted PDFs

passthru("C:\Users\udara\Documents\My Web Sites\test Server\pdfdata\orig_pdftk $pdf_original fill_form $fdf_fn output - flatten");

Open in new window


Should I just drag and drop the files from the PDF Tool kit to orig_pdftk folder? My Folder path looks like this now

orig_pdftk
-- bin
-- docs
-- license_gpl_pdftk

is this correct?
Avatar of udara22

ASKER

@EddieShipman : I tried passing data to the hidden field, it still gets blocked by acrobat and asks for user permission to populate the data, so by the looks of it we can't pass the UserID through the URL using FormTestImport.php. And also on mac its crazy, it just opens the FDF file and sometimes the PHP file with the FDF data in it.
Can you post your new code and PD?
That's a problem with the "\" characters, which are interpreted by PHP as escape characters - in order to get "real" backslashes, you need to escape them with a backslash... So try something like this in the passthru command:
passthru("C:\\Users\\udara\\Documents\\My Web Sites\\test Server\\pdfdata\\orig_pdftk $pdf_original fill_form $fdf_fn output - flatten");

Open in new window

Avatar of udara22

ASKER

@khkremer : I changed the path \ to \\, but same error. I recon its the place I've put the pdfToolKit files are wrong. Coz the path looks correct.

@EddieShipman : See attached file. I removed everything else from the FDF section on the FormTestImport.php and just left the UserID so you can test. Again, if i "allow", the UserID goes through to the submit page, if not, it doesn't.

FormTest-PHP-udara.zip
I just tried this on a Windows machine, and the "\\" as path delimiter works. Are you sure you installed pdftk correctly? There are two files that need to go into the same directory (the executable and a library). What is the error you are getting? Try to just have the path to pdftk.exe in the passthu call (without any arguments). Does that work?
Avatar of udara22

ASKER

@khkremer and EddieShipman : Guys, after a lot of trial and error I came to a solution. Letting customer copy and paste the client id form the Email they receive. So I though I might make the ClientID field required and put it as a standard input field.

Now everything looks fine. thanks guys a lot for your help and I really appreciate your help on this.
Avatar of udara22

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for udara22's comment #37784659
Assisted answer: 200 points for khkremer's comment #37752827
Assisted answer: 300 points for EddieShipman's comment #37766451

for the following reason:

Thanks guys a lot.
You could hwve closed and assigned the points yourself. Just select accept multiple solutions.
Avatar of udara22

ASKER

Ah cmon. You guys deserve it for the time you put in for this.
I know that, but I was saying that you disn't have to enter a close request but to accept the solutions, yourself. I will get a moderator to decline the close and that will let you do it.
Avatar of udara22

ASKER

Oh. Damn. So close request doest mean the problem has been solved? This is confusing. But I selected multiple solutiins
Yes, you can select the multiples, but if you mark yours as the solution vs. assisted, it makes it a close request.
Avatar of udara22

ASKER

Hi, Please ignore the close request. I've done it by a mistake.
Avatar of udara22

ASKER

I've requested that this question be closed as follows:

Accepted answer: 300 points for EddieShipman's comment #37766451
Assisted answer: 200 points for khkremer's comment #37766131
Assisted answer: 0 points for udara22's comment #37784659

for the following reason:

The attached file is a modified version of what EddieShipman posted, and my version works better. Thats it.
Avatar of udara22

ASKER

damn
Avatar of udara22

ASKER

Guys, so sorry to come back to this post, but the boss didnt like to get the user to input the ID, then i found this link which uses PDFToolKit like khkremer said.

PDF array

This Works GREAT, and works on all the platforms and all the browsers. The only problem is when I use my PDF it says "FPDF-Merge Error: Incremental updates are not supported", I have no idea how to solve this issue, is there a setting in PDF at all? or a Mod i can download form the internet? Please help guys,
Avatar of udara22

ASKER

Holy shit. Figured it out.. and Thanks khkramer. PDFToolKit did the job :D