yemeishu 7 часов назад
Родитель
Сommit
e686e5f06d
1 измененных файлов с 35 добавлено и 8 удалено
  1. 35 8
      app/Services/PdfMerger.php

+ 35 - 8
app/Services/PdfMerger.php

@@ -287,7 +287,7 @@ class PdfMerger
 
     /**
      * 使用qpdf合并PDF(带进度回调)
-     * 【修复】qpdf命令格式和文件验证问题
+     * 【修复】强制不使用escapeshhellarg,解决单引号问题
      * 正确格式:qpdf --empty --pages file1.pdf,file2.pdf -- output.pdf
      * qpdf会自动合并所有页面,无需手动指定页面范围
      */
@@ -299,7 +299,11 @@ class PdfMerger
                 Log::error('qpdf合并失败:PDF文件不存在', [
                     'file_path' => $path,
                     'file_index' => $index,
-                    'all_files' => $pdfPaths
+                    'all_files' => $pdfPaths,
+                    'file_exists_check' => file_exists($path),
+                    'directory' => dirname($path),
+                    'directory_exists' => is_dir(dirname($path)),
+                    'directory_contents' => is_dir(dirname($path)) ? scandir(dirname($path)) : 'N/A'
                 ]);
 
                 if ($progressCallback) {
@@ -311,15 +315,31 @@ class PdfMerger
             Log::debug('qpdf验证文件存在', ['path' => $path, 'size' => filesize($path)]);
         }
 
-        // 【修复】构建正确的qpdf命令
+        // 【关键修复】构建qpdf命令 - 强制不使用任何shell转义
         // qpdf --empty --pages file1.pdf,file2.pdf -- output.pdf
         // 注意:qpdf不需要手动指定页面范围,会自动合并所有页面
 
-        // 直接使用文件路径,不使用escapeshellarg(避免单引号问题)
-        $pagesArg = implode(',', $pdfPaths);
+        // 直接拼接路径,不使用任何转义函数(关键修复)
+        $pagesArg = $pdfPaths[0];
+        for ($i = 1; $i < count($pdfPaths); $i++) {
+            $pagesArg .= ',' . $pdfPaths[$i];
+        }
         $command = "qpdf --empty --pages {$pagesArg} -- -- {$outputPath}";
 
-        Log::debug('执行qpdf命令', ['command' => $command]);
+        // 【强制清理OPcache】
+        if (function_exists('opcache_reset')) {
+            opcache_reset();
+            Log::warning('已清理OPcache');
+        }
+
+        Log::debug('【重要】执行qpdf命令(已禁用shell转义)', [
+            'command' => $command,
+            'pages_arg' => $pagesArg,
+            'output_path' => $outputPath,
+            'pdf_paths' => $pdfPaths,
+            'php_version' => PHP_VERSION,
+            'note' => '此命令已禁用escapeshellarg,如果仍有单引号说明服务器代码未更新'
+        ]);
 
         // 【优化】设置超时时间为60秒,避免无限等待
         $timeout = 60;
@@ -340,7 +360,8 @@ class PdfMerger
             Log::info('qpdf合并成功', [
                 'output_path' => $outputPath,
                 'duration_ms' => $duration,
-                'file_count' => count($pdfPaths)
+                'file_count' => count($pdfPaths),
+                'final_command' => $command
             ]);
 
             if ($progressCallback) {
@@ -356,9 +377,15 @@ class PdfMerger
             'error' => $output->errorOutput(),
             'duration_ms' => $duration,
             'timeout_seconds' => $timeout,
-            'command' => $command,
+            'final_command' => $command,
             'input_files' => $pdfPaths,
             'output_file' => $outputPath,
+            'troubleshooting' => [
+                '1. 检查文件是否存在',
+                '2. 检查文件权限',
+                '3. 检查qpdf是否安装',
+                '4. 如果命令仍有单引号,说明服务器代码未更新,需要重启PHP-FPM'
+            ],
             'note' => 'qpdf会自动合并所有页面,不需要指定页面范围'
         ]);