chown in CUPS backend

Hi guys! I am quite new to FreeBSD. I have some questions about setting up my own backend for CUPS. The goal is to save the incoming print job into a PDF on a directory and change the owner of the created PDF file to the user sending the print job. My current progress looks something like this:

Code:
# Get the current username (cupsuser) and create the output directory path
output_directory="/home/PDF/${cupsuser}"

# Create the output directory if it doesn't exist
mkdir -p "${output_directory}"

# Generate a unique filename for each print job using job title and current date/time
current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
output_filename="${output_directory}/${jobtitle}_${current_datetime}.pdf"

# Save the incoming print job data to the specified file
cat - > "${output_filename}"

# Set appropriate permissions for the output directory and file
chmod -R 700 "${output_directory}"
chmod 600 "${output_filename}"
/usr/sbin/chown -R "${cupsuser}" "${output_directory}"

But I checked the error log, and it shows that
chmod: /home/PDF/student: Operation not permitted
and
chown: /home/PDF/student/cups_test.txt_2023-10-16_16-15-21.pdf: Operation not permitted

I believe this is caused by this script is ran by cups itself, so the file created is also owned by it, but it does not have the permission to change the file.

Any idea on how I could solve this?
 
Adds the cups group to the working directories:
chmod -R 770 "${output_directory}"
chown "${cupsuser}":cups "${output_directory}"


