使用PHP的exec(Docker上的Laravel)压缩Ghostscript PDF文件]]

问题描述 投票:1回答:2

需要做什么:

用户必须能够上载PDF,然后将文件上载到Amazon S3存储桶,然后应将文件压缩。

当前环境:
  • Laravel应用程序(安装在Docker上)(php:7.4-fpm-alpine3.11GPL Ghostscript 9.50Laravel Framework 5.8.37

  • 用于存储文档的Amazon S3存储桶
  • 脚本位于可执行的外壳文件中,并作为shrink添加到/ usr / local / bin中>
  • Shell没有明确添加到Docker容器中,应该吗?
  • 当前流量:
    1. 用户上传文件
    2. 文件已上传到S3

  • Laravel然后将文件下载到本地临时文件夹
  • 然后运行Ghostscript来压缩所述文件(此script
  • 压缩文件上传回S3
  • 问题:

    找到该文件并将其压缩,但是输出为空白(一页,白色)pdf文件。

    Dockerfile:

    # Add compression shell script to global executables
    COPY .docker/config/shrink.sh /usr/local/bin/shrink
    RUN chmod u+x /usr/local/bin/shrink
    RUN chown nobody.nobody /usr/local/bin/shrink
    

    [nobody是运行PHP的用户。

    应该压缩文件的PHP函数:

    public function optimizeFile($file_path)
        {
            // Copy file from default disk to temp disk
            Storage::disk('temp')->put($file_path, Storage::get($file_path));
    
            $fullTempFilePath = Storage::disk('temp')->path($file_path);
    
            if (Storage::mimeType($file_path) == 'application/pdf') {
                $output = shell_exec("shrink " . $fullTempFilePath . " " . $fullTempFilePath);
                if ($output != null) {
                    Log::error($output);
                }
            } else {
                ImageOptimizer::optimize($fullTempFilePath);
            }
    
            // Write the compressed file back to default disk
            Storage::put($file_path, Storage::disk('temp')->get($file_path));
    
            // Delete temp file
            Storage::disk('temp')->delete($file_path);
        }
    

    如果文件不是PDF,则ImageOptimizer完成它的工作并成功压缩图像。

    我尝试了什么:

    • ((Local))在没有Docker的情况下成功压缩了文件,使用php artisan serve启动Laravel应用;
    • ((本地)使用Docker使用docker exec -it <container_id> shrink in.pdf out.pdf成功压缩了文件;
    • ((本地)使用docker exec -it <container_id> /bin/bash成功在Docker的外壳中压缩了文件;
    • [(Local),我的同事在本地做过同样的事情,回复也是空白的pdf
    • 需要完成的工作:用户必须能够上传PDF,然后将文件上传到Amazon S3存储桶,然后应将文件压缩。当前环境:Laravel应用程序(已安装...

    php laravel docker amazon-s3 ghostscript
    2个回答
    1
    投票

    好,所以问题出在这里:

    $output = shell_exec("shrink " . $fullTempFilePath . " " . $fullTempFilePath);


    0
    投票

    好的,第一点; Ghostscript(更准确地说是Ghostscript pdfwrite设备)不会收缩PDF文件。实际过程在VectorDevices.htm概述中的文档中进行了描述。我建议您阅读它。

    © www.soinside.com 2019 - 2024. All rights reserved.