GUI程序用于调整PDF文档大小和更改其DPI。

我看到LibreOffice Draw应该能够打开PDF并导出它,可以选择更改质量和DPI分辨率的选项。但是,我拥有的高分辨率扫描PDF在这个应用程序中没有正确打开(只导入了第一页),所以我想找一个更专业的应用程序。
目前,我不需要命令行选项(我的PDF文件太大,尝试过的所有方法都失败了)。
2个回答

Nautilus Script


代码

我已经成功地使用这个Nautilus脚本调整了我的PDF大小:

#! /bin/bash

# AUTHOR:   (c) Ricardo Ferreira, oriolpont -- modified in accordance to https://askubuntu.com/q/280618/81372
# NAME:     Compress PDF 1.4.2
# DESCRIPTION:  A nice Nautilus script with a GUI to compress and optimize PDF files
# REQUIRES: ghostscript, poppler-utils, zenity, sed, python-notify (optional)
# LICENSE:  GNU GPL v3 (http://www.gnu.org/licenses/gpl.html)
# WEBSITE:  https://launchpad.net/compress-pdf

VERSION="1.4.2"
COMPRESSPDF_BATCH_ABORT_ERR=115

# Messages
        # English (en-US)
        error_nofiles="No file selected."
        error_noquality="No optimization level selected."
        error_ghostscript="PDF Compress requires the ghostscript package, which is not installed. Please install it and try again."
        error_nopdf="At least one of the selected file(s) is not a valid PDF archive."
        label_filename="Save PDF as..."
        label_level="Please choose an optimization level below."
        optimization_level="Optimization Level"
        level_default="Default" # apparently close to High Quality
        level_screen="Screen-view only (72dpi)"
        level_low="Low Quality (150dpi)"
        level_high="High Quality (300dpi)"
        level_color="High Quality (Color Preserving) (300dpi)"
        job_done="has been successfully compressed"
        filename_suffix="_opt"
        label_suffix="Choose the suffix for the filenames."
        warning_overwrite="That will overwrite the original pdf files."


