<?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());
}
?>
<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>
<?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/>";
}
}
}
?>
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
)
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.
<?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;
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.
<?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>
Working sample here
That shows the difference in rendering qualityNB: 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.
<?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>';
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?