PHP: Filling Out/Creating a PDF

I am not sure how to accomplish this...

I have been tasked with automating my employer's car seat rental form. Is PHP capable of filling out a predesigned PDF that was created in Adobe? Or is it easier to create/generate a PDF file at run-time?

Either way, the PDF will need to be emailed to a few different people and the renter's information will need to be stored in a MySQL database.
Joseph LongoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Peos JohnPHPCommented:
Is PHP capable of filling out a predesigned PDF that was created in Adobe?
I hope this not possible. But please let us know more about this to understand this better.

Or is it easier to create/generate a PDF file at run-time

Yes, It possible to create PDF file using FPDF library. https://www.fpdf.org/

Either way, the PDF will need to be emailed to a few different people and the renter's information will need to be stored in a MySQL database.

You can create a php page with form and send the link to the user, so that it is easy to get the details and store them in database.
 Is there any reason why you would like to sent the form as PDF file?
0
Joseph LongoAuthor Commented:
Here is an example of the rental form:
Form
I plan on using a PHP form and a MySQL database. Should I use HTML tables to recreate the form, and then have PHP plug-in the information from the database, after storing it? I kind of wanted it to be in a PDF format, so the renter cannot modify the rental agreement. Is it possibly to store the user's information, create the form using HTML tables, email the appropriate people and then echo the form out to the browser all in the same click? Or should I have the script store the user's information in one click, create the rental agreement with another button and then email the appropriate people with a third button?
0
Peos JohnPHPCommented:
Thanks for the posting that, Since you want to store the renter's information in the database.

1.  You can create a php form where the renter can fill in all the details that you have show in the screen sample form.

For exp: Name, Address, Phone number etc.

2.  When you send this form link to this renter you can also send the PDF file(Rental Agreement). This can be manually signed and uploaded in the same form link you are sending.

3. So the php form you are sending will have 'browse' button to upload the rental agreement.

4. Usually when the files are uploaded using the forms, we upload the files in any folder, and store file path in the database.

All this can be done on a single php form.
1
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Joseph LongoAuthor Commented:
The user won't have much interaction with the form, other than receiving a copy of it, after it's been completed. The form will be filled out by the receptionist, and then emailed to herself, the renter and the fiscal department.

Same process as you mentioned or would things change?
0
Ray PaseurCommented:
Here's my teaching example of the "Hello World" exercise with a client HTML form and output to a PDF document.
https://iconoun.com/demo/fpdf_form_to_pdf.php
<?php // demo/fpdf_hello_world.php
/**
 * Demonstrate the basics of an HTML form to populate information in a PDF with FPDF
 *
 * http://www.fpdf.org/
 * http://www.fpdf.org/en/doc/index.php
 *
 * See also: https://www.setasign.com/products/fpdi/about/ (untested, but looks promising)
 */
error_reporting(E_ALL);


// IF THERE IS A POST-METHOD REQUEST
if (!empty($_POST))
{
    // GET THE EXTERNAL REQUEST VARS FROM THE HTML FORM
    $my_data = !empty($_POST['my_data']) ? substr(trim($_POST['my_data']),0,10) : 'NOTHING';

    // LOAD THE FPDF CLASSES
    require_once('fpdf16/fpdf.php');

    // SYNTHESIZE THE PDF FILE INFORMATION
    $pdf_file_link
    = 'storage'
    . DIRECTORY_SEPARATOR
    . 'temp_pdf'
    . '.pdf'
    ;
    $pdf_file_name
    = getcwd()
    . DIRECTORY_SEPARATOR
    . $pdf_file_link
    ;

    // SOME VARIABLES FOR OUR TESTS (COULD COME FROM HTML FORMS, DATA BASE, ETC)
    $font = 'Arial';

    // DO THE HELLO WORLD EXERCISE IN WHITE ON A BLUE BACKGROUND
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont($font, 'B', 16);
    $pdf->SetFillColor(  0,   0, 255);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(40, 10, $my_data, 0, 2, 'L', TRUE);

    // WRITE THE PDF TO DISK
    $pdf->Output($pdf_file_name, 'F');

    // PRESENT A LINK
    echo '<a target="my_PDF" href="' . $pdf_file_link . '">See the PDF with ' . $my_data . '</a>';
}

// PUT UP A FORM TO CAPTURE CLIENT INPUT
$form = <<<EOF
<form method="post">
What do you want in the PDF?
<input name="my_data" placeholder="Put it here" />
<input type="submit"  value="make PDF" />
</form>
EOF;

