Database PDF Manafacture
Their comes a time when people want stuff printed, and no matter what you do, it will still have to be printed, and if your running big events, then that printing can get pretty troublsome, I was involved in building a little bit of a site, where we wanted to reduce people input in to generating printed documents. Essentially taking the copy and pasting out of the job, and as the posh people say “structering the data”.
So this meant HTML form to PHP to Databases and then to PDF’s
I used PDF’s as webpages don’t normally print very well and PDF’s are almost everywhere, they give a nice printed page, which you can print, and the most important thing is people understand them.
Tools used
- jquery
- PHP
- MySQL
- tcpdf
- HTML
The jquery was use in form validation, as show in a earler post http://patrickrice.net/blog/2013/10/form-validation/
PHP was used to process the forms, in the standard way and then insert the code in to the database, but using Prepared statements and stored procedures (http://php.net/manual/en/pdo.prepared-statements.php)
Then using the database and tcpdf (http://www.tcpdf.org/), you could generate pdfs on the fly from the data base, here is a sample of the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
<?php //These are comming in from a HTML form $SelectConfList = $_POST['SelectConfList']; $FormData1 = $_POST['FormData1']; $HoursWorkDone = $_POST['FormData2']; $Date = $_POST['Date']; $Location = $_POST['Location']; $Time = $_POST['Time']; $username = "Database_UserName"; $password = "Database_Password"; $DataBaseVar= 0; // Include the main TCPDF library (search for installation path). require_once('tcpdf_include.php'); $DataBaseVar= 0; //start of Database connection //here we pull out the data here for printing in the //pdf pages try { $conn = new PDO('mysql:host=localhost;dbname=Your_Database_Name', $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $ConfirmedAttended = "yes"; $stmt = $conn->prepare('select FirstName,LastName,ConfirmedAttended from Anseo where name = "'. $SelectConfList . '" AND ConfirmedAttended = "'. $ConfirmedAttended .'" ORDER BY lastname'); $stmt->execute(array('email' => $email)); $result = $stmt->fetchAll(); } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } //End of Database connection // Extend the TCPDF class to create custom Header and Footer class MYPDF extends TCPDF { //Page header public function Header() { // Logo $image_file = K_PATH_IMAGES.'LOGO.jpg'; // File x y w h type link align resize DPI palign $this->Image($image_file, 10, 10, '56', '', 'JPG', '', 'N', false, 300, 'R', false, false, 0, false, false, false); // Set font $this->SetFont('helvetica', '', 8.5); // Title // Set some content to print $html = <<<EOD This is the header <br> this is the second Part of the header EOD; // Print text using writeHTMLCell() $this->writeHTMLCell(0, 0, '150', '', $html, 0, 1, 0, true, 'L', true); } //end Headder // Page footer public function Footer() { // Position at 15 mm from bottom $this->SetY(-40); // Set font $this->SetFont('helvetica', '', 8.5); // Page number // $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); $htmlFooter = <<<EOD This is the Footer EOD; // Print text using writeHTMLCell() $this->writeHTMLCell(0, 0, '150', '', $htmlFooter, 0, 1, 0, true, 'L', true); } //end footer } //End of Class MYPDF // create new PDF document $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Test Author'); $pdf->SetTitle('Based on TCPDF Example 003'); $pdf->SetSubject('TCPDF Tutorial'); $pdf->SetKeywords('TCPDF, PDF, example, test, guide'); // remove default header/footer $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); // set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins //$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->SetMargins(5, 5, 5); $pdf->SetHeaderMargin(5); $pdf->SetFooterMargin(5); // set auto page breaks $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); // set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // set some language-dependent strings (optional) if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { require_once(dirname(__FILE__).'/lang/eng.php'); $pdf->setLanguageArray($l); } // --------------------------------------------------------- // set font $pdf->SetFont('times', 'BI', 12); //Start of page generation //Here we loop through the array that was //created in the database so that we have a //Data set to work with foreach($result as $key => $value){ // add a page $pdf->AddPage(); // print a block of text using Write() //$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0); // Set some content to print $htmlText = <<<EOD This is to Say that <br> <br> EOD; //we use the PHP function ucwords to make sure //that the certs generated have proper formating //this is a catch in case we have missed something //in the form validation $htmlText .=ucwords($value['FirstName']); $htmlText .= " "; $htmlText .=ucwords($value['LastName']); $htmlText .= "<br>"; $htmlText .= <<<EOD <br> <br> <br> $FormData1 <br> <br> On $Date<br> in $Location <br> University College Cork <br> $Time <br> <br> <br> Has worked : $HoursWorkDone hours <br> <br><br> _____________________________________ <BR> Signed EOD; // Print text using writeHTMLCell() $pdf->writeHTMLCell(0, 0, '', '120', $htmlText, 0, 1, 0, true, 'C', true); } //End of page generation // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example_003.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ |