Help with PDFtk function

Yes, for some strange reason it is not full screen like it should be. It does not open inside the browser because of the options used.

Well, show me your code and we can see what is up with it.

Also, yes, you can echo data at any time in your code to see what data it is using. If you want to see the live data that the query is pulling out, you can do it this way:

while ($row = mysqli_fetch_fields($db)) {
        echo $row["name"] . ":  " . $row["data"] . "<br>";
        $new_form = str_replace($row["name"], $row["data"], $new_form);

Or something simlar. It might mess up the display, but, for testing is just fineā€¦
Very glad you made some progress! Once this is done, you can fine-tune it for other forms if needed!

Hey @ErnieAlex
here is a copy of the code

<?php

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

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

$mysqli = new mysqli($server, $username, $password, $dbase);

$results = $mysqli->query("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 ($results) {
//below is the line referenced in error message above     
while ($row = $results->fetch_array(MYSQLI_ASSOC)) {
        $new_form = str_replace($row["name"], $row["data"], $new_form);
    }
}
// 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);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper(ā€˜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);    

?>

Been doing a little digging and I donā€™t think there is anything wrong with the actual code but more the HTML template file itself. It appears that the HTML converted files were created in an absolute position based format with backgrounds and all of this in a size smaller than an actual 8.5 x 11. When I open the HTML temp file in a browser and try to print, they output in preview on a smaller scale than 8.5 x 11paper size. So Iā€™m thinking the fix here needs to be to find a converter or way to convert pdf to html in an 8x11 format or convert to rich text, fill those and then render to pdf with dompdf. Would love to have your take on this, if this infact is the case.

PSā€¦ started doing a little research just in caseā€¦
https://www.uhp-software.com/de/blog-artikel/10/how-to-edit-word-document-templates-with-php-and-convert-it-to-pdf

This line is STILL in the wrong place! It must be moved up before the WHILE for the replacement area.
It should be something like this:

//Replaces form_fields in html temp with "data"
$new_form = file_get_contents("TX_Title_App.html"); 
if ($results) {
while ($row = $results->fetch_array(MYSQLI_ASSOC)) {
        $new_form = str_replace($row["name"], $row["data"], $new_form);
    }
}

And, not sure if you need to check for the ($results) in that spot. You really should move the PDF code into that also and indicate if there is a missing form. More like this:

//Replaces form_fields in html temp with "data"  if the form exists, if not indicate it to the user
if ($results) {
$new_form = file_get_contents("TX_Title_App.html"); 
while ($row = $results->fetch_array(MYSQLI_ASSOC)) {
        $new_form = str_replace($row["name"], $row["data"], $new_form);
    }

   // 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);

   // (Optional) Setup the paper size and orientation
   $dompdf->setPaper(ā€˜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);    
} else {
   echo "No form found for this reference number!";
}
?>

As far as the output being too small. Just edit the HTML version and set it to the correct size. In that code there must be a setting, most likely CSS that is making the page smaller. If you canā€™t see it, then I will look at it.

Man @ErnieAlex, Iā€™m gonna rip my hair outā€¦

So I decided to put it in that piece of code you gave me to test the query results:

<?php

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

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

$mysqli = new mysqli($server, $username, $password, $dbase);

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

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

//Replaces form_fields in html temp with "data"
if ($results) {
//below is the line referenced in error message above     
while ($row = mysqli_fetch_fields($results)) {
        echo $row["name"] . ":" . $row["data"] . "<br>";
 }
}
?>

all it gives me back is repeating " : " down the left side of the browser, but no data. I have to hit the back key for it to stop. Iā€™ve tested the validity of the query in phpmyadmin using a reference code instead of the variable and that returns the arrays of the two columns for name and data. Why itā€™s not doing it with the code is giving me a headacheā€¦

As for the HTML templates they are broken down into separate files. An HTML with absolute positions for each area of text and then jpeg for the form backgrounds. I have attempted to adjust the size of backgrounds and margins in the html file but that would mean I would have to individually adjust each and every absolute position as well and I canā€™t see doing that, just too tedious. I tried using Adobe to convert the file but that doesnā€™t even come out close to what the original pdf looks like. The only way it came out almost exact was by converting it to a rich text file. But that would mean yet more coding and another library altogether And that, if I have read correctly, may not even work due to the fact that Iā€™m on a shared server and library docs say I would need a virtual or dedicated account.

Gonna leave this alone for nowā€¦ take a brake and see what I can work out about converting the pdf into html of the original size. Iā€™m assuming cause Iā€™m using a free file converter it is giving users a false sense of hope by creating a conversion yet making it a smaller size. One doesnā€™t realize what they got until they go to use or print it.

Success turned out to be an obstacle hurdled and more to completeā€¦ but thatā€™s life and Iā€™m still determined to work the issues to completion.

change this code to this:

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

This will stop the program and show you the query that you are really using. Then, run that in your phpMyAdmin and see if the code version works in there. This is how you debug if your code is creating the correct query. Then, you can see why it is not pulling out the correct data.

Okay, take a deep breath and think ā€œthis is only codeā€ and it is no big deal. They call this debugging code actually. You are very close to finishing this up!

Just woke to see your messageā€¦ Iā€™ll try this and get back with you, off to dr an acupunture appt this morning. BTW, I think if I create jpegs of the original and then create html page with jpeg as background and then absolute position each field name that would be a good work around, what do you think. Talk soon, have a good day

I have tried it that way and it is much harder to do. I am sure you are just about done this way.
But, first find out what is going on with the query as you will need that either way!

Good luck with the doctors!

So Iā€™m gonna give you the error message Iā€™m getting and the posted code:

  1. This is what appears in the browser: SELECT name,data FROM thetitl1_livesite754.form_data WHERE form_id IN(SELECT id FROM thetitl1_livesite754.forms WHERE reference_code = ā€˜ā€™)

when using (reference_code = ā€˜$refcodeā€™)

<?php

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

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

$mysqli = new mysqli($server, $username, $password, $dbase);

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

$results = $mysqli->query($query);

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

//Replaces form_fields in html temp with "data"
if ($results) {
//below is the line referenced in error message above     
while ($row = mysqli_fetch_fields($results)) {
        echo $row["name"] . ":" . $row["data"] . "<br>";
 }
}
?>
  1. This is the message in the browser: Parse error : syntax error, unexpected ā€˜$refcodeā€™ (T_VARIABLE) in /home1/thetitl1/public_html/pdforms/TX Title App.php on line 11

when using ( reference_code = ā€œ$refcodeā€)

<?php

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

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

$mysqli = new mysqli($server, $username, $password, $dbase);

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

$results = $mysqli->query($query);

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

//Replaces form_fields in html temp with "data"
if ($results) {
//below is the line referenced in error message above     
while ($row = mysqli_fetch_fields($results)) {
        echo $row["name"] . ":" . $row["data"] . "<br>";
 }
}
?>

I tested this statement in phpmyadmin:

SELECT name, data FROM thetitl1_livesite754.form_data WHERE form_id IN(SELECT id FROM thetitl1_livesite754.forms WHERE reference_code =ā€˜QKEX49MKDEā€™)

and it returned this:

I hope you can tell me whatā€™s going wrongā€¦

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

Is not correct. Therefore it is not placing the $refcode in place! It needs some periods and quotes. You can not use a non-numeric value without the quotes. Do it like this:

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

You needed to ā€œconcatenateā€ the value between the quotes. Note there is a single quote then a double-quote, then the variable, then a double-quote and a single quote. This will make the value of the variable be enclosed in single quotes. The PHP line uses double-quotes around the entire query.

Hope that makes sense. Try itā€¦

Uust changed it and got this message againā€¦

SELECT name,data FROM thetitl1_livesite754.form_data WHERE form_id IN(SELECT id FROM thetitl1_livesite754.forms WHERE reference_code = ā€˜ā€™)

when using (reference_code = ā€˜" . $refcode . "ā€™)

Even tried it with out the spaces around the dots and still got the same result.

Well, that is with the die($query) in place. So, as you see the resulting query is missing the refcode.
Therefore, it is not taking it correctly from your posted value. So the problem is in this section:

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

It is not creating the $refcode correctly. My guess is that in the form where you have the input field,
it is spelled differently than ā€˜reference_codeā€™ā€¦ Check that.

For testing, you can add a line in that sets that variable manually to see if the PDF works right.
But, I suggest finding out why this is failing. So, you can do this:

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

And, see what it IS pulling from your posted form. I looked back to your first posts and it clearly shows the input field spelled correctly, but, something might have been changed. Perhaps you should post the entire test page? Or send it to me in a private messageā€¦

So I looked the gateway html code over and found that the html form code had method=ā€œgetā€ and changed it to method=ā€œpostā€ to match the php file code of

$refcode = filter_input(INPUT_POST, ā€˜reference_codeā€™);

looked over the rest for spelling and other possible errors, but still getting the same message.

Let me ask you this, I know you mentioned ā€˜prepared statementsā€™, would including those at this time do anything to make the situation betterā€¦

Well, that would add some minor security, but, not needed at this point!
So, the part that grabs the $refcode is what we need to fix. So, try this. It should respond with ***code***.

//query for reference_code from ā€˜formsā€™ table
$refcode = filter_input(INPUT_POST,ā€˜reference_codeā€™);
die("***" . $refcode . ā€œ***ā€);

I added the stars so you can see what was returned. If it shows ******, then you are not retrieving the code.
If it does get the code, you should see it in the middle of the stars.

If this works, then I do not understand why it is failing. You posted all of the code, correct? You donā€™t have
it inside a function anymore, correct? (If inside a function variables are local and not global variables.)

If this does not show the formā€™s code, then you need to repost everything so we can examine itā€¦

put this into my php file and got this back

" ****** "

this is a link to the document gateway:
https://pdforms.losttitleconnection.net/TX%20Title%20App%20Gate.php

And this is the reference code used for testing:
QKEX49MKDE

This is the code as is appears in the php file:

<?php

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

//query for reference_code from ā€˜formsā€™ table
$refcode = filter_input(INPUT_POST,ā€˜reference_codeā€™);
die("***" . $refcode . "***");

$mysqli = new mysqli($server, $username, $password, $dbase);

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

$results = $mysqli->query($query);

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

//Replaces form_fields in html temp with "data"
if ($results) {
//below is the line referenced in error message above     
while ($row = mysqli_fetch_fields($results)) {
        echo $row["name"] . ":" . $row["data"] . "<br>";
       $new_form = str_replace($row["name"], $row["data"], $new_form);
 }
}
?>

And this is the code on the html gateway

<html>
<body>
<form action="TX Title App.php" method="post">
<fieldset><legend>Final Details Reference Code</legend> <label for="reference_code">Enter Reference Code Here:</label> <input id="reference_code" name="reference_code" type="text" /></fieldset><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Also wanted to add the fact that the url address looks like this when returning the " ****** "
https://pdforms.losttitleconnection.net/TX%20Title%20App.php?reference_code=QKEX49MKDE

Well, that is the reason! You do not have all of this code in one page! You are using two pages.
The first one is POSTING the data to the second page.

The second page is GETTING the data!

So, from your last post, you have to change the code to read the GET data not the POST data.
In the second page, change the input filter to INPUT_GET, not INPUT_POST !

Jus did this

<form action="TX Title App.php" method="get">

and this

$refcode = filter_input(INPUT_GET,ā€˜reference_codeā€™);
die("***" . $refcode . "***");

and still got back " ****** "

Does $_GET have anything to do here?

You are using two files. So, in the first file, the one with the form on it, use the form method=ā€œPOSTā€ā€¦

The data gets sent to the second file in the format you showed. Therefore, the second file needs to use the INPUT_GET format.

Try that!

Iā€™ve tried all of these:
method = post and INPUT_GET
method = get and INPUT_POST
method = get and INPUT_GET
method = post and INPUT_POST

and still get back " ****** "

Does $_POST or $_GET need to be used somewhere

I just looked into it. You can NOT use either of these ways. I will explain.

Normally, you use POST. Normally, you post to the same page. If you post to another page, you pass parameters using SESSIONS or post thru the URLā€™s. Since you are using formā€™s, you would normally just place the code and processing for the form all in that one page.

Since you have only the entry of a reference number, there is no reason to use a second page. Do you have a special reason to use a second page? Here is the layout of the two ways possible.

First, put all the code into one page. The user enters a ref-code and presses save-pdf or view-pdf.
This would call itself to run all the PDF codes. If done this way, you would use POST only.

Second, use the first page to enter the ref-code and a second one to process it. This way requires the
creation of a URL which would contain the data needed to pass onto the second page. Then, it would
redirect to the second page. The second page would GET the data since it is already inside the URL.
Then, it would process the PDF code.

There is a third way actually that saves the data inside a SESSION variable. This is fairly easy as you would just pass the ref-code in the session variables. But, for testing the first or second version would work.

Now, did you understand that? Pick which way you want to process and I will help you with the code.
As-is now, we would have to rewrite how the ref-code is sent to the second page. Not hard to do.

Sponsor our Newsletter | Privacy Policy | Terms of Service