echo $form;

Open in new window

1
Peos JohnPHPCommented:
O.K. got it.  

It will be the same process, so your php form will have will have only the browse button to upload the PDF file.

Additionally, if you can keep any unique value (Something like Email address of renter, invoice number) to know who has uploaded the file.

Here is the sample code:

Database connection:
<?php
$servername = "hostname";
$username = "username";
$password = "password";
$dbname = "your_databasename";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}


?>

Open in new window



HTML form where you have the browse button to upload the file

<h2>Upload Rental Agreement(PDF) :</h2>


<form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF']?>" method="post">


<p>

Renter's Email : <input 	type="text" name="email" /><br /><br />

Inovice Number : <input 	type="text" name="invoice_number" /><br /><br />


<input type="hidden" name="MAX_FILE_SIZE" value="200000" /> 
<input 	type="file" name="pdfFile" /><br /><br />


<br />
<input type="submit" value="Click here to upload" /></p>
</form>

Open in new window


Code to upload the PDF file and Insert the information in Mysql table

<?php
if ( isset( $_FILES['pdfFile'] ) ) 
{
	if ($_FILES['pdfFile']['type'] == "application/pdf")
	{
		$source_file = $_FILES['pdfFile']['tmp_name'];
		$dest_file = "upload/".$_FILES['pdfFile']['name'];//YOUR UPLOAD FOLDER

		if (file_exists($dest_file)) 
		{
			print "The file name already exists!!";
		}
		else 
		{
			move_uploaded_file( $source_file, $dest_file )
			or die ("Error!!");
			if($_FILES['pdfFile']['error'] == 0) {
				print "Pdf file uploaded successfully!";
				print "<b><u>Details : </u></b><br/>";
				print "File Name : ".$_FILES['pdfFile']['name']."<br.>"."<br/>";
				print "File Size : ".$_FILES['pdfFile']['size']." bytes"."<br/>";
				print "File location : upload/".$_FILES['pdfFile']['name']."<br/>";
				
				$pdf_file_name = $_FILES['pdfFile']['name'];
				
				
				//INSERT FILE INFORMATION TO DATABASE				
 				$sql = "INSERT INTO tbl_rental_agreement(email,invoice_number,rental_agreement) VALUES ('".$_POST['email']."', '".$_POST['invoice_number']."', '".$pdf_file_name."')";

				if (mysqli_query($conn, $sql)) {
				echo "Record inserted to DB";
				} else {
				echo "Error: " . $sql . "<br>" . mysqli_error($conn);
				}

				mysqli_close($conn);	
				
				
			}
		}
	}
	else 
	{
		if ( $_FILES['pdfFile']['type'] != "application/pdf") 
		{
			print "Error occured while uploading file : ".$_FILES['pdfFile']['name']."<br/>";
			print "Invalid  file extension, should be pdf !!"."<br/>";
			print "Error Code : ".$_FILES['pdfFile']['error']."<br/>";
		}
	}
}
?>

Open in new window


Mysql Table to store the detail with file name

CREATE TABLE `tbl_rental_agreement` (
  `id` int(11) NOT NULL,
  `email` varchar(250) NOT NULL,
  `invoice_number` varchar(100) NOT NULL,
  `rental_agreement` text NOT NULL
)

Open in new window



Once the file is uploaded you access the file using folder_path/file_name.pdf

And the form will look something like this

upload_pdf.png
1
Ray PaseurCommented:
Please, folks!  Don't keep telling PHP learners to use die() and especially not with meaningless error messages.  It's 2017, and we know better today!  Here is how it's done in the modern millennium.
https://www.experts-exchange.com/articles/29115/PHP-Error-Handling-Never-Say-die-Again.html

As to the part about getting a PDF uploaded - that may be possible, but the implementation detail that is going to be the central issue will be the process of getting client information into the PDF.  In my experience with FPDF and TCPDF it was easier to build the PDF entirely, rather than try to upload a pre-existing PDF and then try to put data into it.
2
Julian HansenCommented:
The process is simple

Create a PDF from a blank form.

Using a graphics program find the offsets of the entry fields.

Using a tool like FPDF import the blank form PDF and use the offset data to overlay the form data on top of the imported PDF - outputting to a new PDF.

We use this all the time for producing a variety of PDF outputs.
2
Joseph LongoAuthor Commented:
Julian,