Will you need sticky-bit for users?
(I haven't played bash in a long time)
chmod +t /home/PDF
 
Unfortunately I am still encountering the same issue:
Code:
D [16/Oct/2023:21:20:15 +1100] [Job 125] chmod: /home/PDF/student: Operation not permitted
D [16/Oct/2023:21:20:15 +1100] [Job 125] chown: /home/PDF/student: Operation not permitted

I have updated my script to this, or am i misunderstanding you here:

Code:
# Get the current username (cupsuser) and create the output directory path
output_directory="/home/PDF/${cupsuser}"

# Create the output directory if it doesn't exist
mkdir -p "${output_directory}"

# Generate a unique filename for each print job using job title and current date/time
current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
output_filename="${output_directory}/${jobtitle}_${current_datetime}.pdf"

# Save the incoming print job data to the specified file
cat - > "${output_filename}"

# Set appropriate permissions for the output directory and file
# chmod -R 700 "${output_directory}"
chmod -R 770 "${output_directory}"
/usr/sbin/chown "${cupsuser}":cups "${output_directory}"
# chmod 600 "${output_filename}"
# /usr/sbin/chown -R "${cupsuser}" "${output_directory}"
 
Was the /home/PDF/user directory created? You may not have permissions to change permissions.

Outside the script:

ls -ld /home ls -ld /home/PDF chmod 770 /home/PDF chown root:cups /home chown root:cups /home/PDF
 
Unfortunately I am still getting the same error:
Code:
D [17/Oct/2023:07:54:58 +1100] [Job 131] chmod: /home/PDF/student: Operation not permitted
D [17/Oct/2023:07:54:58 +1100] [Job 131] chown: /home/PDF/student: Operation not permitted

Here's my output of ls -l for / and /home

Code:
root@rule58:/home # ls -l ../
total 31
drwxr-xr-x   9 root  cups    9 Oct  8 16:08 home

Code:
root@rule58:/home # ls -l
total 36
drwxrwx---  5 root           cups            5 Oct 16 14:17 PDF

The /home/PDF/{user} directory is created with the script, but they are already there the second time the backend is ran.
 
Without knowing the uid and gid of the executing process we can only guess what's happening.

I suggest you change your script to add the following at the start:
Code:
exec 2>/tmp/trace.$$
set -x
id
Then you can examine the trace file, know the uid and gid, and observe the sequence of the script execution.
 
Without knowing the uid and gid of the executing process we can only guess what's happening.

I suggest you change your script to add the following at the start:
Code:
exec 2>/tmp/trace.$$
set -x
id
Then you can examine the trace file, know the uid and gid, and observe the sequence of the script execution.

I have added that line to the relevant part, but I still couldn't find any useful information. I'll post some relevant segments of the error_log here (I have set the log level to debug),
Code:
D [17/Oct/2023:09:51:36 +1100] [Job 133] 3 filters for job:
D [17/Oct/2023:09:51:36 +1100] [Job 133] texttopdf (text/plain to application/pdf, cost 32)
D [17/Oct/2023:09:51:36 +1100] [Job 133] pdftopdf (application/pdf to application/vnd.cups-pdf, cost 66)
D [17/Oct/2023:09:51:36 +1100] [Job 133] - (application/vnd.cups-pdf to printer/PDFPrinter, cost 0)
D [17/Oct/2023:09:51:36 +1100] [Job 133] job-sheets=none,none
D [17/Oct/2023:09:51:36 +1100] [Job 133] argv[0]="PDFPrinter"
D [17/Oct/2023:09:51:36 +1100] [Job 133] argv[1]="133"
D [17/Oct/2023:09:51:36 +1100] [Job 133] argv[2]="student"
D [17/Oct/2023:09:51:36 +1100] [Job 133] argv[3]="cups_test.txt"
D [17/Oct/2023:09:51:36 +1100] [Job 133] argv[4]="1"
D [17/Oct/2023:09:51:36 +1100] [Job 133] argv[5]="finishings=3 number-up=1 print-color-mode=monochrome job-uuid=urn:uuid:1474ce18-1425-3a42-4029-15022960e597 job-originating-host-name={rule58 IP} date-time-at-creation= date-time-at-processing= time-at-creation=1697496696 time-at-processing=1697496696 document-name-supplied=cups_test.txt"
D [17/Oct/2023:09:51:36 +1100] [Job 133] argv[6]="/var/spool/cups/d00133-001"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[0]="CUPS_CACHEDIR=/var/cache/cups"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[1]="CUPS_DATADIR=/usr/local/share/cups"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[2]="CUPS_DOCROOT=/usr/local/share/doc/cups"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[3]="CUPS_REQUESTROOT=/var/spool/cups"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[4]="CUPS_SERVERBIN=/usr/local/libexec/cups"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[5]="CUPS_SERVERROOT=/usr/local/etc/cups"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[6]="CUPS_STATEDIR=/var/run/cups"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[7]="HOME=/var/spool/cups/tmp"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[8]="PATH=/usr/local/libexec/cups/filter:/usr/local/bin:/usr/local/sbin:/bin:/usr/bin"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[9]="SERVER_ADMIN=root@rule58"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[10]="SOFTWARE=CUPS/2.4.6"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[11]="TMPDIR=/var/spool/cups/tmp"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[12]="USER=root"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[13]="CUPS_MAX_MESSAGE=2047"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[14]="CUPS_SERVER=/var/run/cups/cups.sock"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[15]="CUPS_ENCRYPTION=IfRequested"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[16]="IPP_PORT=631"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[17]="CHARSET=utf-8"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[18]="LANG=en_US.UTF-8"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[19]="PPD=/usr/local/etc/cups/ppd/PDFPrinter.ppd"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[20]="CONTENT_TYPE=text/plain"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[21]="DEVICE_URI=pdf:/"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[22]="PRINTER_INFO="
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[23]="PRINTER_LOCATION="
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[24]="PRINTER=PDFPrinter"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[25]="PRINTER_STATE_REASONS=none"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[26]="CUPS_FILETYPE=document"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[27]="FINAL_CONTENT_TYPE=application/vnd.cups-pdf"
D [17/Oct/2023:09:51:36 +1100] [Job 133] envp[28]="AUTH_I****"
I [17/Oct/2023:09:51:36 +1100] [Job 133] Started filter /usr/local/libexec/cups/filter/texttopdf (PID 989)
I [17/Oct/2023:09:51:36 +1100] [Job 133] Started filter /usr/local/libexec/cups/filter/pdftopdf (PID 990)
I [17/Oct/2023:09:51:36 +1100] [Job 133] Started backend /usr/local/libexec/cups/backend/pdf (PID 991)
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-state-changed event...
D [17/Oct/2023:09:51:36 +1100] [Client 517] Returning IPP successful-ok for Send-Document (ipp://localhost:631/printers/PDFPrinter) from {rule58 IP}.
D [17/Oct/2023:09:51:36 +1100] [Client 517] Content-Length: 180
D [17/Oct/2023:09:51:36 +1100] [Client 517] cupsdSendHeader: code=200, type="application/ipp", auth_type=0
D [17/Oct/2023:09:51:36 +1100] [Client 517] con->http=0x806482800
D [17/Oct/2023:09:51:36 +1100] [Client 517] cupsdWriteClient error=0, used=0, state=HTTP_STATE_POST_SEND, data_encoding=HTTP_ENCODING_LENGTH, data_remaining=180, response=0x8064f75a0(IPP_STATE_DATA), pipe_pid=0, file=-1
D [17/Oct/2023:09:51:36 +1100] [Client 517] Writing IPP response, ipp_state=IPP_STATE_DATA, old wused=0, new wused=0
D [17/Oct/2023:09:51:36 +1100] [Client 517] bytes=0, http_state=0, data_remaining=180
D [17/Oct/2023:09:51:36 +1100] [Client 517] Flushing write buffer.
D [17/Oct/2023:09:51:36 +1100] [Client 517] New state is HTTP_STATE_WAITING
D [17/Oct/2023:09:51:36 +1100] [Client 517] Waiting for request.
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Active clients and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] backend=/usr/local/libexec/cups/backend/pdf
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "backend=/usr/local/libexec/cups/backend/pdf", current level=INFO
D [17/Oct/2023:09:51:36 +1100] [Job 133] jobid=133
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "jobid=133", current level=INFO
D [17/Oct/2023:09:51:36 +1100] [Job 133] cupsuser=student
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "cupsuser=student", current level=INFO
D [17/Oct/2023:09:51:36 +1100] [Job 133] jobtitle=cups_test.txt
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "jobtitle=cups_test.txt", current level=INFO
D [17/Oct/2023:09:51:36 +1100] [Job 133] jobcopies=1
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "jobcopies=1", current level=INFO
D [17/Oct/2023:09:51:36 +1100] [Job 133] joboptions=finishings=3 number-up=1 print-color-mode=monochrome job-uuid=urn:uuid:1474ce18-1425-3a42-4029-15022960e597 job-originating-host-name={rule58 IP} date-time-at-creation= date-time-at-processing= time-at-creation=1697496696 time-at-processing=1697496696 document-name-supplied=cups_test.txt
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "joboptions=finishings=3 number-up=1 print-color-mode=monochrome job-uuid=urn:uuid:1474ce18-1425-3a42-4029-15022960e597 job-originating-host-name={rule58 IP} date-time-at-creation= date-time-at-processing= time-at-creation=1697496696 time-at-processing=1697496696 document-name-supplied=cups_test.txt", current level=INFO
D [17/Oct/2023:09:51:36 +1100] [Job 133] jobfile=
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "jobfile=", current level=INFO
D [17/Oct/2023:09:51:36 +1100] [Job 133] printtime=
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "printtime=", current level=INFO
X [17/Oct/2023:09:51:36 +1100] [Job 133] This is a \"emergency\" level log message
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Set job-printer-state-message to "This is a \"emergency\" level log message", current level=EMERG
A [17/Oct/2023:09:51:36 +1100] [Job 133] This is a \"alert\" level log message
C [17/Oct/2023:09:51:36 +1100] [Job 133] This is a \"critical\" level log message
E [17/Oct/2023:09:51:36 +1100] [Job 133] This is a \"error\" level log message
D [17/Oct/2023:09:51:36 +1100] [Job 133] WARN:   This is a \"warn\" level log message
N [17/Oct/2023:09:51:36 +1100] [Job 133] This is a \"notice\" level log message
D [17/Oct/2023:09:51:36 +1100] [Job 133] This is a \"info\" level log message
D [17/Oct/2023:09:51:36 +1100] [Job 133] This is a 2nd \"info\" level log message
D [17/Oct/2023:09:51:36 +1100] [Job 133] This is a 3rd \"info\" level log message
D [17/Oct/2023:09:51:36 +1100] [Job 133] This is a \"debug\" level log message
D [17/Oct/2023:09:51:36 +1100] [Job 133] pdftopdf: Last filter determined by the PPD: -; FINAL_CONTENT_TYPE: application/vnd.cups-pdf => pdftopdf will log pages in page_log.
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-progress event...
D [17/Oct/2023:09:51:36 +1100] Discarding unused printer-state-changed event...
D [17/Oct/2023:09:51:36 +1100] [Job 133] /usr/local/libexec/cups/backend/pdf: line 53: /: Is a directory
D [17/Oct/2023:09:51:36 +1100] [Client 517] HTTP_STATE_WAITING Closing for error 32 (Broken pipe)
D [17/Oct/2023:09:51:36 +1100] [Client 517] Closing connection.
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] [Job 133] Page = 595x842; 18,36 to 577,806
D [17/Oct/2023:09:51:36 +1100] [Job 133] No viable font found
W [17/Oct/2023:09:51:36 +1100] [Job 133] Ignored bad font \"monospace\"
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-progress event...
D [17/Oct/2023:09:51:36 +1100] Discarding unused printer-state-changed event...
D [17/Oct/2023:09:51:36 +1100] [Job 133] No viable font found
W [17/Oct/2023:09:51:36 +1100] [Job 133] Ignored bad font \"monospace\"
D [17/Oct/2023:09:51:36 +1100] [Job 133] No viable font found
W [17/Oct/2023:09:51:36 +1100] [Job 133] Ignored bad font \"ARPLUmingCN\"
E [17/Oct/2023:09:51:36 +1100] [Job 133] No usable font available
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-progress event...
D [17/Oct/2023:09:51:36 +1100] Discarding unused printer-state-changed event...
W [17/Oct/2023:09:51:36 +1100] [Job 133] temp file: file is damaged
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-progress event...
D [17/Oct/2023:09:51:36 +1100] Discarding unused printer-state-changed event...
W [17/Oct/2023:09:51:36 +1100] [Job 133] temp file: can\'t find startxref
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-progress event...
D [17/Oct/2023:09:51:36 +1100] Discarding unused printer-state-changed event...
W [17/Oct/2023:09:51:36 +1100] [Job 133] temp file: Attempting to reconstruct cross-reference table
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-progress event...
D [17/Oct/2023:09:51:36 +1100] Discarding unused printer-state-changed event...
D [17/Oct/2023:09:51:36 +1100] [Job 133] PID 989 (/usr/local/libexec/cups/filter/texttopdf) stopped with status 1.
E [17/Oct/2023:09:51:36 +1100] [Job 133] loadFile failed: temp file: unable to find trailer dictionary while recovering damaged file
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-progress event...
D [17/Oct/2023:09:51:36 +1100] Discarding unused printer-state-changed event...
D [17/Oct/2023:09:51:36 +1100] [Job 133] PID 990 (/usr/local/libexec/cups/filter/pdftopdf) stopped with status 1.
D [17/Oct/2023:09:51:36 +1100] [Job 133] PID 991 (/usr/local/libexec/cups/backend/pdf) exited with no errors.
D [17/Oct/2023:09:51:36 +1100] Discarding unused job-state-changed event...
E [17/Oct/2023:09:51:36 +1100] [Job 133] Job stopped due to filter errors; please consult the /var/log/cups/error_log file for details.
D [17/Oct/2023:09:51:36 +1100] cupsdMarkDirty(---J-)
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] cupsdSetBusyState: newbusy="Printing jobs and dirty files", busy="Printing jobs and dirty files"
D [17/Oct/2023:09:51:36 +1100] Discarding unused printer-state-changed event...
D [17/Oct/2023:09:51:37 +1100] [Job 133] Unloading...
D [17/Oct/2023:09:51:40 +1100] cupsdSetBusyState: newbusy="Active clients and dirty files", busy="Printing jobs and dirty files"

For reference, here is my full backend script. I have modified the script I got from here.

Bash:
#!/usr/local/bin/bash

#
# /usr/lib/cups/backend/2file
#
# (c) September 2007  Kurt Pfeifle <pfeifle@kde.org>
#                                  <kurt.pfeifle@infotec.com>
#     Network printing consultant Linux/Unix/Windows/Samba/CUPS
#
# License: GNU GPLv2 or GPLv3 (your choice)
# Warranty: None at all; you may need to fix included defects on your own.
#

backend=${0}
jobid=${1}
cupsuser=${2}
jobtitle=${3}
jobcopies=${4}
joboptions=${5}
jobfile=${6}

echo "INFO: backend=${backend}"       1>&2
echo "INFO: jobid=${jobid}"           1>&2
echo "INFO: cupsuser=${cupsuser}"     1>&2
echo "INFO: jobtitle=${jobtitle}"     1>&2
echo "INFO: jobcopies=${jobcopies}"   1>&2
echo "INFO: joboptions=${joboptions}" 1>&2
echo "INFO: jobfile=${jobfile}"       1>&2
echo "INFO: printtime=${printtime}"   1>&2
echo "EMERG:  This is a \"emergency\" level log message" 1>&2
echo "ALERT:  This is a \"alert\" level log message"     1>&2
echo "CRIT:   This is a \"critical\" level log message"  1>&2
echo "ERROR:  This is a \"error\" level log message"     1>&2
echo "WARN:   This is a \"warn\" level log message"      1>&2
echo "NOTICE: This is a \"notice\" level log message"    1>&2
echo "INFO:   This is a \"info\" level log message"      1>&2
echo "INFO:   This is a 2nd \"info\" level log message"  1>&2
echo "INFO:   This is a 3rd \"info\" level log message"  1>&2
echo "DEBUG:  This is a \"debug\" level log message"     1>&2

