How to Merge PDF Files with PHP using the Java PDF Toolkit (jpdfkit) Command Line on Ubuntu Linux?
PDF files are widely used for document sharing and presentation purposes. Occasionally, you may need to merge multiple PDF files into a single document to streamline your data or presentations. In this article, we will explore how to merge PDF files with PHP using the Java PDF Toolkit (jpdfkit) command line on an Ubuntu Linux system. This powerful tool allows you to manipulate PDF files programmatically, and we'll demonstrate how to use it effectively in combination with PHP.
Getting Started
Before we dive into the merging process, you need to ensure you have the necessary tools and software installed on your Ubuntu Linux system. Follow these steps:
- Download Java PDF Toolkit (jpdfkit): You can obtain the Java PDF Toolkit (jpdfkit) from the https://veryutils.com/java-pdf-toolkit-jpdfkit . Make sure you download the appropriate version for your system.
- Install Java: If you haven't already, install Java on your Linux system using the following command:
sudo apt-get install default-jre
- Verify Installation: Confirm that Java is installed correctly by running:
java -version
- Prepare PDF Files: Place the PDF files you want to merge in a directory on your system. For this example, let's assume you have three PDF files named file1.pdf, file2.pdf, and file3.pdf in your current working directory.
Merging PDF Files with jpdfkit Command Line
Now that you have the necessary tools and files, let's proceed to merge the PDF files using the jpdfkit command line.
Open a terminal and navigate to the directory containing your PDF files. To merge the three PDF files into one, use the following command:
java -jar jpdfkit.jar file1.pdf file2.pdf file3.pdf cat output outputfile.pdf
Here's what this command does:
- java -jar jpdfkit.jar: Launches the Java PDF Toolkit (jpdfkit) using the JAR file.
- file1.pdf file2.pdf file3.pdf: Specifies the input PDF files you want to merge. You can list as many input files as needed.
- cat: Indicates that you want to concatenate (merge) the input PDF files.
- output: Specifies the output file that will contain the merged PDF content.
- outputfile.pdf: The name of the output PDF file you want to create. You can change this to any desired name.
This command will merge the specified PDF files into a single PDF document named outputfile.pdf.
Additional Merging Options
The jpdfkit command line offers various merging options, allowing you to manipulate PDF files in more advanced ways. Here are some examples:
Merge All PDF Files in a Folder
You can use a wildcard character to merge all PDF files in a folder into a single PDF file. For instance:
java -jar jpdfkit.jar *.pdf cat output combined.pdf
In this command, *.pdf represents all the PDF files in the current directory, and combined.pdf is the output file that will contain the merged content.
Combine Specific Page Ranges
You can combine specific page ranges from different PDF files into a new PDF file. For example:
java -jar jpdfkit.jar A=in1.pdf cat A1-12 A14-end output out1.pdf
In this command, A=in1.pdf specifies the first input PDF file (in1.pdf). You can add more input files as needed. A1-12 and A14-end denote the page ranges to include from the first input file (in1.pdf). The output will be saved in out1.pdf.
Combine Page Ranges from Multiple Files
To combine page ranges from multiple input files, use a command like this:
java -jar jpdfkit.jar A=in1.pdf B=in2.pdf cat A1-12 A14-end B4 B5 B6 B20-end output out1.pdf
In this command, A=in1.pdf and B=in2.pdf specify two input PDF files. You can include more files by extending this pattern. The page ranges are specified for each input file, and the resulting PDF is saved as out1.pdf.
Here's the PHP program to call jpdfkit.jar and merge multiple PDF files:
<?php // Define the list of PDF files to merge $pdfFiles = [ 'file1.pdf', 'file2.pdf', 'file3.pdf' ]; // Define the output file name $outputFile = 'outputfile.pdf'; // Build the jpdfkit command $command = 'java -jar jpdfkit.jar ' . implode(' ', $pdfFiles) . ' cat output ' . $outputFile; // Execute the command exec($command, $output, $returnCode); // Check if the command executed successfully if ($returnCode === 0) { echo 'PDF files have been successfully merged into ' . $outputFile; } else { echo 'PDF file merging failed. Please check the command and file paths.'; } ?>