This process might be simple to you, but it is foreign to me. Can you describe the process a little more in-depth? Would I create the blank PDF form in Adobe, first without any information/data on the form? Import the PDF into a graphics program to find the offset entry field? How would one find out the offsets?

Do you have an example program?

Peos John,

I appreciate your sample program, but it's not quite the process I am looking for. I will upload a pic of my HTML form in a little while.
0
Joseph LongoAuthor Commented:
So, here is a VERY simple HTML form:
HTML Form
Upon clicking submit, the data will be "processed" and sent to a MySQL database. After the data has been inserted into the database, the page will refresh and a "generate rental agreement" button will appear. Upon clicking the "generate rental agreement", the php script will obtain the necessary information from the MySQL database, such as "invoice number", etc.

The finished PDF file/product will look like this:
Rental Form-Completed
I am like 85% confident that I know how to make EVERYTHING work, except creating the PDF file with the tables as seen in my embedded image. If there are things I am not thinking about or am overcomplicating this process, please let me know.
0
Peos JohnPHPCommented:
Hi Joseph Longo,

Your steps are clear and you are on the right track, It is possible using FPDF as mentioned previous comments.

After the data has been inserted into the database, the page will refresh and a "generate rental agreement" button will appear.
When click  "generate rental agreement" button  you can redirect to the page where you will have the code to create PDF.

Here you can fetch the data from mysqland start populating into FPDF fucntion, please checkout the links below

http://www.fpdf.org/en/tutorial/tuto5.php 

http://www.fpdf.org/en/tutorial/tuto5.php

And you can get the last  inserted id using mysqli_insert_id(connection);
1
Julian HansenCommented:
When you get into it you will find it is really simple :) Unfortunately, I am working on a project that is very similar to this and I am under an NDA so I can only provide general guidance.

Would I create the blank PDF form in Adobe, first without any information/data on the form?
Yes - blank form => PDF

How would one find out the offsets?
This is a bit more complicated - you need find this out by trial and error but once you have the factor for one field the rest follow.

The FPDF library is perfectly adequate for this. You load the PDF (setSourceFile), import the page (importPage), set the template size and orientation, useTemplate to use the above.

Then use Text() to output your text to the desired locations (which can come from the DB) - repeat for all fields then output the result with Output()

There are options to change fonts, colours and sizes but you can find those in the FPDF API
1
Ray PaseurCommented:
Here's a PDF I made using your uploaded form.  Obviously I had to overwrite the existing text in the document, so I did that in red to make it stand out.
temp_pdf.pdf
1
Ray PaseurCommented:
Here's the script I used to make the PDF.  Most of the constants, such as image resize values and X:Y coordinates were arrived at through trial and error.  The PNG image we started with is too big to fit into a standard Letter-size Landscape document, and FPDF did not like its transparency, so I resized it and sharpened it a little bit to make it fit and look as good as possible.  You could store a smaller image and dispense with this part of the exercise.

I found explicit placement of the text to be the most dependable and straightforward way to get good positioning.

Just an opinion, but I think FPDF is getting a little "long in the tooth" as we say about horses.  You might find TCPDF to be more actively maintained and better documented. Similar principles will apply.
https://github.com/tecnickcom/TCPDF

HTH, ~Ray
<?php // demo/temp_joseph_longo.php
/**
 * https://www.experts-exchange.com/questions/29010988/PHP-Filling-Out-Creating-a-PDF.html#a42063532
 *
 * Demonstrate the basics of an HTML form to populate information in a PDF-image with FPDF
 *
 * http://www.fpdf.org/
 * http://www.fpdf.org/en/doc/index.php
 * http://www.fpdf.org/en/doc/image.htm
 */
error_reporting(E_ALL);

// THE URL OF THE BLANK FORM
$url = 'https://filedb.experts-exchange.com/incoming/2017/03_w12/1152824/Rental-Agreement-Form.png';

// A PLACE TO STORE THE INTERMEDIATE IMAGE
$png = 'storage/temp_joseph_longo.png';

// DETERMINE THE DESIRED SIZE OF THE IMAGE IN THE DOCUMENT (TRIAL AND ERROR)
$img = imageCreateFromPNG($url);
if (!$img) trigger_error("Unable to read $url", E_USER_ERROR);
$img_w = imagesX($img);
$new_w = $img_w / 2.8;
$img_h = imagesY($img);
$new_h = $img_h / 2.8;

