Skip to content

Commit

Permalink
Add new pdf-truncate-batch-print action
Browse files Browse the repository at this point in the history
  • Loading branch information
goebbe committed Oct 25, 2024
1 parent 1a3bc94 commit 9a37c09
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pdf-truncate-batch-print@goebbe/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

Version 0.1 2024/10/24

- Initial release

53 changes: 53 additions & 0 deletions pdf-truncate-batch-print@goebbe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Truncate and batch-print PDF(s)
==============================

Cut PDF(s) pages at the start and from the end and batch-print them.

DESCRIPTION
-----------

This action allows you to cut the same number of pages from PDF(s) at the start and from the end
and batch-print (mass-print) the resulting truncated PDF(s) with the printer of your choice.

Use case: Batch processing when printing a large number of PDFs without cover-letter(s) and small print(s).

Example: Printing a large number of similar PDF-invoices. These PDFs all start with a cover-letter
and all end with two pages of the small print. The cover-letter and the small print should not be printed.
This action allows to batch print the pages between the cover-letter and the small print and only print
the interesting part (the actual invoices).

If `printer-driver-cups-pdf` is installed, it is also possible to print to PDF, to easily obtain truncated PDF(s).

INSTALLATION
------------
The easiest way to install a Nemo action is via "Menu > System Settings > Actions":

1. Download: In "Actions", go to "Download" > "Refresh" the list of available actions > select the action > press the download button
2. Enable: In "Actions", go to "Manage" > select the new action > press "+" to enable
3. It may be necessary to restart the Nemo file-manager


USAGE
------------

1. Select one or more PDF-files:
Right click on the selected PDF(s) and choose "Truncate and batch-print PDF(s)" from the context menu.
Note that this menu entry is only visible if the selected file(s) are actually PDF(s).
2. Type the number of pages to be truncated, at the start and from the end.
3. Choose the printer and choose between one-sided and two-sided printing.
Now the batch-print will start.
The same truncation will be applied to all PDF(s) during the batch-print.


DEPENDENCIES
------------

The following packages/ commands must be available:

* `zenity` for a simple GUI
* `lpr` for command line printing (part of cups)
* `lpstat`to get the available printers (part of cups)
* `pdfinfo` to get the total number of pages of a PDF(s) (part of poppler-utils)

On Debian based distributions,to install the required packages in the terminal, type:
sudo apt install zenity cups poppler-utils
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "Cut PDF(s) pages at the start and from the end and batch-print them",
"uuid": "pdf-truncate-batch-print@goebbe",
"name": "Truncate and batch-print PDF(s)",
"author": "goebbe",
"version": "0.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!/bin/bash

# Required packages on Debian based distributions: cups poppler-utils zenity
# sudo apt install cups poppler-utils zenity

# After installation this file should be in .local/share/nemo/actions/pdf-truncate-batch-print.sh
# This .sh-file has to be executable.

# Usage:
# 1. Select one or more PDF(s), right click, select: Truncate and mass-print PDF(s)
# 2. Choose the number of pages to be truncated. E.g. if you do not want the first page
# and the last two pages to be printed for all selected files, then
# set "# of pages cut at the start:" to 1 and "# of pages cut from the end:" to 2.
# 3. Choose the printer and between one-sided and two-sided printing.
# The same truncation will be applied to all pdfs during the mass print.

# This action could be convenient for batch-printing pdf(s) that e.g. all start
# with the same cover-letter and end with two pages of small print.

# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

## terminate script at the first nonzero exit code - make cancel in the first two dialogs terminate
set -e

## Text strings for zenity dialogs (translation friendly)
_TITLE=$"Truncate PDF-printing"
TITLE="$(/usr/bin/gettext "$_TITLE")"

#############################################
## 1. Choose number of pages to be truncated:
#############################################
_TEXT1=$"Number of pages that should NOT be printed.\n \
Empty fields result in no truncation."
TEXT1="$(/usr/bin/gettext "$_TEXT1")"

_ENTRY1=$"Number of pages cut at the start:"
ENTRY1="$(/usr/bin/gettext "$_ENTRY1")"

_ENTRY2=$"Number of pages cut from the end:"
ENTRY2="$(/usr/bin/gettext "$_ENTRY2")"

leftrighttrunc="$( \
zenity --forms --title="$TITLE" \
--width=300 --height=300 \
--text="$TEXT1" \
--separator="," \
--add-entry="$ENTRY1" \
--add-entry="$ENTRY2" \
)"

## Seperate the two numbers from $leftrighttrunc
lefttrunc=$(echo $leftrighttrunc | awk -F "," '{print $1}')
righttrunc=$(echo $leftrighttrunc | awk -F "," '{print $2}')

## If the truncate variables are empty: set their values to 0:
lefttrunc=${lefttrunc:=0}
righttrunc=${righttrunc:=0}

## if $lefttrunc or $righttrunc are not positive integers print an error message and exit
if [[ ! "$lefttrunc" =~ ^[0-9]+$ ]] || [[ ! "$righttrunc" =~ ^[0-9]+$ ]] ; then
zenity --error --text="The number of pages have to be zero or positive integers"; exit 1
fi

#############################################
## 2. Choose the printer:
#############################################
_TEXT2=$"Select a printer for batch-printing"
TEXT2="$(/usr/bin/gettext "$_TEXT2")"

_COLUMN=$"Printers:"
COLUMN="$(/usr/bin/gettext "$_COLUMN")"

## printernames is an array with the names of all available printers:
readarray -t printernames < <( lpstat -p | awk '$1=="printer" {print $2}')

