Skip to content

Instantly share code, notes, and snippets.

@YeapGuy
Forked from ParsaAlizadeh/env-wrapper.sh
Last active May 2, 2026 21:02
Show Gist options
  • Select an option

  • Save YeapGuy/3e35bdf0b8e2285bfceaa2ccb5ae29fe to your computer and use it in GitHub Desktop.

Select an option

Save YeapGuy/3e35bdf0b8e2285bfceaa2ccb5ae29fe to your computer and use it in GitHub Desktop.
Virtual printer for manually printing two sided
#!/bin/bash
# Place this file at /usr/lib/cups/hp-virtual, chown root:root and chmod 0744.
if [[ $# -eq 0 ]]; then
echo 'file hp-virtual:/ "HP Virtual Printer" "HP Virtual Printer"'
exit 0
fi
JOBID="$1"
USER="$2"
TITLE="$3"
COPIES="$4"
OPTIONS="$5"
INPUT_FILE="$6"
# 3. Create a temporary file for the PDF
filename=$(mktemp /tmp/hp-virtual.XXXXXXXX.pdf)
# 4. Handle Input (Crucial Fix)
# If $6 is provided, use it. If not, read from stdin.
if [ -n "$INPUT_FILE" ]; then
cat "$INPUT_FILE" > "$filename"
else
cat - > "$filename"
fi
# 5. Check if the file is actually a PDF
# Some apps send PostScript. If so, convert it on the fly.
file_type=$(file -b --mime-type "$filename")
if [[ "$file_type" == "application/postscript" ]]; then
ps2pdf "$filename" "${filename}.tmp"
mv "${filename}.tmp" "$filename"
fi
echo -e "\n[Obojstranná tlač] Tlačím nepárne strany..." > /dev/tty0
lp -s -d HP_LaserJet_1020 -t "${TITLE}-even" -o page-set=even -o orientation-requested=6 -o outputorder=reverse "${filename}"
echo -e "\n[Obojstranná tlač] Keď sa tlač dokončí, PRELOŽ vytlačené strany do podávača papiera (bez otáčania či preklápania) a STLAČ akékoľvek tlačidlo" > /dev/tty0
read -n 1 -s < /dev/tty0
echo -e "\n[Obojstranná tlač] Tlačím párne strany..." > /dev/tty0
lp -s -d HP_LaserJet_1020 -t "${TITLE}-odd" -o page-set=odd "${filename}"
rm -f "${filename}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment