Handy script to work with PDF files

PDF is commonly used printing file format. Creating & organising it might be a bit hard if without Acrobat Pro’s help.

Today I discovered some open source lib to manipulate PDF files, including Creating, Splitting, Merging, and Re-Ordering PDF pages.

Below is the script line for creating PDF,

echo 'Hello World!' | enscript -B -o - | ps2pdf - content.pdf

-B for enscript omit the header for the output


Here is the script for re-ordering the raw PDF into a book-style printable version.

#!/bin/bash

if [ $# -ne 2 ] ; then
    echo "Invalid input supplied, please choose input PDF and page counts"
    echo "    example : ./format.sh <input.pdf> <pageCount>"
    exit 1
fi

echo "Formatting begins ... "
file="$1"
pageCount="$2"

let "blankPageCount = $pageCount % 4"

# Check if blank page padding needed, if so create one
if [ $blankPageCount -ne 0 ] ; then
    echo ' ' | ps2pdf - blank.pdf 
fi

# setup each fold loop - one fold means one print page
let "foldCount = $pageCount / 4"
startFold=0
pageOrganizor=''

echo "    total $foldCount printing slides generated";
echo "    with $blankPageCount blank pages";

while [ $startFold -lt $foldCount ] ; do
    let "coverPrintpage = $startFold * 4 + 4"
    let "leftPrintpage = $startFold * 4 + 1"
    let "backPrintpage = $startFold * 4 + 3"
    pageOrganizor="$pageOrganizor A$coverPrintpage A$leftPrintpage-$backPrintpage"
    let "startFold += 1"
done

if [ $blankPageCount -eq 0 ] ; then
    cmd="pdftk A=$file cat$pageOrganizor output fmt_output.pdf"
elif [ $blankPageCount -eq 3 ] ; then
    let "blankStartPage = $pageCount-2"
    cmd="pdftk A=$file B=blank.pdf cat$pageOrganizor B A$blankStartPage-$pageCount output fmt_output.pdf"
elif [ $blankPageCount -eq 2 ] ; then
    let "blankStartPage = $pageCount-1"
    cmd="pdftk A=$file B=blank.pdf cat$pageOrganizor B A$blankStartPage A$pageCount B output fmt_output.pdf"
else
    cmd="pdftk A=$file B=blank.pdf cat$pageOrganizor B A$pageCount B B output fmt_output.pdf"
fi

echo "Merging pdfs ... "
$cmd

# cleanning up
echo "Cleanup and done"
if [ -e "blank.pdf" ] ; then
    rm blank.pdf
fi

sed & awk

sed is file line based processing. Each line is executed individually.

sed 's/(match_pattern)/(replace_pattern)'

awk is useful when trying to formatting the string.

awk -F(field separator) '(execute command)' file

(field separator) is defaultly set to be tab.
(execute command) will become very handy with print and $1, $2, etc.

Linux memory management

Study about memory management.(To be finished)
Memory control is critical for running the OS.
4 useful tools to investigate the memory status in Linux system,

  • cat /proc/meminfo

This command shows the current system memory map status. With a simple list, it describes the current situation of the machine.
Sample as below:
[ws_table id=”1″]

  • free

This is a simple command. Noted only the used for Cache is OS currently occupying.
Sample as below:
[ws_table id=”3″]

  • ps aux (combine with pmap)

As the previous blog page mentioned, it shows all the process of current machine. Useful for PID! Type:

Alternatively, we can also have,

Sample as below:
[ws_table id=”4″]

  • top & htop

This is the big star! Useful tips: c, f, r, t

Study about ps aux

The kill command is used in conjunction with a process ID (PID),

The killall command is used in conjunction with command names.

For command names and PIDs, the ps aux comes into use.

name@host:~/$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 231 0.0 0.0 0 0 ? D Apr30 0:16 [jbd2/sda5-8]
root 232 0.0 0.0 0 0 ? S&lt; Apr30 0:00 [ext4-dio-unwrit]
hans 312 0.0 0.0 0 0 ? ZN 13:29 0:00 [debconf-communi]
syslog 644 0.0 0.0 28940 1524 ? Sl Apr30 0:05 rsyslogd -c5

Descriptions as below

USER = user owning the process
PID = process ID of the process
%CPU = It is the CPU time used divided by the time the process has been running.
%MEM = ratio of the process’s resident set size to the physical memory on the machine
VSZ = virtual memory usage of entire process
RSS = resident set size, the non-swapped physical memory that a task has used
TTY = controlling tty (terminal)
STAT = multi-character process state
START = starting time or date of the process
TIME = cumulative CPU time
COMMAND = command with all its arguments

Using

name@host:~/$ ps aux | grep sublime

Should do the work to find the suitable process information to kill.