case $LANG in

    pt* )
        # Portuguese (pt-PT)
        error_nofiles="Nenhum ficheiro seleccionado."
        error_noquality="Nenhum nível de optimização escolhido."
        error_ghostscript="O PDF Compress necessita do pacote ghostscript, que não está instalado. Por favor instale-o e tente novamente."
        error_nopdf="O ficheiro seleccionado não é um ficheiro PDF válido."
        label_filename="Guardar PDF como..."
        label_level="Por favor escolha um nível de optimização abaixo."
        optimization_level="Nível de Optimização"
        level_default="Normal"
        level_screen="Visualização no Ecrã"
        level_low="Baixa Qualidade"
        level_high="Alta Qualidade"
        level_color="Alta Qualidade (Preservação de Cores)"
        job_done="foi comprimido com sucesso"
        filename_suffix="-otimizado";;


    es* )
        # Spanish (es-AR) by Eduardo Battaglia
        error_nofiles="Ningún archivo seleccionado."
        error_noquality="Ningún nivel de optimización escogido."
        error_ghostscript="Compress PDF necesita el paquete ghostscript, que no está instalado. Por favor instálelo e intente nuevamente."
        label_filename="Guardar PDF como..."
        label_level="Por favor escoja un nivel de optimización debajo."
        optimization_level="Nivel de Optimización"
        level_default="Normal"
        level_screen="Sólo visualización"
        level_low="Baja calidad"
        level_high="Alta calidad"
        level_color="Alta calidad (Preservación de Colores)"
        filename_suffix="-optimizado";;


    cs*)
       # Czech (cz-CZ) by Martin Pavlík
        error_nofiles="Nebyl vybrán žádný soubor."
        error_noquality="Nebyla zvolena úroveň optimalizace."
        error_ghostscript="PDF Compress vyžaduje balíček ghostscript, který není nainstalován. Nainstalujte jej prosím a opakujte akci."
        label_filename="Uložit PDF jako..."
        label_level="Prosím vyberte úroveň optimalizace z níže uvedených."
        optimization_level="Úroveň optimalizace"
        level_default="Výchozí"
        level_screen="Pouze pro čtení na obrazovce"
        level_low="Nízká kvalita"
        level_high="Vysoká kvalita"
        level_color="Vysoká kvalita (se zachováním barev)"
        filename_suffix="-optimalizované";;


    fr*)
       # French (fr-FR) by Astromb
        error_nofiles="Aucun fichier sélectionné"
        error_noquality="Aucun niveau d'optimisation sélectionné"
        error_ghostscript="PDF Compress a besoin du paquet ghostscript, mais il n'est pas installé. Merci de l'installer et d'essayer à nouveau."
        error_nopdf="Le fichier que vous avez sélectionné n'est pas un PDF valide."
        label_filename="Sauvegarder le PDF sous..."
        label_level="Merci de choisir, ci-dessous, un niveau d'optimisation."
        optimization_level="Niveau d'optimisation"
        level_default="Défaut"
        level_screen="Affichage à l'écran"
        level_low="Basse qualité"
        level_high="Haute qualité"
        level_color="Haute qualité (Couleurs préservées)"
        filename_suffix="-optimisé";;


    zh_CN*)
        # Simplified Chinese (zh_CN) by TualatriX Chou
        error_nofiles="没有选择文件。"
        error_noquality="没有优化优化等级。"
        error_ghostscript="PDF压缩需要ghostscript软件包,但是它没有安装。请先安装然后再重试。"
        error_nopdf="选择的文件不是一个有效的PDF文件"
        label_filename="另存为PDF..."
        label_level="请在下面选择优化等级"
        optimization_level="优化等级"
        level_default="默认"
        level_screen="仅在屏幕上浏览"
        level_low="低品质"
        level_high="高品质"
        level_color="高品质(护色) "
        filename_suffix="-优化";;


        ar*)
                # Arabic (ar) by Mohammed hasan Taha
        error_nofiles="لم يتم اختيار ملف"
        error_noquality="لم يتم اختيار درجة الضغط"
        error_ghostscript="هذا السكربت يحتاج حزمة ghostscript package لذا يرجى تنصيبها ثم اعادة المحاولة"
        error_nopdf="الملف الذي تم اختياره ليس ملف pdf صالح"
        label_filename="حفظ الملف باسم"
        label_level="الرجاء اختيار درجة الضغط"
        optimization_level="درجة الضغط"
        level_default="افتراضي"
        level_screen="عرض للشاشة فقط(الدرجة الأكثر انخفاضا)"
        level_low="جودة منخفضة"
        level_high="جودة مرتفعة"
        level_color="جودة عالية جدا"
        filename_suffix="-الأمثل";;


    ml_IN*)
        # Malayalam (ml_IN) by Hrishikesh K B
        error_nofiles="ഒരു ഫയലും തിരഞ്ഞെടുത്തിട്ടില്ല."
        error_noquality="യാതൊരു ഒപ്റ്റിമൈസേഷന്‍ ലെവലും തിരഞ്ഞെടുത്തിട്ടില്ല."
        error_ghostscript="പി ഡി എഫ് കംപ്രസ്സറിന് ഗോസ്റ്റ് സ്ക്രിപ്റ്റ് പാക്കേജ് ആവശ്യമാണ്. ആ പാക്കേജ് ഇന്‍സ്റ്റാള്‍ ചെയ്‌‌ത ശേഷം ദയവായി വീണ്ടും ശ്രമിക്കുക."
        error_nopdf="തിരഞ്ഞെടുത്ത ഫയല്‍ സാധുവായ ഒരു പിഡിഎഫ് ആര്‍ച്ചീവ് അല്ല."
        label_filename="പിഡിഎഫ് ഇങ്ങിനെ സംരക്ഷിക്കുക..."
        label_level="ദയവായി താഴെ നിന്നും ഒരു ഒപ്റ്റിമൈസേഷന്‍ ലെവല്‍ തിരഞ്ഞെടുക്കുക."
        optimization_level="ഒപ്റ്റിമൈസേഷന്‍ ലെവല്‍ "
        level_default="ഡീഫാള്‍ട്ട്"
        level_screen="സ്ക്രീനില്‍ കാണാന്‍ മാത്രം "
        level_low="കുറഞ്ഞ നിലവാരം"
        level_high="കൂടിയ നിലവാരം "
        level_color="കൂടിയ നിലവാരം (നിറം സംരക്ഷിച്ചിട്ടുള്ളത്)";;


    he*)
        # Hebrew (he-IL) by Yaron (from Launchpad question)
        error_nofiles="לא נבחר אף קובץ."
        error_noquality="לא נבחרה רמת הייעול."
        error_ghostscript="התכנית PDF Compress דורשת את החבילה ghostscript, שאינה מותקנת. נא להתקין אותה ולנסות שוב."
        error_nopdf="הקובץ הנבחר אינו ארכיון PDF תקני."
        label_filename="שמירת ה־ PDF בשם..."
        label_level="נא לבחור את רמת הייעול להלן."
        optimization_level="רמת הייעול"
        level_default="בררת מחדל"
        level_screen="לצפייה בצג בלבד"
        level_low="איכות נמוכה"
        level_high="איכות גבוהה"
        level_color="איכות גבוהה (שימור הצבע)"
        job_done="הדחיסה הסתיימה בהצלחה";;

