1 and 2 column pdfs work

This commit is contained in:
2026-01-22 18:18:08 -07:00
parent 4d1342782d
commit 23d8ce3910
9 changed files with 141 additions and 155 deletions

View File

@@ -7,34 +7,35 @@
.PH '''' .PH ''''
\# Centered block \# Centered block
.DS C .DS C
.HU "Your Bill" .HU "${COMPANY}'s Bill"
.DE .DE
\# Contact info \# Contact info
.TS .TS
nospaces center tab(;); nospaces center tab(;);
LR. LR.
Cooies Unlimited;(123) 456-7890 ${COMPANY_NAME};$PHONE
123 Sesame St.;cookie@example.com ${ADDR_LN_1};${EMAIL}
Dumpster 1;\*[DT] ${ADDR_LN_2};\*[DT]
Nicevile FL;Invoice ID #${INVOICE_NUM} ${ADDR_LN_3};Invoice ID #${INVOICE_NUM}
.TE .TE
\# Billing info
.TS .TS
nospaces center tab(;); nospaces center tab(;);
CbSS CbSS
CiCi|Ci CiCi|Ci
RN|N. RN|N.
${COMPANY} Work Performed
Job;Hours;Cost Job;Hours;Cost
_ _
${BODY} ${HRS_BODY}
_ _
.T& .T&
RbN|Nb. RbN|Nb.
${TOTAL} Total;${HRS_TOTAL}
.TE .TE
.DS C
.R
.DE
.PP .PP
Hours were charged at $${RATE}/hr. If you have any questions please reach out at any of the provided contacts above. Hours were charged at $${RATE}/hr. If you have any questions please reach out at any of the provided contacts above.
Thank you for your business! Thank you for your business!

62
2col.template.mm Normal file
View File