# we will read the output filename from the printers $DEVICE_URI environment
# variable that should look s.th. like "2file:/path/to/a/filename.suffix"

# Now do the real work:
case ${#} in
      0)
         # this case is for "backend discovery mode"
         echo "file pdf \"KDEPrint Devel Dept.\" \"pdf backend to test CUPS and help KDEPrint development\""
         exit 0
         ;;
      5)
           # backend needs to read from stdin if number of arguments is 5
         cat - > ${DEVICE_URI#pdf:}
         exec 2>/tmp/trace.$$
         set -x
         id
         # Get the current username (cupsuser) and create the output directory path
         output_directory="/home/PDF/${cupsuser}"

         # Create the output directory if it doesn't exist
         mkdir -p "${output_directory}"

         # Generate a unique filename for each print job using job title and current date/time
         current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
         output_filename="${output_directory}/${jobtitle}_${current_datetime}.pdf"

         # Save the incoming print job data to the specified file
         cat - > "${output_filename}"

         # Set appropriate permissions for the output directory and file
         # chmod -R 700 "${output_directory}"
         chmod -R 770 "${output_directory}"
         /usr/sbin/chown "${cupsuser}":cups "${output_directory}"
         # chmod 600 "${output_filename}"
         # /usr/sbin/chown -R "${cupsuser}" "${output_directory}"
         ;;
      6)
           # backend needs to read from file if number of arguments is 6
         cat ${6} > ${DEVICE_URI#pdf:}
         # Get the current username (cupsuser) and create the output directory path
         output_directory="/home/PDF/${cupsuser}"

         # Create the output directory if it doesn't exist
         mkdir -p "${output_directory}"

         # Generate a unique filename for each print job using job title and current date/time
         current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
         output_filename="${output_directory}/${jobtitle}_${current_datetime}.pdf"

         # Save the incoming print job data to the specified file
         cat {6} > "${output_filename}"

         # Set appropriate permissions for the output directory and file
         chmod -R 700 "${output_directory}"
         chmod 600 "${output_filename}"
         chown -R "${cupsuser}" "${output_directory}"
         ;;
      1|2|3|4|*)
         # these cases are unsupported
         echo " "
         echo " Usage: pdf job-id user title copies options [file]"
         echo " "
         echo " (Install as CUPS backend in /usr/lib/cups/backend/pdf)"
         echo " (Use as 'device URI' like \"pdf:/path/to/writeable/jobfile.suffix\" for printer installation.)"
         exit 0
esac

echo  1>&2

# we reach this line only if we actually "printed something"
echo "NOTICE: processed Job ${jobid} to file ${DEVICE_URI#pdf:}" 1>&2
echo "NOTICE: End of \"${0}\" run...."                             1>&2
echo "NOTICE: ---------------------------------------------------" 1>&2
echo  1>&2
exit 0
 
Are you saying that you can't find any files with name matching the pattern /tmp/trace.*?

That is where the evidence we need is likely to be...

Also, what's in the file /var/log/cups/error_log?
 
Are you saying that you can't find any files with name matching the pattern /tmp/trace.*?

That is where the evidence we need is likely to be...

Also, what's in the file /var/log/cups/error_log?

Sorry, I didn't know it was stored in /tmp/trace.*

Anyways, here is the content for it

Code:
root@rule58:~ # cat /tmp/trace.991
+ id
+ output_directory=/home/PDF/student
+ mkdir -p /home/PDF/student
++ date +%Y-%m-%d_%H-%M-%S
+ current_datetime=2023-10-17_09-51-36
+ output_filename=/home/PDF/student/cups_test.txt_2023-10-17_09-51-36.pdf
+ cat -
+ chmod -R 770 /home/PDF/student
chmod: /home/PDF/student: Operation not permitted
+ /usr/sbin/chown student:cups /home/PDF/student
chown: /home/PDF/student: Operation not permitted
+ echo

+ echo 'NOTICE: processed Job 133 to file /'
NOTICE: processed Job 133 to file /
+ echo 'NOTICE: End of "/usr/local/libexec/cups/backend/pdf" run....'
NOTICE: End of "/usr/local/libexec/cups/backend/pdf" run....
+ echo 'NOTICE: ---------------------------------------------------'
NOTICE: ---------------------------------------------------
+ echo

+ exit 0

The /var/log/cups/error_log file contains the content that I have included in my previous reply.
 
root@rule58:/home # ls -l ../
total 31
drwxr-xr-x 9 root cups 9 Oct 8 16:08 home


CUPS will also need writing for home. (u=rwx, g=rwx, other=r-x)
chmod g+w /home
 
The change I suggested to your script needs a fix to catch the output of the id(1) command:
Code:
exec 2>/tmp/trace.$$
set -x
id 1>&2
Never change the ownership or modes of / or /home to suit the needs of any application. They are owned by Unix, and changing them could break things in innumerable ways. They should always be like this (/home is usually a symbolic link -> /usr/home):
Code:
[strand.136] $ ls -lad / /home /usr/home
drwxr-xr-x  22 root  wheel  28 Oct 16 16:14 /
lrwxr-xr-x   1 root  wheel   8 Aug 30  2021 /home -> usr/home
drwxr-xr-x   4 root  wheel   4 Aug 25 11:45 /usr/home
To revert these directories to their correct ownerships and modes, as root:
Code:
chown root:wheel /
chmod 755 /
chown root:wheel /usr/home
chmod 755 /usr/home
Then rm /tmp/trace.*, run your script again, and show us the output of the trace file.