esac



# Check if Zenity is installed
if ! ZENITY=$(which zenity)
then
    echo "error: Compress PDF $VERSION needs zenity to run." # maybe we could translate this someday
    exit 1
fi

# Check if Ghostscript is installed
if ! GS=$(which gs)
then
    $ZENITY --error --title="Compress PDF $VERSION" --text="$error_ghostscript"
    exit 1
fi

# Check if the user has selected any files
# if [ "x$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" = "x"  -o  "$#" = "0" ] # we double check. Remove the first part if you plan to manually invoke the script

if [ "$#" = "0" ] # removed nautilus specific variable to make the script compatible with other file managers
then
    $ZENITY --error --title="Compress PDF $VERSION" --text="$error_nofiles"
    exit 1
fi

# Check if we can properly parse the arguments
INPUT=("$@")
N=("$#")
if [ "${#INPUT[@]}" != "$N" ] # comparing the number of arguments the script is given with what it can count
then
    $ZENITY --error --title="Compress PDF $VERSION" # if we arrive here, there is something very messed
    exit 1
fi

# Check if all the arguments are proper PDF files
for ARG in "$@"
do
    IS_PDF=$(file --brief --mime-type "$ARG" | grep -i "/pdf") # ignoring case for 'pdf'; as far as I know, the slash before (sth/pdf) is universal mimetype output. In most cases we can even expect 'application/pdf' (portability issues?).
    if [ "x$IS_PDF" = x ]; then NOT_PDF=1; break; fi
done
if [ "x$NOT_PDF" != x ]
then
    $ZENITY --error --title="Compress PDF $VERSION" --text="$error_nopdf"
    exit 1
fi



# Everything is OK. We can go on.



# Ask the user to select an output format
selected_level=$($ZENITY --list --title="Compress PDF "$VERSION"" --text "$label_level" --radiolist --column "" --column "$optimization_level" TRUE "$level_default" FALSE "$level_screen" FALSE "$level_low" FALSE "$level_high" FALSE "$level_color" --height 250 --width 400)
if [ "$?" != "0"  -o  "x$selected_level" = x ]; then exit 1; fi

# Select the optimization level to use
case $selected_level in
    "$level_default")
        COMP_COMMAND="/default"
    ;;
    "$level_screen")
        COMP_COMMAND="/screen"
    ;;
    "$level_low")
        COMP_COMMAND="/ebook"
    ;;
    "$level_high")
        COMP_COMMAND="/printer"
    ;;
    "$level_color")
        COMP_COMMAND="/prepress"
    ;;
esac



