I am ubed khan php developer have an 5years plus experience in web development lives in Nagpur India. I have knowledge about phonegap, cakephp ,mysql , jquery, javascript, ajax, html5, and css.
Download dompdf library
https://github.com/dompdf/dompdf/releases/tag/v0.6.1
Extract zip file and rename dompdf and keep dompdf folder in app/vendor directory
open app/config/routes.php and place following line in it
Router::parseExtensions('pdf');
Now create pdf directory in app/view/layouts
then create default.ctp file in app/view/layouts/pdf/ and paste following code in it
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->load_html($content_for_layout);
// (Optional) Setup the paper size and orientation
$dompdf->set_paper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
exit(0);
Now open any controller , assume here PostController.php
Create function as below for pdf link
function pdflink(){
}
Create view file in app/view/posts directory and name it pdflink.ctp and paste following code
<?php
echo $this->Html->link(__('PDF'), array('action' => 'view_pdf', 'ext' => 'pdf', 1));
?>
Create following function to view pdf data
function view_pdf(){
}
Now create "pdf" directory in app/view/posts directory.
Then Create view file in app/view/posts/pdf/ directory and name it "view_pdf.ctp" and paste following html which will display in your pdf.
<h1>Ubed Khan Web Developer</h1>
<p>Thanks to visit ukphpdeveloper.blogspot.in</p>
Now Run
http://localhost/project-name/posts/pdflink
you will see the link on this page and just click on that your pdf will download. It's simple as that.
Note: Keep your debug mode on production level in app/config/core.php
Configure::write('debug',0);
if you will get the error that font.php file not found then you have to download php font from below link
https://github.com/PhenX/php-font-lib/releases
or
https://github.com/PhenX/php-font-lib/tree/master/src/FontLib
And create "classes" directory in app/Vendor/dompdf/lib/php-font-lib/
Then paste downloaded php font library in app/Vendor/dompdf/lib/php-font-lib/classes/
if you large amount of data then it will need execution time more than 30sec which is by default in php.ini file. so you can try by doing following changes.
//extensive suitability check before doing anything with the file…
if (($_FILES['upload'] == "none") OR (empty($_FILES['upload']['name'])) )
{
$message = "No file uploaded.";
}
else if ($_FILES['upload']["size"] == 0)
{
$message = "The file is of zero length.";
}
else if (($_FILES['upload']["type"] != "image/pjpeg") AND ($_FILES['upload']["type"] != "image/jpeg") AND ($_FILES['upload']["type"] != "image/png"))
{
$message = "The image must be in either JPG or PNG format. Please upload a JPG or PNG instead.";
}
else if (!is_uploaded_file($_FILES['upload']["tmp_name"]))
{
$message = "You may be attempting to hack our server. We're on to you; expect a knock on the door sometime soon.";
}
else {
$message = "";
$move = @ move_uploaded_file($_FILES['upload']['tmp_name'], $url);
if(!$move)
{
$message = "Error moving uploaded file. Check the script is granted Read/Write/Modify permissions.";
}
if ($_SERVER['SERVER_ADDR'] == "172.10.1.7" || $_SERVER['SERVER_ADDR'] == "192.155.246.146")
$url = "http://".$_SERVER['SERVER_ADDR'].":".$_SERVER['SERVER_PORT']."/" . $url;
else
$url = "http://".$_SERVER['SERVER_ADDR']."/" . $url;
Execute Following query if you created database in during installation
CREATETABLEusers(
idINT(11)NOTNULLAUTO_INCREMENTPRIMARYKEY,
usernameVARCHAR(255)NOTNULLUNIQUE,
passwordCHAR(40)NOTNULL,
group_idINT(11)NOTNULL,
createdDATETIME,
modifiedDATETIME
);
CREATETABLEgroups(
idINT(11)NOTNULLAUTO_INCREMENTPRIMARYKEY,
nameVARCHAR(100)NOTNULL,
createdDATETIME,
modifiedDATETIME
);
CREATETABLEposts(
idINT(11)NOTNULLAUTO_INCREMENTPRIMARYKEY,
user_idINT(11)NOTNULL,
titleVARCHAR(255)NOTNULL,
bodyTEXT,
createdDATETIME,
modifiedDATETIME
);
CREATETABLEwidgets(
idINT(11)NOTNULLAUTO_INCREMENTPRIMARYKEY,
nameVARCHAR(100)NOTNULL,
part_noVARCHAR(12),
quantityINT(11)
);
These are the tables we will be using to build the rest of our application. Once we have the table structure in the database we can start cooking. Use Code Generation with Bake to quickly create your models, controllers, and views.
To bake you just need to follow this video, in video we only baked Group model so repeat same step for User, Post and Widget model. This will have generated the 4 controllers, models and your views for you.
Video
Our third point would be completed here.
Now we are going to set permission to users on the basis of their Group. Which is our last 4th point.
Open app/Controller/UsersController.php and paste following function in that
Now Run application http://localhost/acl/groups/add
Add Following Groups
·administrators
·managers
·users
Then Add users for each
group,
http://localhost/acl/users/add
Note: acl is my project name so do change if your project name is different.
Creating ACOs (Access Control Objects)
Now that we have our users and groups (aros), we can begin inputting our existing controllers into the Acl and setting permissions for our groups and users, as well as enabling login / logout.
Our ARO are automatically creating themselves when new users and groups are created. What about a way to auto-generate ACOs from our controllers and their actions? Well unfortunately there is no magic way in CakePHP’s core to accomplish this. The core classes offer a few ways to manually create ACO’s though. You can create ACO objects from the Acl shell OR by Query. Creating Acos from the shell looks like below OR execute below query
As mentioned before, there is no pre-built way to input all of our controllers and actions into the Acl. However, we all hate doing repetitive things like typing in what could be hundreds of actions in a large application.
After execution
remove this line $this->Auth->allow(); from beforeFilter function and also
remove initDB function from usersController.
Also remove this line $this->Auth->allow(); from beforeFilter function groupsController.
We now have set up some basic access rules. We’ve allowed administrators to everything. Managers can access everything in posts and widgets. While users can only access add and edit in posts & widgets.
You may have noticed that I deliberately left out index and view from my Acl permissions. We are going to make view and index public actions in PostsController and WidgetsController. This allows non-authorized users to view these pages, making them public pages.
So add below beforeFilter function in /app/Controller/PostsController.php and /app/Controller/WidgetsController.php
publicfunctionbeforeFilter(){
parent::beforeFilter();
$this->Auth->allow('index','view');
}
InAppController::beforeFilter()add the following line
$this->Auth->allow('display');
This makes the ‘display’ action public. This will keep our PagesController::display() public. This is important as often the default routing has this action as the home page for your application.
Replace Login and logout
function in /app/Controller/UsersController.php with below Login and logout function.