Edit: after doing all the above, show us the output of the this command:ls -laR /home/PDF
 
The change I suggested to your script needs a fix to catch the output of the id(1) command:
Code:
exec 2>/tmp/trace.$$
set -x
id 1>&2
Never change the ownership or modes of / or /home to suit the needs of any application. They are owned by Unix, and changing them could break things in innumerable ways. They should always be like this (/home is usually a symbolic link -> /usr/home):
Code:
[strand.136] $ ls -lad / /home /usr/home
drwxr-xr-x  22 root  wheel  28 Oct 16 16:14 /
lrwxr-xr-x   1 root  wheel   8 Aug 30  2021 /home -> usr/home
drwxr-xr-x   4 root  wheel   4 Aug 25 11:45 /usr/home
To revert these directories to their correct ownerships and modes, as root:
Code:
chown root:wheel /
chmod 755 /
chown root:wheel /usr/home
chmod 755 /usr/home
Then rm /tmp/trace.*, run your script again, and show us the output of the trace file.

Edit: after doing all the above, show us the output of the this command:ls -laR /home/PDF

I have applied the changes, and this is the /tmp/trace file
Code:
root@rule58:/home # cat /tmp/trace.27752
+ id
uid=193(cups) gid=193(cups) groups=193(cups)
+ output_directory=/home/PDF/student
+ mkdir -p /home/PDF/student
++ date +%Y-%m-%d_%H-%M-%S
+ current_datetime=2023-10-18_09-34-29
+ output_filename=/home/PDF/student/cups_test.txt_2023-10-18_09-34-29.pdf
+ cat -
+ chmod -R 770 /home/PDF/student
+ /usr/sbin/chown student:cups /home/PDF/student
chown: /home/PDF/student: Operation not permitted
+ echo

+ echo 'NOTICE: processed Job 138 to file /'
NOTICE: processed Job 138 to file /
+ echo 'NOTICE: End of "/usr/local/libexec/cups/backend/pdf" run....'
NOTICE: End of "/usr/local/libexec/cups/backend/pdf" run....
+ echo 'NOTICE: ---------------------------------------------------'
NOTICE: ---------------------------------------------------
+ echo

+ exit 0

and this is ls -laR /home/PDF
Code:
root@rule58:/home # ls -laR /home/PDF
total 10
drwxrwx---  4 root  cups   4 Oct 18 09:34 .
drwxr-xr-x  9 root  wheel  9 Oct  8 16:08 ..
drwxrwx---  2 cups  cups   3 Oct 18 09:34 remroot
drwxrwx---  2 cups  cups   3 Oct 18 09:34 student

/home/PDF/remroot:
total 2
drwxrwx---  2 cups  cups  3 Oct 18 09:34 .
drwxrwx---  4 root  cups  4 Oct 18 09:34 ..
-rwxrwx---  1 cups  cups  0 Oct 18 09:34 cups_test.txt_2023-10-18_09-34-33.pdf

/home/PDF/student:
total 2
drwxrwx---  2 cups  cups  3 Oct 18 09:34 .
drwxrwx---  4 root  cups  4 Oct 18 09:34 ..
-rwxrwx---  1 cups  cups  0 Oct 18 09:34 cups_test.txt_2023-10-18_09-34-29.pdf
 
I am not sure. But I added the line cups ALL=(ALL) NOPASSWD: /usr/sbin/chown with visudo as a test, but it still says Operation not permitted. But I am not sure if it actually gave permission to run chown tho.
 
A user (cups in this case) cannot set the ownership of a file to another user. Only root can do this.

Code:
dice@fbsd-test:~/temp % touch hello
dice@fbsd-test:~/temp % ls -al
total 10
drwxr-xr-x  2 dice  dice   3 Oct 18 11:30 .
drwxr-xr-x  6 dice  dice  19 Oct 18 11:30 ..
-rw-r--r--  1 dice  dice   0 Oct 18 11:30 hello
dice@fbsd-test:~/temp % chown rctest hello
chown: hello: Operation not permitted

But I added the line cups ALL=(ALL) NOPASSWD: /usr/sbin/chown with visudo as a test, but it still says Operation not permitted.
You have to sudo(8) the command, i.e. sudo chown ..... Those sudoers rules aren't going to magically apply without invoking sudo(8).
 
A user (cups in this case) cannot set the ownership of a file to another user. Only root can do this.

Code:
dice@fbsd-test:~/temp % touch hello
dice@fbsd-test:~/temp % ls -al
total 10
drwxr-xr-x  2 dice  dice   3 Oct 18 11:30 .
drwxr-xr-x  6 dice  dice  19 Oct 18 11:30 ..
-rw-r--r--  1 dice  dice   0 Oct 18 11:30 hello
dice@fbsd-test:~/temp % chown rctest hello
chown: hello: Operation not permitted


You have to sudo(8) the command, i.e. sudo chown ..... Those sudoers rules aren't going to magically apply without invoking sudo(8).

This solved the issue for me. Thank you so much!
 
Hi! I have another problem, not sure whether to start another new thread or not. But I can't seem to redirect the output from stdin to a file (might be due to filter error, at least from what I can see from the logs). I can see that the text file passed in is stored in /var/spool/cups/{d0005*-001}, and I could cat to see the text file just fine. However, when I am trying to save the output of the print job to the directory, the output file will just be empty. Any idea on how could I solve this issue?

