Contact Us


Home Earn money Earn more Jobs Free Search Engine Submission

Merge pdf files

Instructions to merge pdf files

Step1: Upload atleast 2 pdf files
Step2: Click Merge pdf files button
Step3: Wait for the merge process              to complete
Step4: Download your file after               merge process complete
Step5: Delete your files from our              database

Upload pdf files


Select pdf file 1 to upload:(Required)

Select pdf file 2 to upload:(Required)

Select pdf file 3 to upload:(Optional)











PropellerAds
Upload,merge and download pdf files
The process of uploading, merging and downloading can be easily executed with the application of HTML (Hypertext Markup Language), PHP (Hypertext Preprocessor) and Cpanel without even creating database to hold information. HTML is used to create electronic pages that are displayed on the World Wide Web. PHP is a server side scripting language which allows connection to the server. cPanel allows you to publish websites, manage domains, organize web files, create email accounts, and more. Here we show a step by step process on how to implement this. The implementation of this tool has also been added on the right hand side of this page please feel free to try it.
Step 1 : Create HTML file and name it as index.html
This HTML file will mainly contain form and allow users to upload three pdf files.

<html>
<body>
<center>
<form action="upload.php" method="post" 
 enctype="multipart/form-data">
 Select pdf1
 <input type="file" name="file1" 
 id="file1" 
 accept="application/pdf">
 Select pdf2
 <input type="file" name="file2" 
 id="file2" 
 accept="application/pdf">
 Select pdf3
 <input type="file" name="file3" 
 id="file3" 
 accept="application/pdf">
 <p><input type="submit" value="Merge" 
 name="submit"></p>
</form>
</center>
</body>
</html>
 
   
The action="upload.php" attribute send the form data to upload.php script for input processing. The method="post" attribute instruct the web browser to send input data to web server. The attribute enctype="multipart/form-data" allows user to upload files through the form. The type="file" attribute allows user to select the file from device storage. Attribute name="file1" is used to reference the form-data after submitting the form. The id="file1" is most used in a style sheet or JavaScript to manipulate the element with the specific id. The accept="application/pdf" attribute is used to specifies the type of file that the server accepts in this case the server accepts pdf files. The type="submit" defines a submit button to submit the form to server. The attribute value="Merge" defines the text on the submit button.
Step 2 : Create PHP file and name it as upload.php
The php script indicated below accepts input data from html form (index.html) uploads, validate and process it.

<?php   
// Uploads files
// if save button on the form is clicked
if (isset($_POST['submit'])) { 
// name of the uploaded file
$fn1 = $_FILES['file1']['name'];
$fn2 = $_FILES['file2']['name'];
$fn3 = $_FILES['file3']['name'];

//Take 1st character of the file
//and append file ext. 
//to the 1st character
//Override the previous files  
$fnA = substr($fn1,1,1) . "." . 
       substr($fn1,-3);
$fnB = substr($fn2,1,1) . "." . 
       substr($fn2,-3);
$fnC = substr($fn3,1,1) . "." . 
       substr($fn3,-3);

// destination of the file on the server
//Create the path for temporary pdf files
//This path must be created in cPanel
$des1='/home/username/public_html/'.$fnA;
$des2='/home/username/public_html/'.$fnA;
$des3='/home/username/public_html/'.$fnA;
   
// get the file extension
$ext1 = pathinfo($fname1, 
        PATHINFO_EXTENSION);
$ext2 = pathinfo($fname2, 
        PATHINFO_EXTENSION);
$ext3 = pathinfo($fname3, 
        PATHINFO_EXTENSION);

// Temporary uploads 
//directory on the server
$file1 = $_FILES['file1']['tmp_name'];
$file2 = $_FILES['file2']['tmp_name'];
$file3 = $_FILES['file3']['tmp_name'];
$size1 = $_FILES['file1']['size'];
$size2 = $_FILES['file2']['size'];
$size3 = $_FILES['file3']['size'];
    
//Upload files to the temporary folder    
move_uploaded_file($file1, $des1); 
move_uploaded_file($file2, $des2); 
move_uploaded_file($file3, $des3);

//Delete the previous merged file
//This will explain later 
$fdelete = '/home/username/public_html/' 
           . "mergedPDF.pdf";
unlink($fdelete);
//Validate the values from HTML form
if(!in_array($extension1,['pdf','PDF'])|| 
 !in_array($extension2,['pdf', 'PDF'])|| 
 (!in_array($extension3,['pdf','PDF']) 
  && $filename33 != '')) {
 $message2 = "You file extension must 
 be .pdf format, not blank and 
 upload atleast 2 pdf files!";
 } elseif ($_FILES['fileToUpload1']
   ['size'] 
  > 10000000 || 
 $_FILES['fileToUpload2']['size'] 
  > 10000000 || 
 $_FILES['fileToUpload3']['size'] 
  > 10000000) { 
 $message3 = "File size must be less 
             than 1gigabyte!";
 } else {
   echo "No error";
 }
}
//Delete pdf files after done merging
$fdelete='/home/username/public_html/'. 
           $fnA;
