Skip to content

Commit

Permalink
Update to PostScript version
Browse files Browse the repository at this point in the history
  • Loading branch information
gfolego committed Oct 10, 2017
1 parent e94766f commit 3aad4a6
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 37 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Please note that this is *saturday afternoon* quality code, *i.e.*, it is primar

### Requirements
```bash
sudo apt-get install file poppler-utils bc pdfjam pdftk
sudo apt-get install file poppler-utils bc ghostscript python
```

### Using
Expand All @@ -24,5 +24,6 @@ bash src/faas.sh example/input.pdf example/output.pdf
- Add personal handwriting style for numbers
- Add some stochastic variations

### Extra
Images in `res` directory were taken from [https://www.1001freedownloads.com/free-cliparts/?order=new&tag=handwritten](https://www.1001freedownloads.com/free-cliparts/?order=new&tag=handwritten), and converted to PDF with `inkscape --without-gui --export-pdf=1.pdf 1.svg` (for each file).
### Contributors (in alphabetical order)
- Bruno Ribeiro
- Guilherme Folego ([email protected])
Binary file removed res/1.pdf
Binary file not shown.
Binary file removed res/7.pdf
Binary file not shown.
Binary file removed res/8.pdf
Binary file not shown.
44 changes: 10 additions & 34 deletions src/faas.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@
# "1" for debug
DEBUG="0"

# Offset for pdfjam
OFFSET8="-225"
OFFSET1="-115"
OFFSET7="-108"

# Scale for pdfjam
SCALE8="0.035"
SCALE1="0.03"
SCALE7="0.03"



##################
Expand Down Expand Up @@ -85,9 +75,9 @@ fi
[[ "$DEBUG" == "1" ]] && echo "Temp dir: $tmpdir"


# Resources directory
resdir="$(dirname "$0")/../res"
[[ "$DEBUG" == "1" ]] && echo "Res bir: $resdir"
# Source directory
srcdir="$(dirname "$0")/"
[[ "$DEBUG" == "1" ]] && echo "Src bir: $srcdir"



Expand All @@ -109,30 +99,16 @@ pos=($(pdftotext "$infile" -bbox /dev/stdout |
grep ">-----</word>" -B1 |
grep ">[0-9][0-9]</word>" |
cut -f4,8 -d\" | sed s,\",+, | bc |
xargs -i echo "$size"*0.5-{}*0.5 | bc ))
xargs -i echo "$size"-{}*0.5 | bc ))
[[ "$DEBUG" == "1" ]] && printf "Pos: %s\n" "${pos[@]}"

# Process
inputps="$tmpdir"/input.ps
outputps="$tmpdir"/output.ps

# Create a PDF for each entry
for i in ${!pos[@]}; do
pdfjam --quiet --scale "$SCALE8" --offset "$OFFSET8 ${pos[i]}" "$resdir"/8.pdf --outfile "$tmpdir"/jam8-"$i".pdf
pdfjam --quiet --scale "$SCALE1" --offset "$OFFSET1 ${pos[i]}" "$resdir"/1.pdf --outfile "$tmpdir"/jam1-"$i".pdf
pdfjam --quiet --scale "$SCALE7" --offset "$OFFSET7 ${pos[i]}" "$resdir"/7.pdf --outfile "$tmpdir"/jam7-"$i".pdf
done

# Separate input pages
pdftk "$infile" cat 1 output "$tmpdir"/stamp-0.pdf
pdftk "$infile" cat 2 output "$tmpdir"/back.pdf

# Combine entries
for i in ${!pos[@]}; do
pdftk "$tmpdir"/jam8-"$i".pdf stamp "$tmpdir"/jam1-"$i".pdf output "$tmpdir"/tmp-"$i".pdf
pdftk "$tmpdir"/tmp-"$i".pdf stamp "$tmpdir"/jam7-"$i".pdf output "$tmpdir"/jamall-"$i".pdf
pdftk "$tmpdir"/stamp-"$i".pdf stamp "$tmpdir"/jamall-"$i".pdf output "$tmpdir"/stamp-"$((i+1))".pdf
done

# Generate final file
pdfunite "$tmpdir"/stamp-${#pos[@]}.pdf "$tmpdir"/back.pdf "$outfile"
pdf2ps "$infile" "$inputps"
python "$srcdir"/process.py "$inputps" "$outputps" "${pos[@]}"
ps2pdf "$outputps" "$outfile"

# Clean up
[[ "$DEBUG" != "1" ]] && rm -rf "$tmpdir"
Expand Down
111 changes: 111 additions & 0 deletions src/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/python


# process.py
# Copyright 2017
# Bruno Ribeiro
# Guilherme Folego ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.



import sys
import argparse


def parse_args(argv):
parser = argparse.ArgumentParser()

parser.add_argument('infile', metavar='input.ps', type=argparse.FileType('r'),
help='input PostScript file')
parser.add_argument('outfile', metavar='output.ps', type=argparse.FileType('w'),
help='output PostScript file')
parser.add_argument('pos', metavar='position', type=float, nargs='+',
help='positions for time entries')
parser.add_argument('-d', '--debug', action='store_true',
help='activate debug mode')
parser.add_argument('--start-string', help='string to be used as start time')
parser.add_argument('--end-string', help='string to be used as end time')


args = parser.parse_args(args=argv)
return args


def process(infile, outfile, pos, startstr, endstr, debug=False):
content = infile.read()

if (startstr == None):
startstr = '08:00'
if (endstr == None):
endstr = '17:00'

pos1 = content.find("COLABORADOR)Tj")
pos2 = content.rfind("<</Length", 0, pos1);
pos3 = content.find("endstream", pos1)

# everything before the first page
firstPart = content[:pos2]
# everything after the first page
lastPart = content[pos3:]
# first page data
content = content[pos2:pos3]
# skip the 'start of stream' marker
pos2 = content.find("stream")
content = content[pos2 + 6:]

extra = []
# define the current font
extra.append('/R12 9 Tf')

# Note: at the point this extra content is inserted, the coordinate system is reset to its
# default (i.e. origin is located at the bottom left corner of the page)

# fix the y positions
pos = [ y - 1 for y in pos ]

# create PostScript entries using some dark magic
for y in pos:
# write the start time
extra.append('68 ' + str(y) + ' moveto')
extra.append('(' + startstr + ') dup stringwidth pop 2 div neg 0 rmoveto show')
# write the end time
extra.append('186 ' + str(y) + ' moveto')
extra.append('(' + endstr + ') dup stringwidth pop 2 div neg 0 rmoveto show')

# insert any extra content at the end of the stream (later content has higher z-index)
pos1 = content.find('Q\nQ\n\n')
content = content[:pos1 + 5] + '\n' + '\n'.join(extra) + '\n' + content[pos1 + 5:]

outfile.write(firstPart)
outfile.write("<</Length ")
outfile.write(str(len(content)))
outfile.write(">>stream\n")
outfile.write(content)
outfile.write(lastPart)


# Main
def main(argv):

# Parse arguments
args = parse_args(argv)
if args.debug: print(args)

process(args.infile, args.outfile, args.pos, args.start_string, args.end_string, args.debug)


if __name__ == "__main__":
main(sys.argv[1:])

0 comments on commit 3aad4a6

Please sign in to comment.