Code:
W [19/Oct/2023:20:12:50 +1100] [Job 58] Ignored bad font \"monospace\"
W [19/Oct/2023:20:12:50 +1100] [Job 58] Ignored bad font \"monospace\"
W [19/Oct/2023:20:12:50 +1100] [Job 58] Ignored bad font \"ARPLUmingCN\"
E [19/Oct/2023:20:12:50 +1100] [Job 58] No usable font available
W [19/Oct/2023:20:12:50 +1100] [Job 58] temp file: file is damaged
W [19/Oct/2023:20:12:50 +1100] [Job 58] temp file: can\'t find startxref
W [19/Oct/2023:20:12:50 +1100] [Job 58] temp file: Attempting to reconstruct cross-reference table
E [19/Oct/2023:20:12:50 +1100] [Job 58] loadFile failed: temp file: unable to find trailer dictionary while recovering damaged file
E [19/Oct/2023:20:12:50 +1100] [Job 58] Job stopped due to filter errors; please consult the /var/log/cups/error_log file for details.
D [19/Oct/2023:20:12:50 +1100] [Job 58] The following messages were recorded from 20:12:50 to 20:12:50
D [19/Oct/2023:20:12:50 +1100] [Job 58] Applying default options...
D [19/Oct/2023:20:12:50 +1100] [Job 58] Adding start banner page "none".
D [19/Oct/2023:20:12:50 +1100] [Job 58] Queued on "PDFPrinter" by "remroot".
D [19/Oct/2023:20:12:50 +1100] [Job 58] Auto-typing file...
D [19/Oct/2023:20:12:50 +1100] [Job 58] Request file type is text/plain.
D [19/Oct/2023:20:12:50 +1100] [Job 58] File of type text/plain queued by "remroot".
D [19/Oct/2023:20:12:50 +1100] [Job 58] Adding end banner page "none".
D [19/Oct/2023:20:12:50 +1100] [Job 58] time-at-processing=1697706770
D [19/Oct/2023:20:12:50 +1100] [Job 58] 4 filters for job:
D [19/Oct/2023:20:12:50 +1100] [Job 58] texttopdf (text/plain to application/pdf, cost 32)
D [19/Oct/2023:20:12:50 +1100] [Job 58] pdftopdf (application/pdf to application/vnd.cups-pdf, cost 66)
D [19/Oct/2023:20:12:50 +1100] [Job 58] pdftops (application/vnd.cups-pdf to application/vnd.cups-postscript, cost 100)
D [19/Oct/2023:20:12:50 +1100] [Job 58] - (application/vnd.cups-postscript to printer/PDFPrinter, cost 0)
D [19/Oct/2023:20:12:50 +1100] [Job 58] job-sheets=none,none
D [19/Oct/2023:20:12:50 +1100] [Job 58] argv[0]="PDFPrinter"
D [19/Oct/2023:20:12:50 +1100] [Job 58] argv[1]="58"
D [19/Oct/2023:20:12:50 +1100] [Job 58] argv[2]="remroot"
D [19/Oct/2023:20:12:50 +1100] [Job 58] argv[3]="cups_test.txt"
D [19/Oct/2023:20:12:50 +1100] [Job 58] argv[4]="1"
D [19/Oct/2023:20:12:50 +1100] [Job 58] argv[5]="finishings=3 number-up=1 job-uuid=urn:uuid:0ae90c75-7f0a-3f3a-5853-ca0b711c6fa2 job-originating-host-name={RULE58 IP} date-time-at-creation= date-time-at-processing= time-at-creation=1697706770 time-at-processing=1697706770 document-name-supplied=cups_test.txt"
D [19/Oct/2023:20:12:50 +1100] [Job 58] argv[6]="/var/spool/cups/d00058-001"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[0]="CUPS_CACHEDIR=/var/cache/cups"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[1]="CUPS_DATADIR=/usr/local/share/cups"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[2]="CUPS_DOCROOT=/usr/local/share/doc/cups"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[3]="CUPS_FONTPATH=/usr/local/share/cups/fonts"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[4]="CUPS_REQUESTROOT=/var/spool/cups"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[5]="CUPS_SERVERBIN=/usr/local/libexec/cups"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[6]="CUPS_SERVERROOT=/usr/local/etc/cups"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[7]="CUPS_STATEDIR=/var/run/cups"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[8]="HOME=/var/spool/cups/tmp"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[9]="PATH=/usr/local/libexec/cups/filter:/usr/local/bin:/usr/local/sbin:/bin:/usr/bin"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[10]="SERVER_ADMIN=root@rule58.caia.swin.edu.au"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[11]="SOFTWARE=CUPS/2.3.3"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[12]="TMPDIR=/var/spool/cups/tmp"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[13]="USER=root"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[14]="CUPS_MAX_MESSAGE=2047"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[15]="CUPS_SERVER=/var/run/cups/cups.sock"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[16]="CUPS_ENCRYPTION=IfRequested"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[17]="IPP_PORT=631"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[18]="CHARSET=utf-8"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[19]="LANG=en_US.UTF-8"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[20]="PPD=/usr/local/etc/cups/ppd/PDFPrinter.ppd"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[21]="RIP_MAX_CACHE=128m"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[22]="CONTENT_TYPE=text/plain"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[23]="DEVICE_URI=pdf:/"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[24]="PRINTER_INFO="
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[25]="PRINTER_LOCATION="
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[26]="PRINTER=PDFPrinter"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[27]="PRINTER_STATE_REASONS=none"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[28]="CUPS_FILETYPE=document"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[29]="FINAL_CONTENT_TYPE=application/vnd.cups-postscript"
D [19/Oct/2023:20:12:50 +1100] [Job 58] envp[30]="AUTH_I****"
D [19/Oct/2023:20:12:50 +1100] [Job 58] Started filter /usr/local/libexec/cups/filter/texttopdf (PID 9641)
D [19/Oct/2023:20:12:50 +1100] [Job 58] Started filter /usr/local/libexec/cups/filter/pdftopdf (PID 9642)
D [19/Oct/2023:20:12:50 +1100] [Job 58] Started filter /usr/local/libexec/cups/filter/pdftops (PID 9643)
D [19/Oct/2023:20:12:50 +1100] [Job 58] Started backend /usr/local/libexec/cups/backend/pdf (PID 9644)
D [19/Oct/2023:20:12:50 +1100] [Job 58] Page = 595x842; 12,12 to 583,830
D [19/Oct/2023:20:12:50 +1100] [Job 58] pdftopdf: Last filter determined by the PPD: None; FINAL_CONTENT_TYPE: application/vnd.cups-postscript => pdftopdf will not log pages in page_log.
D [19/Oct/2023:20:12:50 +1100] [Job 58] backend=/usr/local/libexec/cups/backend/pdf
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "backend=/usr/local/libexec/cups/backend/pdf", current level=INFO
D [19/Oct/2023:20:12:50 +1100] [Job 58] jobid=58
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "jobid=58", current level=INFO
D [19/Oct/2023:20:12:50 +1100] [Job 58] cupsuser=remroot
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "cupsuser=remroot", current level=INFO
D [19/Oct/2023:20:12:50 +1100] [Job 58] jobtitle=cups_test.txt
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "jobtitle=cups_test.txt", current level=INFO
D [19/Oct/2023:20:12:50 +1100] [Job 58] jobcopies=1
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "jobcopies=1", current level=INFO
D [19/Oct/2023:20:12:50 +1100] [Job 58] joboptions=finishings=3 number-up=1 job-uuid=urn:uuid:0ae90c75-7f0a-3f3a-5853-ca0b711c6fa2 job-originating-host-name={RULE58 IP} date-time-at-creation= date-time-at-processing= time-at-creation=1697706770 time-at-processing=1697706770 document-name-supplied=cups_test.txt
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "joboptions=finishings=3 number-up=1 job-uuid=urn:uuid:0ae90c75-7f0a-3f3a-5853-ca0b711c6fa2 job-originating-host-name={RULE58 IP} date-time-at-creation= date-time-at-processing= time-at-creation=1697706770 time-at-processing=1697706770 document-name-supplied=cups_test.txt", current level=INFO
D [19/Oct/2023:20:12:50 +1100] [Job 58] jobfile=
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "jobfile=", current level=INFO
D [19/Oct/2023:20:12:50 +1100] [Job 58] printtime=
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "printtime=", current level=INFO
D [19/Oct/2023:20:12:50 +1100] [Job 58] pdftops - copying to temp print file \"/var/spool/cups/tmp/025ab653257f8\"
D [19/Oct/2023:20:12:50 +1100] [Job 58] /usr/local/libexec/cups/backend/pdf: line 53: /: Is a directory
D [19/Oct/2023:20:12:50 +1100] [Job 58] No viable font found
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "Ignored bad font \"monospace\"", current level=WARN
D [19/Oct/2023:20:12:50 +1100] [Job 58] No viable font found
D [19/Oct/2023:20:12:50 +1100] [Job 58] No viable font found
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "Ignored bad font \"ARPLUmingCN\"", current level=WARN
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "No usable font available", current level=ERROR
D [19/Oct/2023:20:12:50 +1100] [Job 58] PID 9641 (/usr/local/libexec/cups/filter/texttopdf) stopped with status 1.
D [19/Oct/2023:20:12:50 +1100] [Job 58] Hint: Try setting the LogLevel to "debug" to find out more.
D [19/Oct/2023:20:12:50 +1100] [Job 58] Input is empty, outputting empty file.
D [19/Oct/2023:20:12:50 +1100] [Job 58] Running with 5 args
D [19/Oct/2023:20:12:50 +1100] [Job 58] PID 9643 (/usr/local/libexec/cups/filter/pdftops) exited with no errors.
D [19/Oct/2023:20:12:50 +1100] [Job 58] PID 9642 (/usr/local/libexec/cups/filter/pdftopdf) stopped with status 1.
D [19/Oct/2023:20:12:50 +1100] [Job 58] Hint: Try setting the LogLevel to "debug" to find out more.
D [19/Oct/2023:20:12:50 +1100] [Job 58] chmod: /home/PDF/remroot: Operation not permitted
D [19/Oct/2023:20:12:50 +1100] [Job 58] processed Job 58 to file /
D [19/Oct/2023:20:12:50 +1100] [Job 58] End of \"/usr/local/libexec/cups/backend/pdf\" run....
D [19/Oct/2023:20:12:50 +1100] [Job 58] ---------------------------------------------------
D [19/Oct/2023:20:12:50 +1100] [Job 58] PID 9644 (/usr/local/libexec/cups/backend/pdf) exited with no errors.
D [19/Oct/2023:20:12:50 +1100] [Job 58] End of messages
D [19/Oct/2023:20:12:50 +1100] [Job 58] printer-state=3(idle)
D [19/Oct/2023:20:12:50 +1100] [Job 58] printer-state-message="---------------------------------------------------"
D [19/Oct/2023:20:12:50 +1100] [Job 58] printer-state-reasons=none
 
