为什么我无法合并由图像字符串构建的 PDF


Why can't I merge my PDFs built from image strings?

假设我有一个对象数组$shippingDetails,其中每个shippingDetail都有一个属性image_string

我正在使用pdf_decodefile_put_contents将每个image_string转换为PDF。我想将 PDF 合并在一起。

为什么我下面的代码不起作用?

    $fileArray = array();
    $i = 0;
    foreach($shippingDetails as $shippingDetail) {
        $pdf_decoded = base64_decode ($shippingDetail['image_string']);
        $tempName = 'temp' . $i++ . '.pdf';
        file_put_contents($tempName, $pdf_decoded);
        $fileArray[] = $tempName;
    }
    $dataDir = 'save_path/';
    $outputName = $dataDir . 'shippingLabel.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." ";
    }
    $result = shell_exec($cmd);

我得到的输出只是最后一个PDF。

在我看来

,这不起作用,因为我的服务器上没有安装 GhostScript。

在我意识到这一点之前,我最终使用了 hanneskod 的 libmergepdf,如果将来有人偶然发现这一点,它运行得很好。