// MAKE A NEW IMAGE TO RESIZE AND REMOVE THE ALPHA-TRANSPARENCY CHANNEL
$new = ImageCreateTrueColor($new_w, $new_h);
ImageCopyResampled
( $new        // DESTINATION IMAGE RESOURCE
, $img        // SOURCE IMAGE RESOURCE
, 0           // DESTINATION X
, 0           // DESTINATION Y
, 0           // SOURCE X
, 0           // SOURCE Y
, $new_w      // DESTINATION WIDE
, $new_h      // DESTINATION HIGH
, $img_w      // SOURCE WIDE
, $img_h      // SOURCE HIGH
)
;

// SHARPEN THE NEW IMAGE
$sharpenMatrix = array
( array( -1.2, -1.0, -1.2 )
, array( -1.0, 20.0, -1.0 )
, array( -1.2, -1.0, -1.2 )
)
;
$divisor = array_sum(array_map('array_sum', $sharpenMatrix));
$offset  = 0;
imageConvolution($new, $sharpenMatrix, $divisor, $offset);

// CREATE THE NEW IMAGE THAT WILL BE USED IN THE PDF
imagePNG($new, $png);
imageDestroy($img);
imageDestroy($new);


// IF THERE IS A POST-METHOD REQUEST
if (!empty($_POST))
{
    // GET THE EXTERNAL REQUEST VARS FROM THE HTML FORM
    $rn = !empty($_POST['rn']) ? substr(trim($_POST['rn']),0,32) : 'NO NAME';
    $an = !empty($_POST['an']) ? substr(trim($_POST['an']),0,32) : 'NO ADDRESS';

    // LOAD THE FPDF CLASSES
    require_once('fpdf16/fpdf.php');

    // SYNTHESIZE THE PDF FILE INFORMATION
    $pdf_file_link
    = 'storage'
    . DIRECTORY_SEPARATOR
    . 'temp_pdf'
    . '.pdf'
    ;
    $pdf_file_name
    = getcwd()
    . DIRECTORY_SEPARATOR
    . $pdf_file_link
    ;

    // USE THIS FONT FOR THE PDF
    $font = 'Arial';

    // DO THE WRITING IN RED TO MAKE IT OBVIOUS
    $pdf = new FPDF();
    $pdf->SetMargins(0, 0);
    $pdf->AddPage('L', 'Letter');
    $pdf->SetFont($font, 'B', 18);
    $pdf->SetTextColor(0xCC, 0x00, 0x00);
    $pdf->Image($png, 0, 0);

    // SET THE THE RENTER'S NAME ON THE DOCUMENT
    $x = 50;
    $y = 30;
    $pdf->SetXY($x, $y);
    $pdf->Write(0, $rn);

    // SET THE THE RENTER'S ADDRESS ON THE DOCUMENT
    $x = 50;
    $y = 38;
    $pdf->SetXY($x, $y);
    $pdf->Write(0, $an);

    // WRITE THE PDF TO DISK
    $pdf->Output($pdf_file_name, 'F');

    // PRESENT A LINK
    echo '<a target="my_PDF" href="' . $pdf_file_link . '">See the PDF with ' . $rn . '</a>';
}


// PUT UP A FORM TO CAPTURE CLIENT INPUT
$form = <<<EOF
<form method="post">
What do you want in the PDF?
<br>
<input name="rn" placeholder="Renter Name" />
<br>
<input name="an" placeholder="Renter Address" />
<br>
<input type="submit"  value="make PDF" />
</form>
EOF;

echo $form;

Open in new window

1
Joseph LongoAuthor Commented:
Ray Paseur,

Here is the blank PDF I intend on using. Some fields, such as security deposit and total, will be auto-calculated, depending on the amount of car seats and the term(s) the renter intends on renting the car seat(s) for. One feature I like about Adobe is that it has an "auto" size field for the font. Is it possible to accomplish this with FPDF?

Rental-Agreement-Form-without.pdf

Is it possible to "check" the check boxes, too?
0
Ray PaseurCommented:
I don't think there will be any auto-size in FPDF.  And it would be easier, I think, to start with an image instead of a PDF (maybe Julian has an example that starts with a PDF, but I don't). Other than that, I think all things are more-or-less possible, but telling you that would not be as useful as having an experienced PHP pro work the issues and give you a solution.  Consider creating a project in E-E Gigs.
1
Julian HansenCommented:
From here https://github.com/tecnickcom/TCPDF
A new version of this library is under development at https://github.com/tecnickcom/tc-lib-pdf and as a consequence this version will not receive any additional development or support. This version should be considered obsolete, new projects should use the new version as soon it will become stable.
I use FPDF as I found it the most versatile for what I want to do - specifically importing other PDF's for the purpose of overlaying data.