The code would be something like I mentioned above. I'll just repost it here for reference.

Bash:
#!/usr/local/bin/bash

backend=${0}
jobid=${1}
cupsuser=${2}
jobtitle=${3}
jobcopies=${4}
joboptions=${5}
jobfile=${6}

echo "INFO: backend=${backend}"       1>&2
echo "INFO: jobid=${jobid}"           1>&2
echo "INFO: cupsuser=${cupsuser}"     1>&2
echo "INFO: jobtitle=${jobtitle}"     1>&2
echo "INFO: jobcopies=${jobcopies}"   1>&2
echo "INFO: joboptions=${joboptions}" 1>&2
echo "INFO: jobfile=${jobfile}"       1>&2
echo "INFO: printtime=${printtime}"   1>&2

case ${#} in
      0)
         # this case is for "backend discovery mode"
         echo "file pdf \"KDEPrint Devel Dept.\" \"pdf backend to test CUPS and help KDEPrint development\""
         exit 0
         ;;
      5)
           # backend needs to read from stdin if number of arguments is 5
         cat - > ${DEVICE_URI#pdf:}
         #exec 2>/tmp/trace.$$
         #set -x
         #id 1>&2
         # Get the current username (cupsuser) and create the output directory path
         output_directory="/home/PDF/${cupsuser}"

         # Create the output directory if it doesn't exist
         mkdir -p "${output_directory}"

         # Generate a unique filename for each print job using job title and current date/time
         current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
         output_filename="${output_directory}/${jobtitle}_${current_datetime}.pdf"

         # Save the incoming print job data to the specified file
         cat - > "${output_filename}"
         echo "INFO: Running with 5 args"       1>&2


         # Set appropriate permissions for the output directory and file
         chmod -R 770 "${output_directory}"

         sudo -n /usr/sbin/chown "${cupsuser}":cups "${output_directory}"
         sudo -n /usr/sbin/chown "${cupsuser}":cups "${output_filename}"
         ;;
      6)
           # backend needs to read from file if number of arguments is 6
         cat ${6} > ${DEVICE_URI#pdf:}
         exec 2>/tmp/trace.$$
         set -x
         id 1>&2
         # Get the current username (cupsuser) and create the output directory path
         output_directory="/home/PDF/${cupsuser}"

         # Create the output directory if it doesn't exist
         mkdir -p "${output_directory}"

         # Generate a unique filename for each print job using job title and current date/time
         current_datetime=$(date +"%Y-%m-%d_%H-%M-%S")
         output_filename="${output_directory}/${jobtitle}_${current_datetime}.pdf"

         # Save the incoming print job data to the specified file
         cat {6} > "${output_filename}"
         echo "INFO: Running with 6 args"       1>&2

         # Set appropriate permissions for the output directory and file
         chmod -R 700 "${output_directory}"

         sudo /usr/sbin/chown -R "${cupsuser}" "${output_directory}"
         sudo -n /usr/sbin/chown "${cupsuser}":cups "${output_filename}"
         ;;
      1|2|3|4|*)
         # these cases are unsupported
         echo " "
         echo " Usage: pdf job-id user title copies options [file]"
         echo " "
         echo " (Install as CUPS backend in /usr/lib/cups/backend/pdf)"
         echo " (Use as 'device URI' like \"pdf:/path/to/writeable/jobfile.suffix\" for printer installation.)"
         exit 0
