使用PHP的exec进行Ghostscript PDF文件压缩(Docker上的Laravel) [英] Ghostscript PDF file compression using PHP's exec (Laravel on Docker)

查看:99
本文介绍了使用PHP的exec进行Ghostscript PDF文件压缩(Docker上的Laravel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

User has to be able to upload a PDF, then the file is uploaded to an Amazon S3 bucket, the file should be compressed then.

  • Laravel应用程序(安装在Docker上)( php:7.4-fpm-alpine3.11 GPL Ghostscript 9.50 Laravel Framework 5.8.37 )
  • 用于在其中保存文档的Amazon S3存储桶
  • 脚本在外壳文件中,该文件可以执行并作为 shrink
  • 添加到/usr/local/bin
  • 没有在Docker容器中显式添加Shell,应该吗?
  1. 用户上传文件
  2. 文件已上传到S3
  3. Laravel然后将文件下载到本地临时文件夹
  4. 然后运行Ghostscript压缩所述文件(此脚本)
  5. 压缩文件上传回S3

问题:

找到并压缩了文件,但输出为空白(一页,白色)pdf文件.

Problem:

The file is found and being compressed, but the output is a blank (1 page, which is white) pdf file.

Dockerfile:

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的用户.

nobody is the user PHP is running on.

应该压缩文件的PHP函数:

PHP function that should compress the file:

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 会完成工作并成功压缩图像.

If the file isn't a PDF, ImageOptimizer does it's job and compresses the image successfully.

  • (本地),使用 php artisan serve 启动Laravel应用程序,成功地在没有Docker的情况下压缩了文件;
  • (本地),使用 docker exec -it< container_id>成功地使用Docker压缩了文件.缩小.pdf缩小.pdf ;
  • (本地)使用 docker exec -it< container_id>在外壳中使用Docker成功压缩了文件./bin/bash ;
  • (本地)我的同事在本地做过同样的事情,而且回复也是空白的pdf
  • (Local) Successfully compressed the file without Docker, using php artisan serve to launch Laravel app;
  • (Local) Successfully compressed the file with Docker using docker exec -it <container_id> shrink in.pdf out.pdf;
  • (Local) Successfully compressed the file with Docker in it's shell using docker exec -it <container_id> /bin/bash;
  • (Local) My coworker did the same things locally and the response is also a blank pdf

推荐答案

好,所以问题出在这里:

Ok, so the problem is in here:

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

如果输入和输出文件相同,则Ghostscript PDF压缩将无法正常工作.解决方案:

Ghostscript PDF compression does not work as expected if the input and output files are the same. Solution:

$output = shell_exec("shrink " . $fullTempFilePath  . $fullTempFilePath . "-compressed ");
shell_exec("mv " . $fullTempFilePath . "-compressed " . $fullTempFilePath);

这篇关于使用PHP的exec进行Ghostscript PDF文件压缩(Docker上的Laravel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