@@ -0,0 +1,62 @@
\# Make headers bold
.ds HF 3 3
\# Make lvl 1 headers 20pt, lvl 2 18pt
.ds HP 20 18
.S 12
\# Disable page header
.PH ''''
\# Centered block
.DS C
.HU "${COMPANY}'s Bill"
.DE
\# Contact info
.TS
nospaces center tab(;);
LR.
${COMPANY_NAME};$PHONE
${ADDR_LN_1};${EMAIL}
${ADDR_LN_2};\*[DT]
${ADDR_LN_3};Invoice ID #${INVOICE_NUM}
.TE
.2C
.TS
nospaces center tab(;);
CbSS
CiCi|Ci
RN|N.
Work Performed
Job;Hours;Cost
_
${HRS_BODY}
_
.T&
RiNi|Ni.
Sub-Total;${HRS_TOTAL}
.TE
.NCOL
.TS
nospaces center tab(;);
CbS
Ci|Ci
R|N.
Other
Item;Cost
_
${ITEM_BODY}
_
.T&
Ri|Ni.
Sub-Total;${ITEM_TOTAL}
.TE
.1C 1
.DS C
.PP
.B
Total: $${TOTAL}
.R
.DE
.PP
Hours were charged at $${RATE}/hr. If you have any questions please reach out at any of the provided contacts above.
Thank you for your business!

11
end.mm
View File

@@ -1,11 +0,0 @@
.1C 1
.DS C
.PP
.B
Total: $${TOTAL}
.R
.DE
.PP
Hours were charged at $${RATE}/hr. If you have any questions please reach out at any of the provided contacts above.
Thank you for your business!

147
gen.sh
View File

@@ -1,95 +1,76 @@
#!/bin/bash #!/bin/bash
# What you charge an hour # What you charge an hour
RATE=50 export RATE=50
export COMPANY_NAME="Cookies Unlimited, LLC"
export ADDR_LN_1="123 Sesasme St"
export ADDR_LN_2="Dumpster 1"
export ADDR_LN_3="12345, Nicevile, FL"
export PHONE="(123) 456-7890"
export EMAIL="cookie@exapmle.com"
short_help() { print_help() {
echo "$0 <hrs.csv> <items.csv> <Paying Company Name> [output file]" echo "$0 -1 <hrs.csv> <Paying Company Name> [output file]"
echo "--help for help" echo "$0 -2 <hrs.csv> <items.csv> <Paying Company Name> [output file]"
}
long_help() {
short_help
echo "" echo ""
echo "The csv file should be setup as:" echo "CSV files should be <name>,<value>."
echo "" echo "CSV files should have no empty line at the end."
echo "Job <string>, Hours <number>"
echo ""
echo "With no empty line at the end."
echo "Paying company name is any string" echo "Paying company name is any string"
echo "The output argument is optional and will by default output to invoice-<num>.pdf" echo "The output argument is optional and will by default output to invoice-<num>.pdf"
} }
get_invoice_num() {
CSV="" # Increment a global invoice number
if [[ -z $1 ]]; then INV_FILE="last_invoice_number"
short_help INV_NUM=0
exit if [[ -f $INV_FILE ]]; then
elif [[ $1 = "--help" ]]; then
long_help
exit
else
CSV=$1
fi
CSV2=""
if [[ -z $2 ]]; then
short_help
exit
else
CSV2=$2
fi
COMPANY=""
if [[ -z $3 ]]; then
short_help
exit
else
COMPANY=$3
fi
# Increment a global invoice number
INV_FILE="last_invoice_number"
INV_NUM=0
if [[ -f $INV_FILE ]]; then
INV_NUM=$(expr $(cat $INV_FILE) + 1) INV_NUM=$(expr $(cat $INV_FILE) + 1)
else else
INV_NUM=1 INV_NUM=1
fi fi
echo $INV_NUM > $INV_FILE echo $INV_NUM > $INV_FILE
echo $INV_NUM
}
read_hrs_csv() {
TMP=$(mktemp)
# Parse the CSV into: Label;Hrs;Cost
cat $1 | RATE=$RATE awk -F, '{ sum += $2 }; { print $1 ";" $2 ";" $2 * ENVIRON["RATE"] } END { print sum ";" sum * ENVIRON["RATE"] }' > $TMP
echo $TMP
}
read_items_csv() {
TMP=$(mktemp)
cat $1 | awk -F, '{ sum += $2 }; { print $1 ";" $2 } END { print sum }' > $TMP
echo $TMP
}
# 3rd argument is output location (optional) if [[ -z $1 ]]; then
OUTPUT="invoice-${INV_NUM}.pdf" print_help
if [[ ! -z $4 ]]; then exit
fi
export INVOICE_NUM=$(get_invoice_num)
OUTPUT="invoice-${INVOICE_NUM}.pdf"
HRS_TMP=$(read_hrs_csv $2)
export HRS_TOTAL=$(cat $HRS_TMP | tail -n 1)
export HRS_BODY=$(cat $HRS_TMP | head -n -1)
if [[ $1 = "-1" ]]; then
if [[ ! -z $4 ]]; then
OUTPUT=$4 OUTPUT=$4
fi
export COMPANY=$3
envsubst < 1col.template.mm | groff -t -mm -T pdf > $OUTPUT
elif [[ $1 = "-2" ]]; then
if [[ ! -z $5 ]]; then
OUTPUT=$5
fi
HRS_COST=$(cat $HRS_TMP | tail -n 1 | awk -F";" '{ print $2 }')
CONSUME_TMP=$(read_items_csv $3)
CONSUME_COST=$(cat $CONSUME_TMP | tail -n 1 | awk -F";" '{ print $2 }')
export ITEM_TOTAL=$(cat $CONSUME_TMP | tail -n 1)
export ITEM_BODY=$(cat $CONSUME_TMP | head -n -1)
export TOTAL=$(A=$HRS_COST B=$ITEM_TOTAL awk 'BEGIN{ print ENVIRON["A"] + ENVIRON["B"] }')
export COMPANY=$4
envsubst < 2col.template.mm | groff -t -mm -T pdf > $OUTPUT
else
echo First arg should be either -1 or -2
fi fi
# Parse the CSV into: Label;Hrs;Cost
HRS_TMP=$(mktemp)
cat $CSV | RATE=$RATE awk -F, '{ sum += $2 }; { print $1 ";" $2 ";" $2 * ENVIRON["RATE"] } END { print "Sub-Total" ";" sum ";" sum * ENVIRON["RATE"]}' > $HRS_TMP
CONSUME_TMP=$(mktemp)
cat $CSV2 | awk -F, '{ sum += $2 }; { print $1 ";" $2 } END { print "Sub-Total" ";" sum }' > $CONSUME_TMP
HRS_COST=$(cat $HRS_TMP | tail -n 1 | awk -F";" '{ print $3 }')
CONSUME_COST=$(cat $CONSUME_TMP | tail -n 1 | awk -F";" '{ print $2 }')
TOTAL_COST=$(A=$HRS_COST B=$CONSUME_COST awk 'BEGIN{ print ENVIRON["A"] + ENVIRON["B"] }')
# TOTAL = Last line
# BODY = Everything but the last line
COMPANY=$COMPANY \
INVOICE_NUM=$INV_NUM \
envsubst < start.mm > 1.tmp
HRS_TOTAL=$(cat $HRS_TMP | tail -n 1) \
HRS_BODY=$(cat $HRS_TMP | head -n -1) \
envsubst < hours.tbl > 2.tmp
ITEM_TOTAL=$(cat $CONSUME_TMP | tail -n 1) \
ITEM_BODY=$(cat $CONSUME_TMP | head -n -1) \
envsubst < item.tbl > 3.tmp
TOTAL=$TOTAL_COST \
RATE=$RATE \
envsubst < end.mm > 4.tmp
cat 1.tmp 2.tmp 3.tmp 4.tmp | groff -t -mm -T pdf > $OUTPUT
rm $HRS_TMP
rm $CONSUME_TMP

View File

@@ -1,14 +0,0 @@
.TS
nospaces center tab(;);
CbSS
CiCi|Ci
RN|N.
Work Performed
Job;Hours;Cost
_
${HRS_BODY}
_
.T&
RiNi|Ni.
${HRS_TOTAL}
.TE

View File

View File

@@ -1,15 +0,0 @@
.NCOL
.TS
nospaces center tab(;);
CbS
Ci|Ci
R|N.
Other
Item;Cost
_
${ITEM_BODY}
_
.T&
Ri|Ni.
${ITEM_TOTAL}
.TE

4
items.csv Normal file
View File

@@ -0,0 +1,4 @@
Prep,2
Cooking,1.25
Event,10
Cleanup,2.5
1 Prep 2
2 Cooking 1.25
3 Event 10
4 Cleanup 2.5

View File

@@ -1,22 +0,0 @@
\# Make headers bold
.ds HF 3 3
\# Make lvl 1 headers 20pt, lvl 2 18pt
.ds HP 20 18
.S 12
\# Disable page header
.PH ''''
\# Centered block
.DS C
.HU "${COMPANY}'s Bill"
.DE
\# Contact info
.TS
nospaces center tab(;);
LR.
Cooies Unlimited;(123) 456-7890
123 Sesame St.;cookie@example.com
Dumpster 1;\*[DT]
Nicevile FL;Invoice ID #${INVOICE_NUM}
.TE
.2C