myprinter="$( \
zenity --list --title="$TITLE" \
--width=300 --height=300 \
--text="$TEXT2" \
--column="$COLUMN" \
"${printernames[@]}" \
)"

## If $myprinter is empty: set $myprinter to the default printer:
defaultprinter=$(lpstat -d | awk '{print $4}')
myprinter=${myprinter:="$defaultprinter"}

#############################################
## 3. Choose between one-sided and two-sided printing:
#############################################

_TEXT3=$" <b>Summary:</b> \n \
Number of pages cut at the start: $lefttrunc \n \
Number of pages cut from the end: $righttrunc \n \
Printer: $myprinter \n \n \
Choose one-sided or two-sided printing \n \n \
<b>Batch-printing will start instantly! </b>"
TEXT3="$(/usr/bin/gettext "$_TEXT3")"

_BUTTON_ONE=$"one-sided"
BUTTON_ONE="$(/usr/bin/gettext "$_BUTTON_ONE")"

_BUTTON_CANCEL=$"Cancel"
BUTTON_CANCEL="$(/usr/bin/gettext "$_BUTTON_CANCEL")"

_BUTTON_TWO=$"two-sided"
BUTTON_TWO="$(/usr/bin/gettext "$_BUTTON_TWO")"

# unset exit on error for the following dialog
set +e

msides="$( \
zenity --question --title="$TITLE" \
--width=300 --height=300 \
--text="$TEXT3" \
--switch \
--extra-button "$BUTTON_ONE" \
--extra-button "$BUTTON_CANCEL" \
--extra-button "$BUTTON_TWO" \
)"

if [ "$msides" = "$BUTTON_ONE" ]; then mysides="one-sided"
elif [ "$msides" = "$BUTTON_TWO" ]; then mysides="two-sided-long-edge"
else exit 1
fi

## Debug: Uncomment the following line to check if the variables are generated correctly:
#echo "$lefttrunc" "$righttrunc" "$myprinter" "$msides" "$mysides" > ~/test2.txt

#############################################
## 4. Mass print the pdf(s) applying left- and righttruncation to each pdf:
#############################################
echo "$@" | while read -r file
do
## npage is the total number of pages:
numpages=$(pdfinfo "$file" | awk '/^Pages:/ {print $2}')
## firstp is the first page to be printed:
firstp=$(("$lefttrunc"+1))
## lastp is the last page to be printed:
lastp=$(("$numpages"-"$righttrunc"))
# only print if there is at least one page to print:
if (( (lpage-fpage) > -1 )); then
lpr -o sides="$mysides" -o ColorModel=Gray -o page-ranges="$firstp"-"$lastp" -P "$myprinter" "$file"
fi
done

exit 0

## EOF
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# TRUNCATE AND BATCH-PRINT PDF(S)
# This file is put in the public domain.
# goebbe, 2023
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: pdf-truncate-batch-print@goebbe 0.1\n"
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-actions/"
"issues\n"
"POT-Creation-Date: 2024-10-24 21:57+0200\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#. metadata.json->description
#. [email protected]_action.in->Comment
msgid "Cut PDF(s) pages at the start and from the end and batch-print them"
msgstr ""

#. metadata.json->name
#. [email protected]_action.in->Name
msgid "Truncate and batch-print PDF(s)"
msgstr ""

#. pdf-truncate-batch-print.sh:28
msgid "Truncate PDF-printing"
msgstr ""

#. pdf-truncate-batch-print.sh:34
msgid ""
"Number of pages that should NOT be printed.\\n Empty fields result in no "
"truncation."
msgstr ""

#. pdf-truncate-batch-print.sh:38
msgid "Number of pages cut at the start:"
msgstr ""

#. pdf-truncate-batch-print.sh:41
msgid "Number of pages cut from the end:"
msgstr ""

#. pdf-truncate-batch-print.sh:69
msgid "Select a printer for batch-printing"
msgstr ""

#. pdf-truncate-batch-print.sh:72
msgid "Printers:"
msgstr ""

#. pdf-truncate-batch-print.sh:94
#, sh-format
msgid ""
" <b>Summary:</b> \\n Number of pages cut at the start: $lefttrunc \\n Number "
"of pages cut from the end: $righttrunc \\n Printer: $myprinter \\n \\n "
"Choose one-sided or two-sided printing \\n \\n <b>Batch-printing will start "
"instantly! </b>"
msgstr ""

#. pdf-truncate-batch-print.sh:102
msgid "one-sided"
msgstr ""

#. pdf-truncate-batch-print.sh:105
msgid "Cancel"
msgstr ""

#. pdf-truncate-batch-print.sh:108
msgid "two-sided"
msgstr ""
3 changes: 3 additions & 0 deletions pdf-truncate-batch-print@goebbe/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"author": "goebbe"
}
16 changes: 16 additions & 0 deletions pdf-truncate-batch-print@goebbe/[email protected]_action.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Nemo Action]
_Name=Truncate and batch-print PDF(s)
_Comment=Cut PDF(s) pages at the start and from the end and batch-print them
#Batch processing for printing PDFs without cover-letter(s) or small print(s)
Icon-Name=document-print-symbolic
Exec=<pdf-truncate-batch-print@goebbe/pdf-truncate-batch-print.sh %F>

#propose action only for pdf-files:
Mimetypes=application/pdf;
#get one filepath per line:
Separator="\n"
#at least one file has to be selected:
Selection=notnone
#the following packages have to be installed
#zenity, lpr and lpstat are likely to be available by default, pdfinfo is part of poppler-utils
Dependencies=zenity;lpr;lpstat;pdfinfo;

0 comments on commit 9a37c09

Please sign in to comment.