The PDF supplied uses forms. I have not explored this option but apparently FPDF has a form module that will do this http://www.fpdf.org/en/script/script93.php which references FPDM https://github.com/madnh/FPDM

There is also PDFtk which is a separate system that can be invoked by PHP https://www.sitepoint.com/filling-pdf-forms-pdftk-php/
1
Ray PaseurCommented:
@Julian: If you follow that part about "A new version of this library is under development..." you will see that the link I posted is the current level and the "new version" is not quite ready for prime time yet.  This often happens with open-source projects when they are built by a small community; not everything is 100% "perfect" at all times.  Contrast WordPress, where the community is huge.  I've never used PDFtk, so I can't comment on that.

Anyway, I think the Author will be best served by the E-E Gigs project or maybe by E-E Live.  But if you can show a simple working example that starts by importing his sample PDF, that might save him some money!
1
Ray PaseurCommented:
This expert suggested creating a Gigs project.
Sorry, I forgot to add the link to E-E Gigs.  Here you go...
1
Julian HansenCommented:
Here is the code
<?php
require_once "fpdf/fpdf.php";
require_once "fpdi.php";
require_once "fpdf_tpl.php";

$fpdi = new FPDI();
$font = new stdClass;
$font->family = 'Arial';
$font->size = 16;
$font->weight = '';

$fpdi->setSourceFile('t2235.pdf');
$template = $fpdi->importPage(1);
$fpdi->templateSize   = $fpdi->getTemplateSize($template);
$fpdi->templateCenter = $fpdi->templateSize['w'] >> 1;
$orientation = 'P';
$fpdi->AddPage($orientation);
$fpdi->useTemplate($template, 0, 0);

$fpdi->SetFont($font->family, $font->weight, $font->size);

// Assumes an array of PDF text items made up of
// x: x offset of start of text
// y: y offset of top of text
// text: text to output

if ($pdfitems = getPDFItems()) {
	foreach($pdfitems as $item) {
		$fpdi->Text($item->x,$item->y,$item->text);
	}

	$clientnumber = rand(1000,2000);
	$filename = "Rental-Agreement-Form-{$clientnumber}.pdf";

	$fpdi->Output($filename, 'D');
}
/*
** Emulate getting data for PDF by getting from POST
*/
function getPDFItems()
{
	$name = isset($_POST['name']) ? $_POST['name'] : false;
	
	if ($name) {
		$items = array();
		$newitem = new stdClass;
		$newitem->text = $name;
		$newitem->x = 24;
		$newitem->y = 17;
		
		$items[] = $newitem;
	}
	else {
		$items = false;
	}
	
	return $items;
}
?>
<!doctype html>
<html>
<body>
<form method="post">
	<label>Full Name</label><input type="text" name="name" /> <input type="submit" />
</form>
<a href="t2235.pdf">Sample input PDF</a>

</body>
</html>

Open in new window

Working sample here
1
Ray PaseurCommented:
That shows the difference in rendering quality.  If you start with a PNG it will probably look cleaner.  But in theory, you should be able to get a nearly perfect looking document if you create the entire PDF from PHP programming with FPDF.  The library has Line, Rect, and Cell methods that would seem to cover the form layout, and you can write anything you want on the PDF object, using a variety of fonts and colors, as we have shown.  To check a box, you might simply write the letter "X" at the appropriate coordinates, something like this:
    // CHECK A CHECKBOX ON THE DOCUMENT
    $x = 110.3;
    $y = 138.8;
    $pdf->SetXY($x, $y);
    $pdf->Write(0, 'X');

Open in new window

Another alternative might be to layer the images, adding a semi-transparent checkmark at the appropriate coordinates.

My experience with this library is several years old, and it hasn't changed a bit!  The layout part is a plodding exercise in meticulous attention to detail (sort of like undergraduate organic chemistry), but once you have the layout correct, the library is lightning fast and accurate to within 1/100 inch.  It's that "plodding exercise" that you might want to short-circuit with a Gigs engagement.
1
Julian HansenCommented:
That shows the difference in rendering quality
NB: The image used was taken from the scanned image the author submitted and then using Gimp to take out the writing. In reality you create the clean PDF template - and there is NO quality loss over creating the template from scratch with PHP.

I strongly recommend you DO NOT create from scratch - there is no benefit to this whatsoever - quality is identical and you don't have to go through the pain of layout. Layout has already been done - simply dump that to a PDF and use it as the background - obviously don't use a photo-shopped (gimp'd) scanned image - I used that as I did not have access to any other template.
1
Ray PaseurCommented:
PHP7 note for FPDF.  PHP7 now warns about PHP4-style constructors, and you may find that FPDF has the old constructor (method name == class name).  It's an easy fix - just change the FPDF method name to __construct() and you'll get past the warning.  The properties and methods do not declare their visibility, which means they are all public.  The deprecation of the var keyword was voted down last year, so it will remain almost the same meaning as public.

When I've used FDPF before, I did all of the document layout with the class methods, and I didn't find it too hard - just time consuming the first time through, and since you have to get the positioning right for the content, positioning the template is not much different.  Here's a script fragment and a link to the resulting document showing how I've done it.

Link to PDF: https://iconoun.com/demo/storage/temp_pdf.pdf
<?php // demo/temp_joseph_longo.php
/**
 * https://www.experts-exchange.com/questions/29010988/PHP-Filling-Out-Creating-a-PDF.html#a42063532
 *
 * Demonstrate the basics of an HTML form to populate information in a PDF-image with FPDF
 *
 * http://www.fpdf.org/
 * http://www.fpdf.org/en/doc/index.php
 * http://www.fpdf.org/en/doc/image.htm
 */
error_reporting(E_ALL);


// PRETEND TO GET THE EXTERNAL REQUEST VARS FROM THE HTML FORM
$rn = 'NO NAME';
$an = 'NO ADDRESS';


// LOAD THE FPDF CLASSES
require_once('fpdf16/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'storage'
. DIRECTORY_SEPARATOR
. 'temp_pdf'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// USE THIS FONT FOR THE PDF
$font = 'Arial';

$pdf = new FPDF();
$pdf->SetMargins(0, 0);
$pdf->SetDrawColor(0);
$pdf->AddPage('L', 'Letter');

// FOR CAPTIONS
$pdf->SetFont($font);

// BORDER AROUND DOCUMENT IS EQUIVALENT TO FOUR LINES
//  $pdf->Line(  4,   4, 275,   4);  // TOP
//  $pdf->Line(  4,   4,   4, 212);  // LEFT
//  $pdf->Line(275,   4, 275, 212);  // RIGHT
//  $pdf->Line(  4, 212, 275, 212);  // BOTTOM
$pdf->Rect(4,4,272,208);

// BOXES FOR NAME, ADDRESS, PHONE
$pdf->Rect(  4,  4,  272/2, 12);
$pdf->Rect(  4,  4,  272/2, 24);
$pdf->Rect(  4,  4,  272/2, 36);
$pdf->Rect(  4,  4,  272/2, 48);

// CAPTIONS FOR NAME, ADDRESS, PHONE
$pdf->setXY( 5, 8 );
$pdf->Write(0, 'Name');
$pdf->setXY( 5, 20);
$pdf->Write(0, 'Address');
$pdf->setXY( 5, 44);
$pdf->Write(0, 'Phone No.');

// BOXES FOR TERMS AND DATES
$pdf->Rect(275-64, 16, 65, 12);
$pdf->Rect(275-64, 16, 65, 24);
$pdf->Rect(275-64, 16, 65, 36);

// BOX FOR A CHECKMARK
$pdf->Rect(110, 140,  8, 8);


// DO THE WRITING IN RED TO MAKE IT OBVIOUS
$pdf->SetFont($font, 'B', 18);
$pdf->SetTextColor(0xCC, 0x00, 0x00);


// SET THE THE RENTER'S NAME ON THE DOCUMENT
$x = 24;
$y = 10;
$pdf->SetXY($x, $y);
$pdf->Write(0, $rn);

// SET THE THE RENTER'S ADDRESS ON THE DOCUMENT
$x = 24;
$y = 22;
$pdf->SetXY($x, $y);
$pdf->Write(0, $an);

// CHECK A CHECKBOX ON THE DOCUMENT
$x = 111;
$y = 144;
$pdf->SetXY($x, $y);
$pdf->Write(0, 'X');

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">See the PDF with ' . $rn . '</a>';

Open in new window

1
Julian HansenCommented:
We are getting too focused on FPDF here - it is a diverse library that can do N things - we only want it to do a very small subset of that

1. Load a PDF page to use as a template
2. Overlay text on it

There are other PDF libraries out there that do the same thing. I made it clear at the start of this thread I am limited by NDA on what I can provide. I was asked to provide code - I provided code that demonstrates the concept.

If FPDF does not work for you then there are other libraries to choose from - however I believe the code provided demonstrates quite clearly the principle of the solution.
1
Ray PaseurCommented:
Agreed - we could probably do this lots of different ways, but I think FPDF is a good enough solution, and it's almost as easy to create the PDF from FPDF commands as it is to try to get a credible image by loading an external PDF.  I've gotten good results when loading PNG images, but aside from that, I've gotten the best results by letting FPDF do all the work.
https://iconoun.com/demo/temp_joseph_longo.php

In any case, this question is well-enough answered that our Author can choose a path and proceed, or can show the question and comments to a Gigs engagement.  

Over and out...
1
Joseph LongoAuthor Commented:
Awesome!

I apologize. I haven't updated this post in a few days. I have been busy with work, church and other projects.

Ray,

For the image you created, which program did you use? Did you use Photoshop or a similar program to create a PNG image first, and input the data onto the PNG image, using fpdf?
0
Ray PaseurCommented:
Hi, Joseph.  I used FPDF.  Here's my reasoning.

It's a bit laborious, but fairly easy to get the alignment right.

The image quality in the PDF is as good as you can get.

You're going to have to get the alignment figured out anyway so you know the X,Y coordinates for the information you're copying out of the request (like $_POST) into the PDF.

You can keep everything in one PHP script (maybe make a class to extend FPDF with the Line() and Rect() functions).

Here's the source code.  Just add your HTML form and a little PHP to position the request variables.
<?php // demo/temp_joseph_longo.php
/**
 * https://www.experts-exchange.com/questions/29010988/PHP-Filling-Out-Creating-a-PDF.html#a42063532
 *
 * Demonstrate the basics of an HTML form to populate information in a PDF-image with FPDF
 *
 * http://www.fpdf.org/
 * http://www.fpdf.org/en/doc/index.php
 * http://www.fpdf.org/en/doc/image.htm
 */
error_reporting(E_ALL);


// PRETEND TO GET THE EXTERNAL REQUEST VARS FROM THE HTML FORM
$rn = 'NO NAME';
$an = 'NO ADDRESS';


// LOAD THE FPDF CLASSES (LATEST RELEASE 1.8.1 IN DECEMBER 2015)
require_once('fpdf181/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'storage'
. DIRECTORY_SEPARATOR
. 'temp_pdf'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// USE THIS FONT FOR THE PDF
$font = 'Arial';

$pdf = new FPDF();
$pdf->SetMargins(0, 0);
$pdf->SetDrawColor(0);
$pdf->AddPage('L', 'Letter');

// FOR CAPTIONS
$pdf->SetFont($font);

// BORDER AROUND DOCUMENT IS EQUIVALENT TO FOUR LINES
//  $pdf->Line(  4,   4, 275,   4);  // TOP
//  $pdf->Line(  4,   4,   4, 212);  // LEFT
//  $pdf->Line(275,   4, 275, 212);  // RIGHT
//  $pdf->Line(  4, 212, 275, 212);  // BOTTOM
$pdf->Rect(4,4,272,208);

// BOXES FOR NAME, ADDRESS, PHONE
$pdf->Rect(  4,  4,  272/2, 12);
$pdf->Rect(  4,  4,  272/2, 24);
$pdf->Rect(  4,  4,  272/2, 36);
$pdf->Rect(  4,  4,  272/2, 48);

// CAPTIONS FOR NAME, ADDRESS, PHONE
$pdf->setXY( 5, 8 );
$pdf->Write(0, 'Name');
$pdf->setXY( 5, 20);
$pdf->Write(0, 'Address');
$pdf->setXY( 5, 44);
$pdf->Write(0, 'Phone No.');

// BOXES FOR TERMS AND DATES
$pdf->Rect(275-64, 16, 65, 12);
$pdf->Rect(275-64, 16, 65, 24);
$pdf->Rect(275-64, 16, 65, 36);

// CAPTIONS FOR TERMS AND DATES
$pdf->SetXY(275-64, 8);
$pdf->Write(0, 'Invoice No');
$pdf->setXY(275-64, 20);
$pdf->Write(0, 'Terms');
$pdf->setXY(275-64, 32);
$pdf->Write(0, 'Receive Date');
$pdf->setXY(275-64, 44);
$pdf->Write(0, 'Return Date');

// BOXES FOR COLUMNS IN MIDDLE OF DOCUMENT
$pdf->Rect(  4,  60, 272, 212-60);
$pdf->Rect(  4,  60, 272,     96);
$pdf->Rect(  4,  70, 272,     86);
$pdf->Rect(  4,  60,  28,     96); // DATE
$pdf->Rect(  4,  60,  40,     96); // QTY
$pdf->Rect(  4,  60,  80,     96); // SERIAL NUMBER
$pdf->Rect(  4,  60, 275-68,  96); // DESCRIPTION

// CAPTIONS FOR COLUMNS
$pdf->setXY(12, 66);
$pdf->Write(0, 'Date');
$pdf->setXY(34, 66);
$pdf->Write(0, 'Qty');
$pdf->setXY(48, 66);
$pdf->Write(0, 'Serial Number');
$pdf->setXY(132, 66);
$pdf->Write(0, 'Description');
$pdf->setXY(275-40, 66);
$pdf->Write(0, 'Amount');

// BOXES FOR A CHECKMARK
$pdf->Rect(100, 144,  8, 8);
$pdf->Rect(120, 144,  8, 8);

// CAPTIONS FOR CHECKMARK BOXES
$pdf->SetXY(86, 140);
$pdf->Write(0, 'Paid by:');
$pdf->SetXY(88, 148);
$pdf->Write(0, 'Cash');
$pdf->SetXY(110, 148);
$pdf->Write(0, 'Chk');
$pdf->SetXY(128, 148);
$pdf->Write(0, '#______');

// BOXES FOR SECURITY DEPOSIT AND TOTAL
$pdf->Rect(275-64,    212-56, 65, 16);
$pdf->Rect(275-64-65, 212-56, 65, 16);
$pdf->Rect(275-64,    212-40, 65, 16);
$pdf->Rect(275-64-65, 212-40, 65, 16);

// CAPTIONS FOR SECURITY DEPOSIT AND TOTAL
$pdf->SetXY(275-64-65+8, 212-56+8);
$pdf->Write(0, 'Security Deposit');
$pdf->setXY(275-64-65+8, 212-40+8);
$pdf->Write(0, 'TOTAL');

// ADDITIONAL CAPTIONS
$pdf->setXY(14, 212-44);
$pdf->Write(0, 'Security deposit will be returned in seven business days');
$pdf->setXY(5, 212-23);
$pdf->Write(0, 'Drivers License #');
$pdf->setXY(5, 212-17);
$pdf->Write(0, 'State');


// DO THE WRITING IN RED TO MAKE IT OBVIOUS
$pdf->SetFont($font, 'B', 18);
$pdf->SetTextColor(0xCC, 0x00, 0x00);


// SET THE THE RENTER'S NAME ON THE DOCUMENT
$x = 24;
$y = 10;
$pdf->SetXY($x, $y);
$pdf->Write(0, $rn);

// SET THE THE RENTER'S ADDRESS ON THE DOCUMENT
$x = 24;
$y = 22;
$pdf->SetXY($x, $y);
$pdf->Write(0, $an);

// CHECK A CHECKBOX ON THE DOCUMENT
$x = 101;
$y = 148;
$pdf->SetXY($x, $y);
$pdf->Write(0, 'X');

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">See the PDF with ' . $rn . '</a>';

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joseph LongoAuthor Commented:
I am getting closer to achieving my end result. I have began create the PDF and laying out the document accordingly. It is a lot easier than I originally thought it was going to be.

Ray,

In your code, I have a question. How or where did you come up with the number 272? Was that kind of just a trial and error number, or did you calculate the width of the letter size in millimeters?
0
Ray PaseurCommented:
The FPDF constructor assumes the values are given in millimeters, unless you tell it otherwise, so yes, the number 272 was a distance in millimeters.  That does not eliminate some trial and error, but it makes the measurements more predictable.

FWIW, 8.5" x 11" is pretty close to 216mm x 280mm. so a width of 272mm, starting from a left margin of 4mm gives a 4mm empty border on both sides of an 11" wide document.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.