[英]Ghostscript PDF file compression using PHP's exec (Laravel on Docker)
用戶必須能夠上傳 PDF,然后將文件上傳到 Amazon S3 存儲桶,然后文件應該被壓縮。
php:7.4-fpm-alpine3.11
, GPL Ghostscript 9.50
, Laravel Framework 5.8.37
)shrink
該文件已找到並正在壓縮,但輸出為空白(1 頁,白色)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 app;docker exec -it <container_id> shrink in.pdf out.pdf
成功地使用 Docker 壓縮文件;docker exec -it <container_id> /bin/bash
在它的外殼中使用 Docker 成功壓縮文件;好的,所以問題出在這里:
$output = shell_exec("shrink " . $fullTempFilePath . " " . $fullTempFilePath);
如果輸入和輸出文件相同,Ghostscript PDF 壓縮將無法按預期工作。 解決方案:
$output = shell_exec("shrink " . $fullTempFilePath . $fullTempFilePath . "-compressed ");
shell_exec("mv " . $fullTempFilePath . "-compressed " . $fullTempFilePath);
好的,所以第一點; Ghostscript(更准確地說是 Ghostscript pdfwrite 設備)不會縮小 PDF 文件。 實際過程在 VectorDevices.htm 概述中的文檔中進行了描述。 我建議你閱讀它。
其次,您不能對輸入和輸出文件使用相同的名稱。 當 pdfwrite 設備想要寫入輸出文件時,Ghostscript 仍將從輸入文件中讀取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.