Home  >  Article  >  Backend Development  >  How Can I Merge Multiple PDFs into One Using PHP?

How Can I Merge Multiple PDFs into One Using PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-25 10:29:141004browse

How Can I Merge Multiple PDFs into One Using PHP?

Merging PDF Files with PHP: A Comprehensive Guide

Q: How can PHP be used to merge multiple PDF files into a single, consolidated document?

A: PHP, with its robust capabilities, offers a solution for seamlessly merging PDF files. By integrating GhostScript, an external tool, the following PHP code can achieve the desired outcome:

$fileArray = array("name1.pdf", "name2.pdf", "name3.pdf", "name4.pdf");

$datadir = "save_path/";
$outputName = $datadir . "merged.pdf";

$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";

// Append each PDF file to the command
foreach ($fileArray as $file) {
    $cmd .= $file . " ";
}

$result = shell_exec($cmd);

Note: This code requires GhostScript to be installed and accessible on the system where the PHP script is executed.

Key Considerations:

  • GhostScript licensing: For commercial use, GhostScript requires an annual license fee of $25,000. However, under the AGPL license, you can distribute your merged PDF creation as open source code.
  • System requirements: GhostScript must be installed on either the Linux/Mac OS system (as gs) or Windows (as Ghostscript) for the code to function properly.

The above is the detailed content of How Can I Merge Multiple PDFs into One Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn