Welcome folks today in this blog post we will be counting the total number of pages inside the pdf document in php language. All the full source code of the application is shown below
Get Started
In order to get started you need to make an index.php
file and copy paste the following code
index.php
1 2 3 4 5 6 7 8 |
<html> <body> <form action="" method="POST" enctype="multipart/form-data"> <input type="file" accept=".pdf" name="image" /> <input type="submit"/> </form> </body> </html> |
Here in this block of code we are having the html5 form to upload the pdf file from the user. We have a post method to submit the information to the php script inside the same file. And also we are providing the encoding type parameter which is equal to multipart/form-data. And in this we have the input file where we upload the pdf file. We have the filter of accept
where we will only upload pdf files. And also we have given the name parameter to the input field which is image. And then we have the submit button to submit the form.
1 2 3 4 5 6 7 |
<?php if(isset($_FILES['image'])){ $errors= array(); $file_name = $_FILES['image']['name']; $file_tmp =$_FILES['image']['tmp_name']; } ?> |
Here as you can inside this php code we are first of all checking if the submit button is clicked or not by using the isset method of php where we comparing in if condition the submit button is clicked or not. If it is clicked then we are declaring an array of errors which is currently empty. And then we are getting the filename of the file which is upload by using the $_FILES Array. And also we are also getting the temporary location of the pdf file which is uploaded. So we need to tmp location of this file to upload it to the server directory. Now in the next step we will be counting the number of pages inside the pdf document
1 2 3 4 |
move_uploaded_file($file_tmp,"uploads/".$file_name); $pdf = file_get_contents("uploads/".$file_name); $number = preg_match_all("/\/Page\W/", $pdf, $dummy); echo "<br><h1>The number of pages inside pdf document is ". $number . "</h1><br>"; |
Now inside this block of php code we are moving the uploaded file from temporary location to the location which is specified inside the second argument. In this case we are storing the pdf files inside the uploads folder. After uploading the pdf file now we need to use the preg_match_all() method to actuall count the number of pages inside it by using a simple regular expression. And lastly we are printing the number of pages present in pdf document
Full Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php if(isset($_FILES['image'])){ $errors= array(); $file_name = $_FILES['image']['name']; $file_tmp =$_FILES['image']['tmp_name']; move_uploaded_file($file_tmp,"uploads/".$file_name); $pdf = file_get_contents("uploads/".$file_name); $number = preg_match_all("/\/Page\W/", $pdf, $dummy); echo "<br><h1>The number of pages inside pdf document is ". $number . "</h1><br>"; } ?> <html> <body> <form action="" method="POST" enctype="multipart/form-data"> <input type="file" accept=".pdf" name="image" /> <input type="submit"/> </form> </body> </html> |