我在Windows上安装了Ghostscript,因为我想做的是压缩/减少网络共享中12,000 PDF文件的大小。任何GUI软件都无法实现,因为它在一段时间后仅由于资源用尽而炸弹,所以我认为命令行是可以转到此处的方式。
我已经阅读了Ghostscript文档和压缩PDF文件的不同示例,但是我似乎找不到任何可作为大批次操作的东西。
基本上,我需要针对多个文件夹以递归压缩将在网络共享上的文件。
这有可能与Ghostscript这样做?如果是这样,请建议您提供一些命令示例,以帮助我实现这一目标。
谢谢!
使用以下脚本,您可以在数组变量filesDir
中定义所有目录。
它将在所有这些目录上循环,并在包括子目录的所有目录中搜索所有PDF文件。
对于所有找到的PDF文件,它将使用此ghostscript命令(github),并输出使用名称的文件。fileabc.pdf
带有新名称:compr_fileabc.pdf
。
编辑#1:
我按照注释要求更改了脚本,以编写新的PDF文件或覆盖输入PDF文件。在这些选择之间选择选项,将createNewPDFs
变量更改为1(新文件)或0(覆盖)。
由于Ghostscript无法写入输入文件,因此输出文件将写在用户临时路径(%TEMP%
)上,然后将其移至原始输入文件以超越此文件。如果新文件的大小较小,它将仅覆盖输入PDF文件。
此外,ghostscript
命令由具有相同名称的变量代替,因为在Windows下,它可以是gswin64c
(64位)或gswin32c
(32位)。
如果使用这些ghostscript命令开关:-dPDFSETTINGS=/printer
的量大规模不够小,请在下面说明。
批处理脚本:
@echo off
setlocal EnableDelayedExpansion
rem ghostscript executable name
set "ghostscript=gswin64c"
rem directories to scan for files
set "filesDir[0]=FOLDER1"
set "filesDir[1]=FOLDER2"
set "filesDir[2]=FOLDER3"
rem extension of files to be scanned
set "ext=pdf"
rem new file be creation or input file overwrite
set "createNewPDFs=0"
rem file prefix for new files (if they should be created)
set "filepre=compr_"
rem loop over all directories defined in filesDir array
for /f "tokens=2 delims==" %%d in ('set filesDir[') do (
if exist "%%~d" (
pushd "%%~d"
rem loop over all files in all (sub)directories with given extension
for /f "delims=*" %%f in ('dir "*.%ext%" /b /s /a:-d') do (
if [%createNewPDFs%] EQU [1] (
%ghostscript% -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%%~dpf%filepre%%%~nxf" "%%~f"
) else (
%ghostscript% -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile="%TEMP%%%~nxf" "%%~f"
for %%t in ("%TEMP%%%~nxf") do ( set "newSize=%%~zt" )
for %%t in ("%%~f") do ( set "oldSize=%%~zt" )
if [!newSize!] LSS [!oldSize!] (
rem new file is smaller --> overwrite
move /y "%TEMP%%%~nxf" "%%~f"
) else (
rem new file is greater --> delete it of the temp dir
del "%TEMP%%%~nxf"
)
)
)
popd
)
)
找到github ghostscript命令以减少pdf大小:
这可以将文件降低到其大小的〜15%(在一种情况下为345K),没有明显的质量降解。
ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
PDFSettings的其他选项:
- /屏幕选择类似于Acrobat Distiller"屏幕优化"设置的低分辨率输出。
- /电子书选择类似于Acrobat Distiller" Ebook"设置的中分辨率输出。
- /打印机选择类似于Acrobat Distiller"打印优化"设置的输出。
- /prepress选择输出类似于Acrobat Distiller" Prepress"优化"设置。
- /默认选择输出旨在在多种用途中有用,可能是以较大的输出文件为代价。
来源:http://ghostscript.com/doc/current/ps2pdf.htm
命令参考链接来自ss64.com:
- set
- 延迟扩展
- for/f
- dir
- 如果
- pushd
- popd
- rem
我不知道是否有人需要它,但这是我高度压缩PDF文件而不会质量降解的命令。我通过许多试验的方法找到了它,并且错误的方法大大降低了PDF文件的大小。 P.S。很抱歉在上面的线程中发布不发布,但我没有足够的声誉作为新来者。
%ghostscript% -q -dNOPAUSE -dBATCH -dSAFER -dSimulateOverprint=true -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dEmbedAllFonts=true -dSubsetFonts=true -dAutoRotatePages=/None -dColorImageDownsampleType=/Bicubic -dColorImageResolution=150 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=150 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=150 -sOutputFile=output.pdf input.pdf
我面临着同样的问题,这对我有帮助。将15 MB PDF转换为400KB
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.2 -r200 -dPrinted=false -dNOPAUSE -dQUIET -dBATCH -sOutputFile=c12_{filename} {filename}
[使用Mac]我需要同时减小多个PDF文件的大小。
所以,我使用了ghostscript和bash脚本来做到这一点。
- 通过啤酒安装
$ brew install ghostscript
- 将所有PDF文件放入文件夹中。
- 在同一文件夹中创建一个名为
output/
的新的空目录 - 在同一文件夹中,创建一个名为
script.sh
的bash脚本。 - 使用
sh script.sh
运行脚本
#!/bin/bash
# Get a list of all .pdf files in the current directory
files=$(ls *.pdf)
# Loop over the list of files
for file in $files
do
echo "Processing file: $file"
# Run the GS script for each file
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.2 -r200 -dPrinted=false -dNOPAUSE -dQUIET -dBATCH -sOutputFile=./output/"$file" "$file" &
# Wait for the previous command to finish before starting the next one
wait $!
echo "Finished processing file: $file"
done
echo "All .pdf files have been processed"
这里没有什么可用于最新GS,所以我最终与
一起gswin64c.exe -dPDFSETTINGS#/ebook -dPDFX -dBATCH -dNOPAUSE -sColorConversionStrategy=CMYK -sDEVICE=pdfwrite -sOutputFile="output.pdf" "input.pdf"
并用自动化形式JPG和旋转90度顺时针
gswin64c -dORIENT1=false -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=C:UsersusernameDesktopsomepdffile.pdf -c "<</Orientation 3>> setpagedevice" -f "C:Program Filesgsgs9.19libviewjpeg.ps" -c "(C:\Users\username\Desktop\somejpgfile.JPG) << /PageSize 2 index viewJPEGgetsize 2 array astore >> setpagedevice viewJPEG"
/方向3控制旋转角
我确实注意到,在这样的许多主题下,用户正在寻找一种简单的方法来使PDF文件较小,以较小的网络交付。例如。Windows 10 Innolielt PDF-Writer无需压缩PDF文件。
这就是为什么我想在这里共享我的最终版本的自动化Ghostscript PDF转换批量文件,用于Windows 64位。当用户拖动并将文件放在Windows文件夹中的该批处理文件顶部时,此批处理文件将自动将大型PDF文件转换为较小的版本。笔记! - 此批处理文件和大型PDF文件必须位于同一文件夹中,并且在使用此文件之前,必须在Windows上安装正确的版本(32/64位)。还要注意,此批次以" _original.pdf"结尾的名称保存了原始的大型PDF。当在同一文件上使用一次时。如果用户不断反复转换同一文件,则原始文件将被较小的版本替换。
@echo off
rem
rem === This part separates the filename and folder names to two different variables and replaces empty spaces in filename with underlines ===
set filename=%1
set filename=%filename: =_%
for %%A in ("%filename%") do (
set Folder=%%~dpA
set Name=%%~nA )
echo.Folder is: %Folder%
echo.Name is: %Name%
rem
rem === Copy the original file to - Name_original.pdf - removing the space from the end of - Name - variable ===
copy %1 "%Name: =_%original.pdf"
rem
rem === Copy the original file to a temporary file for Ghostscript to use ===
copy %1 oldTempFile.pdf
rem
rem === Run Ghostscript to create much smaller size PDF from your original and replace the drag and dropped - Name.pdf - file with it ==
gswin64.exe -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -dDownsampleColorImages=true -dColorImageResolution=200 -dColorImageDownsampleType=/Bicubic -sOutputFile=%1 oldTempFile.pdf
rem
rem == Remove the temporary file after use ===
del oldTempFile.pdf