unlink($fdelete);
$fdelete='/home/username/public_html/'. 
           $fnB;
unlink($fdelete);
$fdelete='/home/username/public_html/'. 
            $fnC;
unlink($fdelete);
?>

The (isset($_POST['submit'])) logic code check if the the submit button was clicked by the user. The next thing the script does fetch the names (using $_FILES['file1']['name'];) of the pdf files uploaded through the html form and assign them to viariables. Next the script extract the first character and extention of the pdf file name and append them ($fnA = substr($fn1,1,1).".".substr($fn1,-3);). This logic will help us to override the pdf files loaded before. Next the script create the path where the pdf files will be temporary saved or stored ($des1='/home/username/public_html/'.$fnA;). The path '/home/username/public_html/' is where all the files (index.html etc.) are stored and will be explain in cPanel. ($ext1=pathinfo($fname1,PATHINFO_EXTENSION);) get the file extention. The ($size1=$_FILES['file1']['size'];) get the file size or file name and assign it to the variable. The (move_uploaded_file($file1,$des1);) upload or move files to the temporay folder. The (unlink($fdelete);) delete the specified file. This will delete already merged pdf file that was merged before. The merge process will be explain in next section. The next twe parts of the scripts validate the file to check if the file is pdf and size of file is not large. The last part of the script delete uploaded files loaded before.
Step 3 : Create PHP file and name it as merge.php
This is where merge process take place. The php script shown below fetch the names of pdf files and merge them into one pdf file.

<?php   
// Uploads files
// if save button on the form is clicked
if (isset($_POST['submit'])) { 
// name of the uploaded file
$fn1 = $_FILES['file1']['name'];
$fn2 = $_FILES['file2']['name'];
$fn3 = $_FILES['file3']['name'];

//Take 1st character of the file
//and append file ext. 
//to the 1st character
//Override the previous files  
$fnA = substr($fn1,1,1) . "." . 
       substr($fn1,-3);
$fnB = substr($fn2,1,1) . "." . 
       substr($fn2,-3);
$fnC = substr($fn3,1,1) . "." . 
       substr($fn3,-3);

//Delete previous file
$fdelete='/home/username/public_html/'
         . "mergedPDF.pdf";
unlink($fdelete);

//Merge files using Ghostscript
if (isset($_POST['submit'])) {
$fileArray= array($fnA,$fnB,$fnC);
$ddir="/home/username/public_html/";
$outputName = $ddir."mergedPDF.pdf";
$cmd = "gs -q -dNOPAUSE -dBATCH 
       -sDEVICE=pdfwrite 
       -sOutputFile=$outputName ";
//Add each pdf file to the 
//end of the command
foreach($fileArray as $file) {
 $cmd .= $file." ";
}

?>
 
   
When the user input data in the form and click submit button the script above get values or names of the files. Then the script extract the the first character of the name of the file and file extention. Then append the file extention to the first character. Next the script delete the old pdf file merged before. Next the script create the new merged pdf file and save it to the path. In order for this to work you must install Ghostscript in your server. In cPanel this works without installing Ghostscript.
Step 4 : Create PHP file and name it as download.php
The php script indicated below shows how to download the merged pdf file from the link and save it to your local storage device. You must create a link such as
<a href="http://www.example.com/
download.php?file=mergedPDF"> 
Download your file </a>

<?php
   
//Get the file name from the link
$file = $_GET["file"]  . ".pdf"; 

//Check if the link is clicked
//and download the merged files  
if (isset($_GET['file'])) {
$filepath='/home/username/public_html/'
         . $file;
if (file_exists($filepath)) {
 header('Content-Description: 
       File Transfer');
 header('Content-Type: 
       application/octet-stream');
 header('Content-Disposition: 
       attachment; filename=' 
       . basename($filepath));
 header('Expires: 0');
 header('Cache-Control: 
       must-revalidate');
 header('Pragma: public');
 header('Content-Length: '.filesize
       ('/home/username/public_html/' 
       . $file));
 readfile('/home/username/public_html/' 
       . $file);
}
} 
?> 
 
   
When the user click on the download link the ($_GET["file"] . ".pdf";) fetches the file name from the link. Then append .pdf to the file name. The script next assigns the results to the variable. The ($filepath) variable create the the directory with a file name. The next part of the script download your pdf file to your local computer. The value of the file from the downloadable link must be same name as the merged pdf file named in step 3.
Step 5 : Save/upload your files to cPanel File manager
Follow intructions below to upload files in cPanel

Step 1 : Login to your cPanel
cPanel
Step 2 : Click on File Manger
cPanel
Step 3 : Double click on public_html
cPanel
Step 4 : Click on upload
cPanel
Step 5 : Select index.html, upload.php, merge.php and download.php files
cPanel

© 2016 - 2020 onlineaffiliateonline.co.za, All rights reserved | Terms & Privacy