用户必须能够上载PDF,然后将文件上载到Amazon S3存储桶,然后应将文件压缩。
php:7.4-fpm-alpine3.11
,GPL Ghostscript 9.50
,Laravel Framework 5.8.37
)shrink
添加到/ usr / local / bin中>找到该文件并将其压缩,但是输出为空白(一页,白色)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
完成它的工作并成功压缩图像。我尝试了什么:
php artisan serve
启动Laravel应用;docker exec -it <container_id> shrink in.pdf out.pdf
成功压缩了文件;docker exec -it <container_id> /bin/bash
成功在Docker的外壳中压缩了文件;需要完成的工作:用户必须能够上传PDF,然后将文件上传到Amazon S3存储桶,然后应将文件压缩。当前环境:Laravel应用程序(已安装...
好,所以问题出在这里:
$output = shell_exec("shrink " . $fullTempFilePath . " " . $fullTempFilePath);
好的,第一点; Ghostscript(更准确地说是Ghostscript pdfwrite设备)不会收缩PDF文件。实际过程在VectorDevices.htm概述中的文档中进行了描述。我建议您阅读它。