# Choose output filename(s)
if [ $# -eq 1 ]
then
    pdf_file=$(basename "$1")
    suggested_filename=${pdf_file%.*}${filename_suffix}.${pdf_file##*.}
    output_filename=$($ZENITY --file-selection --save --confirm-overwrite --filename="$PWD/$suggested_filename" --title="$label_filename")
    if [ "$?" != "0"  -o  "x$output_filename" = x ]; then exit 1; fi
else
    filename_suffix=$($ZENITY --entry --title="Compress PDF $VERSION" --text="$label_suffix" --entry-text="$filename_suffix")
    if [ "$?" != "0" ]; then exit 1; fi
    if [ "x$filename_suffix" = x ]
        then if ! $ZENITY --warning --title="Compress PDF $VERSION" --text="$warning_overwrite"; then exit 1; fi
    fi
    case "$filename_suffix" in */*) $ZENITY --error --title="Compress PDF $VERSION"; exit 1; esac # Check if the specified suffix is legal (we use 'case' instead of 'if' to directly use asterisk * globbing -- and avoid [[...]] for portability)
fi



# Finally, we process the files



for arg in "$@" # this processing is partly inspired by Edouard Saintesprit's patch from Compress PDF page at Launchpad
do
    if [ $# -ne 1 ]
    then
        pdf_file=$(basename "$arg")
        output_filename=${pdf_file%.*}${filename_suffix}.${pdf_file##*.}
    fi

    output_name=$(basename "$output_filename")

    temp_pdfmarks=tmp-compresspdf-$output_name-pdfmarks
    temp_filename=tmp-compresspdf-$output_name

    if [ -e $temp_pdfmarks  -o  -e $temp_filename ]; then $ZENITY --error --title="Compress PDF $VERSION"; exit 1; fi

    # Extract metadata from the original PDF. This is not a crucial functionality, but maybe we could warn if pdfinfo or sed are not available
    pdfinfo "$arg" | sed -e 's/^ *//;s/ *$//;s/ \{1,\}/ /g' -e 's/^/  \//' -e '/CreationDate/,$d' -e 's/$/)/' -e 's/: / (/' > "$temp_pdfmarks"
    if ! grep /Title "$temp_pdfmarks"; then echo '  /Title ()' >> "$temp_pdfmarks"; fi # Warning: if the pdf has not defined a Title:, ghostscript makes a fontname become the title.
    # echo -e 0a'\n''  /Title ()''\n'.'\n'w | ed afile # use to prepend instead of append
    sed -i '1s/^ /[/' "$temp_pdfmarks"
    sed -i '/:)$/d' "$temp_pdfmarks"
    echo "  /DOCINFO pdfmark" >> "$temp_pdfmarks"

    # Execute ghostscript while showing a progress bar
    (
        $GS -sDEVICE=pdfwrite -dPDFSETTINGS=$COMP_COMMAND -dColorConversionStrategy=/LeaveColorUnchanged -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -dSAFER -sOutputFile="$temp_filename" "$arg" "$temp_pdfmarks" & echo -e "$!\n"
        # we output the pid so that it passes the pipe; the explicit linefeed starts the zenity progressbar pulsation
    ) | ( # the pipes create implicit subshells; marking them explicitly
        read PIPED_PID
        if $ZENITY --progress --pulsate --auto-close --title="Compress PDF $VERSION"
        then
            rm "$temp_pdfmarks"
            mv -f "$temp_filename" "$output_filename" & # we go on to the next file as fast as possible (this subprocess survives the end of the script, so it is even safer)
            notify-send "Compress PDF" "$output_name $job_done"
        else
            kill $PIPED_PID
            rm "$temp_pdfmarks"
            rm "$temp_filename"
            exit $COMPRESSPDF_BATCH_ABORT_ERR # Warning: it exits the subshell but not the script
        fi
    )
    if [ "$?" = "$COMPRESSPDF_BATCH_ABORT_ERR" ]; then break; fi # to break the loop in case we abort (zenity fails)
done

原始来源:https://launchpad.net/compress-pdf

脚本基于this分支,由oriolpont提供,并根据cipricus的建议进行了修改。

Oriolpont的修改版本带来了一些改进:

  • 由Yaron提供的希伯来语翻译
  • 基于Edouard Saintesprit的更改实现的多文件支持
  • 其他几个bash调整,使脚本更加安全(PID临时文件,用于GS的更安全模式,更多的初始检查)

截图

Compress


选项

在zenity GUI对话框中提供的选项对应于这些ghostscript设置:

  • dPDFSETTINGS=/screen(屏幕查看质量,72 dpi图像)
  • dPDFSETTINGS=/ebook(低质量,150 dpi图像)
  • dPDFSETTINGS=/printer(高质量,300 dpi图像)
  • dPDFSETTINGS=/prepress(高质量,保留颜色,300 dpi图像)
  • dPDFSETTINGS=/default(几乎与/screen相同)

更多选项和精细调整请参考网站: http://milan.kupcevic.net/ghostscript-ps-pdf/

来源:用于优化PDF的开源命令行工具

你可以在这里找到有关所使用选项的更多信息。Oriolpont的README提供了有关根据您的需求自定义脚本的额外细节。

安装

该脚本依赖于一些软件包。您可以使用以下命令进行安装:

sudo apt-get install ghostscript poppler-utils zenity

如果你不知道如何安装Nautilus脚本,只需按照这个指南操作: 我该如何安装Nautilus脚本?

在其他桌面环境上安装

该脚本不依赖于任何自定义Nautilus变量,可以与几乎支持自定义菜单操作的任何文件管理器一起使用。请参考您的文件管理器手册以了解如何安装您的脚本。XFCE用户可以在这里找到一个很好的教程。

当然,您也可以从命令行启动该脚本。基本用法:

compresspdf "filename.pdf"

Moonshiner


概述

使用Moonshiner,一个ghostscript的图形用户界面前端工具,可以实现对PDF输出的更高级控制。


安装

Moonshiner在官方软件仓库中不可用。不幸的是,我也没有找到官方的PPA。只有这个已经有一段时间没有更新的私有PPA。这里提供的.deb存档应该可以在最新的Ubuntu版本上使用,但不要完全相信我的话。

据我所知,从源代码直接安装moonshiner是最安全的方式。为了这样做,首先下载最新的修订版:

svn co https://moonshiner.svn.sourceforge.net/svnroot/moonshiner/trunk moonshiner_svn
cd $HOME/moonshiner_svn/

Moonshiner依赖于Python、PyGTK和Ghostscript。如果这些依赖已满足(在标准的Ubuntu安装中应该已经满足),您可以继续安装应用程序,方法如下:
sudo python setup.py install

或者使用Python的包管理工具(sudo apt-get install python-pip):
python setup.py sdist
sudo pip install $HOME/moonshiner_svn/dist/moonshiner-1.1svn.tar.gz

卸载moonshiner非常简单,只需使用pip命令即可:
sudo pip uninstall moonshiner

如果您选择第一种安装方法,您将需要手动删除所有已安装的文件。


使用方法

安装完成后,您应该能够从启动器或您喜欢的应用程序启动Moonshiner。界面非常直观易懂。要修改PDF文件,只需将文件拖放到界面上,更改所需的设置,然后点击开始处理!

图像选项卡下的三个独立调整器允许您根据需要微调DPI和质量设置:

http://i.imgur.com/Y5ziLnu.gif

我不会在这里深入讨论其他设置,但我鼓励你查阅官方文档以解答你可能有的任何问题。
自定义图标
我对那个走私者的图标不太喜欢,所以我自己创建了一个:

您可以在此处下载SVG文件here。该图标基于出色的Faenza图标集,并在相同许可证(GNU/GPL)下发布。


(1) 烧酒很棒; (2) 我已经找到了错误的原因:实际上脚本需要进行调整才能在Xfce/Thunar中运行(http://askubuntu.com/a/280623/47206):**第150行应该替换为**:`if [ -z "$pdf_file" ]; then`。我应该将这些信息添加到您的答案中还是您希望自己添加? - user47206
这是修改后的脚本:http://pastebin.com/eEWHCwK7 - user47206
@cipricus 我之前忽略了那个nautilus的参考,谢谢你!我找到了一个更更新的脚本版本,支持多文件,并且已经编辑了我的回答,包括了你的建议和新的脚本。 - Glutanimate
1@cipricus 最后一次更新。我讨厌丑陋的图标,所以我为moonshiner创建了一个新的图标。一定要去看看! - Glutanimate
很棒的回答!但也许将回答分成两个部分会更好,一个是关于Nautlius-Script的,另一个是关于Moonshiner的。尤其是最后一部分效果非常好! - math
摩恩赛纳表现得非常出色!请为Ubuntu打包。 - AmanicA
看起来Moonshiner已经不再提供了,所有的链接都失效了。 - user47206

幽灵脚本命令行
就这么简单:
ps2pdf "argument.pdf"

它将生成argument.pdf.pdf,一个文件,通常比原始文件小。

Nemo 脚本

打开一个文本编辑器并写入:

ps2pdf "$*"

将其保存到/home/youruser/.local/share/nemo/scripts,使用一个直观的名称,比如"减小PDF文件大小",不需要使用文件扩展名。
然后,应用chmod +x "减小PDF文件大小"命令。
这样,您只需打开Nemo,导航到文件,右键单击PDF文件,选择脚本,然后选择"减小PDF文件大小"。
副作用:它无法识别Samba共享,会生成一个新的损坏文件。