Help with PDFtk function

I use it, just the PHP version. The others are overkill for most programmers. But, if you do Java or other programming the others are handy.

Also, NetBeans lets you link to your site using FTP and will update the files automatically once you save them. It is a good tool. And, it is safe. Takes a bit to get used to it all as it looks ahead for you and you can auto-enter commands and the such. It will automatically enter ending brackets for you so you don’t forget them. I like it a lot! Good luck with it…

Bro, been playing with this code on NetBean for hours and I think I finally got it. Or at least when I submit my reference code to the page that does the query it doesn’t spit error messages at me any more. This is the code


<?php

    //create a connection to MySQL
    require 'dogs.php';
    

    function forms_data() {
$servername = XXXXXX";
$username = "XXXXXX";
$password = "XXXXX";   

// database name
$dbname = "thetitl1_livesite754";
$conn = mysqli_connect($servername,$username,$password,$dbname);

// Create connection
$db = new mysqli($conn);

// Check for errors
if($db->connect_errno){
echo $db->connect_error;
}
//query for reference_code from 'forms' table
$refcode = filter_input(INPUT_POST, ["reference_code"]);
    
// Execute query
$result = $db->mysqli_real_query("SELECT * FROM thetitl1_livesite754.form_data WHERE id IN 
    (SELECT form_id FROM thetitl1_livesite754.forms WHERE reference_code ='$refcode'");

// Always check for errors
   if($db->connect_errno){
   echo $db->connect_error;
   }

  return $result == true;
}

?>

Here is the link requesting the reference code gate way: https://pdforms.losttitleconnection.net/TX%20Title%20App%20Gate.php

And here is the reference code I’m using: QKEX49MKDE

When I click the link it gives me the html form field page that request the refcode. When I enter the code and hit submit, I’m carried to the TX Title App.php and does not give me errors. If it means success. I’m going to start putting in the other mysqli_use_results code I found here:
http://php.net/manual/en/mysqli.use-result.php… wish me luck and thanks @ErnieAlex

  • EDIT by benanamen: Deleted login credentials

You should edit your post and REMOVE your password. Never post passwords online in post’s!

Looks like you are getting closer. Good luck!

Hey ErnieAlex,
Thanks for the heads up on the passwords, total brain fart there, but also thanks to @benanamen for hooking me up on the edit and repost. I appreciate the looking after.

so I’m at the point where I think I’ve almost got this done and would like to ask you to take a look at my code. It seems like everything is going well except for a few points when it comes to the following lines of code:


//Replaces form_fields in html temp with "data"
// the following line gives me a " Unreachable statement " error in beans but isn't called out when I //execute the file online, so not to worried about this one, but I know it KINDA important.
while ($row = mysql_fetch_assoc($result)) {
$new_form = str_replace($row["name"], $row["data"], $new_form);
}

// reference the Dompdf namespace
require_once ‘lib/dompdf/autoload.inc.php’;

//the following line is giving me a " **Parse error** : syntax error, unexpected 'use' (T_USE) in  //**/home1/thetitl1/public_html/pdforms/TX Title App.php**  on line  **52**"
use Dompdf\Dompdf;
//and this one gives a "Syntax Error, Unexpected Identifier 'Dompdr" "
use Dompdf\Options;

besides these to areas of code, my only other concern is beans giving me a "
method length lines 33 (20 allowed)" in relation to the ‘function forms_data()’ line of code.

here is a complete copy of the code:


<?php

    //create a connection to MySQL
    require 'dogs.php';
    

    function forms_data() {
$servername = "xxxx";
$username = "xxxx";
$password = "=xxxx";   

// database name
$dbname = "xxxx";
$conn = mysqli_connect($servername,$username,$password,$dbname);

// Create connection
$db = new mysqli($conn);

// Check for errors
if($db->connect_errno){
echo $db->connect_error;
}
//query for reference_code from 'forms' table
$refcode = filter_input(INPUT_POST, ["reference_code"]);
    
// Execute query
$query = $db->mysqli_real_query("SELECT * FROM thetitl1_livesite754.form_data WHERE form_id IN 
    (SELECT id FROM thetitl1_livesite754.forms WHERE reference_code ='$refcode'");

// Always check for errors
   if($db->connect_errno){
   echo $db->connect_error;
   }

  return $query == true;

//Replaces form_fields in html temp with "data"
while ($row = mysql_fetch_assoc($query)) {
$new_form = str_replace($row["name"], $row["data"], $new_form);
}

// reference the Dompdf namespace
require_once ‘lib/dompdf/autoload.inc.php’;
use Dompdf\Dompdf;
use Dompdf\Options;

$new_form = file_get_contents("TX_Title_App.html");

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($new_form);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper(‘A4’, ‘portrait’);

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

// Save to file
$pdf_gen = $dompdf->output();
file_put_contents(“TxTitleApp.pdf”, $pdf_gen);

// Display PDF in browser
echo $dompdf->output();
   
/* free result set */
mysqli_free_result($query);

/* close connection */
mysqli_close($conn);    
    }

?>

I’ve looked this over many times, each time trying to work through each line of code imagining what it is supposed to do as I do. But as my eyes are still not as well versed as yours, I may have missed something. If I could ask you to please look this over and give me your insight, I would again be grateful for the help.

Nice! You have made a lot of progress! First, you don’t need to use the mysqli_real_query! Just use the function mysqli_query instead. If you use the “real” version, it does not access the results internally and you would need to use extra commands to use/store the results. The standard mysqli_query() functions does these for you on the server. So, just remove the real from the “Execute query” area.

Once you get this working, you should learn about “prepared statements” to protect your database from bad inputs from the user. The filter_input() function does protect it a lot, but, nowadays, most also use the prepared statements, too. We can cover that after you get it all working.

Right after you execute the query, you check for a connection error. ??? I think you want to check for a query error instead. Instead check for just use mysqli_error($db) OR $db->error (Object version) …
(Basically just remove the connect_ part)

This line: return $query == true; will end the function and return “true”. Not sure about that line! Why are you using a function? Are you planning on using this code as a function and calling it from several places in your page’s code? If not, you should not make it a function and use a button to post the page and have the code check for the button and handle the creation of the form.

In the replace form_fields loop, you should not use the live field names in your form. Remember they have to be unique. We were using three underscores before and after each field name to make them unique. Unless you are 100% sure that the name of the field is unique in your form page. You can check that by searching the incoming form for each field name and insure there is only the one you want is there. Easier to me to just use the underscores as they are always unique.

Next, you need to load the new_form BEFORE you replace the field names with the data on it. The $new_form variable that holds the live form is created AFTER you replace the data in it. So, that will not work. Move the file_get_contents up just before you do the mysql_fetch_assoc() and replace the data.

Lastly, near the end of the code, you save the altered form. You might want to save that finished form in a special folder and label it with the user’s ID or name. If you save it just as TxTitleApp.pdf, a second user will write over that one. You would only have the last form created. You should think out the logic of how you would want to store these for future uses. You might even want to add date-time to it in case a user tries to send many different versions of it. Think a little “out-of-the-box” on this and remember a user might not be a perfect user… Ha!

So, in general, it looks good. Very close to having it finished up. Good luck…

Good Sunday morning to you @ErnieAlex.

Hope life is being as good to you as you have been to me… I truly believe in Karma and you should have plenty of it coming your way.

So I’m gonna respond to you in bullet point so i make sure I cover everything in your response

  1. mysqli_query - fixed

  2. Prepared statements - need to research that, but understood

  3. $db->error, connect removed on both the error and errno statements

  4. function form_data() removed.

  5. form fields - 100% unique as I created them to be abreviation combos of to words like vin6, vehvin, vehyr, vehmk, vehmod…etc.

  6. new_form - this one is gonna be alittle long as I am a bit confused by your wording, so here goes. in your explanation you use new_form and $new_form are these mean to be one and the same? as I understand it you would like that I move the ($new_form = file_get_contents(“TX_Title_App.html”); ) just before you do the mysql_fetch_assoc() statement. But it is already on line 43 and the fecth is on line 50 with the ($dompdf = new Dompdf();
    $dompdf->loadHtml($new_form); ) statements sandwiched in between. So I’m not sure what you think needs to be changed.

  7. Your last point. I’m really not interested in saving a copy of each form for me… these forms are for the customers’ service and I don’t plan on needing them as right now I am not required to keep a copy by any statue. So the fact that the form will be overwritten each time is good with me. In fact as I understand, the code is fetching the html doc with field names and replacing them on each occurrence, then allowing the customer to save, print or download a copy for themselves. That’s exactly how it should be…and the fact that they will be given access to the page that creates these forms on a time period basis will ensure they are not correcting the info and running various transactions on the one purchase of the service.

  8. This point is one of my own, the (use Dompdf\Dompdf) statement. Okay, never mind, just glanced over at my code and the changes you asked me to make cleared up the error message on that line. Not sure what did it, but it’s gone now.

Let me run this and see what I get… and study up on the “prepared statements”. I’ll get back with you here soon with the outcome. Hopefully, it’s with a message of success. If so, I’m gonna need your address… so I can send you a bottle of champagne and we can pop a top via video chat in celebration. LOL

1 - good!
2 - Here is a link to get you started. I should also say that everyone seems to favor PDO and prepared statements for the best security. So, once you get this working, we can help you move up to both of these. This will give you something to read: Prepared-Statements
3 - You want the connect_error when checking after the connection is made, not on any other queries
4 - good!
5 - good!
6 - What I meant is that you use file_get_contents to load the new form BEFORE you replace the data inisde it with the live data. You need to load the new form THEN replace the fields inside it. Make sense?
7 - Yes and no! If you have two user’s running this at the same time, and you save the form, who knows who would save it. I would suggest not even save it then. You can code it so it is just displayed or downloaded automatically and let the user worry about keeping it. Saving over itself could cause issues when multiple users are active. That is why I suggested changing the name. Just don’t save it, I think.
8 - good!

Very close, my friend!!! Nice going so far!

Looks like I’ve worked my way all the way done the code clearing up issues and now I’m stuck on this
syntax error, unexpected end of file in /home1/thetitl1/public_html/pdforms/TX Title App.php on line 66

<?php

//create a connection to MySQL
require 'dogs.php';
   
$servername = "xxxx";
$username = "xxxx";
$password = "xxxx";   
$dbname = "thetitl1_livesite754";
$conn = mysqli_connect($servername,$username,$password,$dbname);

// Create connection
$db = new mysqli($servername, $username, $password, $database);

// Check for errors
if($db->connect_errno){
echo $db->connect_error;
}
//query for reference_code from 'forms' table
$refcode = filter_input(INPUT_POST, 'reference_code');

// reference the Dompdf namespace
require_once '/home1/thetitl1/public_html/pdforms/lib/dompdf/autoload.inc.php';
$dompdf = new Dompdf\Dompdf();
$options = new Options(); $options->set('isPhpEnabled','true');

$new_form = file_get_contents("TX_Title_App.html"); 

$dompdf = new Dompdf($options); $dompdf->loadHtml($new_form);

// Execute query
$query = mysqli_query($conn,("SELECT * FROM thetitl1_livesite754.form_data WHERE form_id IN SELECT id FROM thetitl1_livesite754.forms WHERE reference_code ='$refcode'"));

//Replaces form_fields in html temp with "data"
if ($result = mysqli_query($conn, $query)) {
    while ($row = mysqli_fetch_assoc($result)) {
$new_form = str_replace($row["name"], $row["data"], $new_form);
}

// Always check for errors
   if($db->errno){
   echo $db->error;
   }

// (Optional) Setup the paper size and orientation
$dompdf->setPaper(‘A4’, ‘portrait’);

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

// Save to file
$output= $dompdf->output();
file_put_contents('TXTitleApp.pdf', $output);
   
/* free result set */
mysqli_free_result($result);

/* close connection */
mysqli_close($conn);    

?>

These were the two options. Save to file which we were going to drop. And, display in browser which you took out. I think you need to swap them in your current code. Other than that, it all looks good now.

Note, I took out your password and IP addresses. You do not ever want to show them in an online post or someone will hack your database.

thanks for doing that, was so focused on writing the post and forgot…

please look over post 68/70 closely as I have edited it a few times…but still getting a

Parse error : syntax error, unexpected end of file in /home1/thetitl1/public_html/pdforms/TX Title App.php on line 64

Thanks again sir

which is line 64? ???

still playing with it, let me get to a stopping point and I’ll send you what I have then with error messages if any

Okay… Usually if you get “Unexpected end of file” it is just missing one of the ending brackets.

If (…) { code } If you forget the ending bracket, it does not work. So, If’s , While’s etc…

bit frustrating cause I would think beans would point them out, but I’m gonna go line for line to see what happens. Thanks bro.

Well, if you click on the first one in the clause in Netbeans, it will highlight the last one for you.
If the last one is NOT where you think it should be, fix it. If the ending one is missing it will mark it red.
So, it does mark it, but, sometimes it does not show it correctly unless you tag the first one in a clause.
Hope that makes sense…

Really trying to work this out myself but I seem to be stuck…

  1. Do i really need to require the dog.php file that set up a dbase connection and have variable in the code for a connection too.

  2. I’ve been working this line for line and I’m stuck on this error message:

Warning : mysqli_fetch_fields() expects parameter 1 to be mysqli_result, object given in /home1/thetitl1/public_html/pdforms/TX Title App.php on line 34
Unable to stream pdf: headers already sent

It gives me this whether I use the fetch assoc or the fetch fields. I figured the fetch fields would be better as I need the arrays of both the name and data columns.

When I run the following query statement through phpmyadmin using a reference number :

SELECT name, data FROM thetitl1_livesite754.form_data WHERE form_id IN(SELECT id FROM thetitl1_livesite754.forms WHERE reference_code =‘QKEX49MKDE’)

I get this as a result:

Here is a copy of the full code as of now.


<?php

//create a connection to MySQL
require 'dogs.php';

//query for reference_code from 'forms' table
$refcode = filter_input(INPUT_POST,'reference_code');

// reference the Dompdf namespace
require_once '/home1/thetitl1/public_html/pdforms/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf;

use Dompdf\Options;
$options = new Options($options);
$options->set('isPhpEnabled','true');

$new_form = file_get_contents("TX_Title_App.html"); 
   
$dompdf = new Dompdf($options); $dompdf->loadHtml($new_form);
$servername = "x";
$username = "x";
$password = "x";   
$dbname = "thetitl1_livesite754";
// Create connection
$db = new mysqli($servername, $username, $password, $database);

$db->query("SELECT name, data FROM thetitl1_livesite754.form_data WHERE form_id IN(SELECT id FROM thetitl1_livesite754.forms WHERE reference_code ='.$refcode.')");


//Replaces form_fields in html temp with "data"
if ($db) {
//below is the line referenced in error message above     
while ($row = mysqli_fetch_fields($db)) {
        $new_form = str_replace($row["name"], $row["data"], $new_form);
    }
}

// (Optional) Setup the paper size and orientation
$dompdf->setPaper(‘A4’, ‘portrait’);

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

// Save to file
$output= $dompdf->output();
file_put_contents('TXTitleApp.pdf', $output);
   
/* free result set */
mysqli_free_result($result);

/* close connection */
mysqli_close($conn);    

?>

Hope you can see whatever it is that I’m missing, cause man, it’s frustrating to know I’m so close and I don’t know how to fix it.

I am confused why you keep moving the code around into different order. This is how it should be.

Set up your database connection (Not 100% sure, but, I think that is what the dog.php is for.
(Normally we would call that config.php or db_connect.php, something that makes you think of DB set up.)

Next, get the form reference number which you did.

Now, load all of the fields of data which is done with a mysqli_fetch_assoc() function not a fetch_fields() function. You moved this part down into the PDF code, too. WHY? It needs to be before you create your PDF.

Then, load the template of the form. For some odd reason you moved it into the PDF code section.
After loading the template which is housed in the $new_form variable, you then replace the code words in the template with the live fields. For some reason you moved this section down into the PDF code section.

Lastly, with the template now created and all of the keywords replaced with live data, you use the PDF library to create the PDF for you. And, of course send it to the browser so the user can review the finished form.

So, please rewrite the order of your code and repost the new version and it should work.

Oh, also, the error you showed us is because you used OOP style of coding the first part of your query and then when you get the data, you did not use OOP. (Object Orientated Programming) So, your query would need to be changed. Something like this:

$db->query("SELECT name, data FROM thetitl1_livesite754.form_data WHERE form_id IN(SELECT id FROM thetitl1_livesite754.forms WHERE reference_code ='.$refcode.')");

//Replaces form_fields in html temp with "data"
if ($db) {
//below is the line referenced in error message above     
while ($row = $db->fetch_array(MYSQLI_ASSOC)) {
        $new_form = str_replace($row["name"], $row["data"], $new_form);
    }
}

Notice the slight difference in the $row= part. The two types of programming these are called “Object Orientated Programming” and “Procedural Programming”. They are quite different. You started the DB connection and query using OOP ( $db->etc ) and created the $row’s using fetch($DB) which is the procedural version. Hope you understand the difference.

Go to it. Rewrite using my notes and repost the code when done. That should finish this project up for you I think! Good luck…

Hey @ErnieAlex, thanks again for all the help. Gonna work on getting this corrected as you’ve laid it out… My moving stuff around was my attempt at putting thing where I thought they needed to be for steps of processing. But since reading your explanation of the workflow, I can see what you mean. Busy weekend, probably for you too, huh? Have a good one though.

HEY @ErnieAlex,
WE HAVE SUCCESS! but only to a degree… yet success it is. So here is what is happening. Clicking the link and the form opens, but not in the browser. It opens in the browser and the form fields are not filled but still show the field names…

can i echo $results to see what’s created by the query before it actually renders, just as a test?

It isn’t giving me error messages so I’m assuming that it is pulling the query correctly…

I also noticed that the form is actually much smaller than the page and this will make the document unacceptable for the government agencies the customer will be dealing with. I’ve already tried removing the “set up paper size” to see if it would revert back using the original boundaries of the HTML template, but that didn’t work. My next notion was instead of loading an HTML template could I load a pdf form with the field names instead?

Besides that, I’m really excited to finally at least see a generated pdf. And I have all your help to thank for it…

here is a link to the form process gateway so you can see what I mean… use refcode: QKEX49MKDE

https://pdforms.losttitleconnection.net/TX%20Title%20App%20Gate.php

Sponsor our Newsletter | Privacy Policy | Terms of Service