esac

echo  1>&2

# we reach this line only if we actually "printed something"
echo "NOTICE: processed Job ${jobid} to file ${DEVICE_URI#pdf:}" 1>&2
echo "NOTICE: End of \"${0}\" run...."                             1>&2
echo "NOTICE: ---------------------------------------------------" 1>&2
echo  1>&2
exit 0

The problem is that it does not seem to output anything into the file. As far as I know, if no 6th argument is passed in, it will read the file from stdin (but I could be wrong on that, but at least that is what I understood from the example code). I was trying to output that to a file by using cat - > "${output_filename}", but this doesn't work. I tried using the print test page feature in the web client, but when I cat the output file it just doesn't show anything.
 
I'm not an expert in CUPS, it keeps showing permission errors. Font issues...
I also don't know how you invoke the script and if stdin is available, or how you have the print network configured, "stopped with status = 1" seems like a filter configuration issue (use lpadmin() "-m raw" option).

And... "Hint: Try setting the LogLevel to "debug" to find out more."
cupsd.conf()
/usr/local/etc/cups/cupsd.conf and /var/log/cups/error_log

Do you have cups-filters/qpdf installed?
I don't know if cups-pdf is necessary.

Look at those lines:

D [19/Oct/2023:20:12:50 +1100] [Job 58] No viable font found
D [19/Oct/2023:20:12:50 +1100] [Job 58] No viable font found
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "Ignored bad font \"ARPLUmingCN\"", current level=WARN
D [19/Oct/2023:20:12:50 +1100] [Job 58] Set job-printer-state-message to "No usable font available", current level=ERROR
D [19/Oct/2023:20:12:50 +1100] [Job 58] PID 9641 (/usr/local/libexec/cups/filter/texttopdf) stopped with status 1.
D [19/Oct/2023:20:12:50 +1100] [Job 58] Hint: Try setting the LogLevel to "debug" to find out more.
D [19/Oct/2023:20:12:50 +1100] [Job 58] Input is empty, outputting empty file.
D [19/Oct/2023:20:12:50 +1100] [Job 58] Running with 5 args
D [19/Oct/2023:20:12:50 +1100] [Job 58] PID 9643 (/usr/local/libexec/cups/filter/pdftops) exited with no errors.
D [19/Oct/2023:20:12:50 +1100] [Job 58] PID 9642 (/usr/local/libexec/cups/filter/pdftopdf) stopped with status 1.
D [19/Oct/2023:20:12:50 +1100] [Job 58] Hint: Try setting the LogLevel to "debug" to find out more.
D [19/Oct/2023:20:12:50 +1100] [Job 58] chmod: /home/PDF/remroot: Operation not permitted
D [19/Oct/2023:20:12:50 +1100] [Job 58] processed Job 58 to file /
D [19/Oct/2023:20:12:50 +1100] [Job 58] End of \"/usr/local/libexec/cups/backend/pdf\" run....
 
Back
Top