加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – Ghostscript PDF批量压缩

发布时间:2020-12-14 01:35:39 所属栏目:Windows 来源:网络整理
导读:我在 Windows上安装了Ghostscript,因为我要做的是压缩/减少网络共享上12,000个PDF文件的大小.任何GUI软件都无法做到这一点,因为资源耗尽导致它们在一段时间后就会爆炸,所以我认为命令行是这里的方法. 我已经阅读了Ghostscript文档和压缩PDF文件的不同示例,但
我在 Windows上安装了Ghostscript,因为我要做的是压缩/减少网络共享上12,000个PDF文件的大小.任何GUI软件都无法做到这一点,因为资源耗尽导致它们在一段时间后就会爆炸,所以我认为命令行是这里的方法.

我已经阅读了Ghostscript文档和压缩PDF文件的不同示例,但我似乎无法找到任何大批量操作.

基本上,我需要定位多个文件夹来递归压缩将在网络共享上的文件.

使用Ghostscript可以这样做吗?如果是这样,请通过一些命令示例来帮助我实现这一点.

谢谢!

解决方法

使用以下脚本,您可以定义数组变量filesDir中所需的所有目录.
它将遍历所有这些目录并搜索所有目录中的所有pdf文件,包括子目录.
对于所有找到的pdf文件,它将使用 this ghostscript command (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
   )
)

Found GitHub ghostscript command减少pdf大小:

This can reduce files to ~15% of their size (2.3M to 345K,in one case) with no obvious degradation of quality.

06001

Other options for PDFSETTINGS:

  • /screen selects low-resolution output similar to the Acrobat Distiller “Screen Optimized” setting.
  • /ebook selects medium-resolution output similar to the Acrobat Distiller “eBook” setting.
  • /printer selects output similar to the Acrobat Distiller “Print Optimized” setting.
  • /prepress selects output similar to Acrobat Distiller “Prepress Optimized” setting.
  • /default selects output intended to be useful across a wide variety of uses,possibly at the expense of a larger output file.

Source: 07002

ss64.com的命令参考链接:

> set
> DelayedExpansion
> for /f
> dir
> if
> pushd
> popd
> rem

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读