Tuesday, March 28, 2017

manipulating pdf files for fun and profit

Ok so you have a pdf file

get the following  tools

1)pdftk
2)mutool (part of mupdf-tools package)


then I need to rotate it 90 degrees

 pdftk in-file.pdf cat 1-endeast output out-file.pdf

this rotated it 90 degrees

Then i needed to split the pdf which had pages 2 on one to one on one:

mutool poster -x 2 input.pdf output.pdf

but sometimes the pages are out of order (even and odd)
so then i can do

pdftk infile.pdf shuffle even odd output outfile.pdf


also useful will be

djvu2pdf by christoph sighart


http://0x2a.at/s/projects/djvu2pdf

which looks like


#!/bin/sh
# (c) Christoph Sieghart 2007 - 2011

VERSION=0.9.2

# Flags for commandline options
S_FLAG=0
C_FLAG=0

trap 'if [ $S_FLAG -eq 1 ]; then clean_temp_dir; clean_cursor; fi' 0 1 2

#*****************************************************************************
# - Functions
#*****************************************************************************

check_print_error() {
    if [ $? -ne 0 ]
    then
        echo "Error: $0: $1"
        exit 1
    fi
}

print_version() {
    echo "djvu2pdf $VERSION - Christoph Sieghart "
}

print_help() {
    print_version
    echo -e "\n  Usage: ./djvu2pdf filename.djvu [filename.djvu] ...\n"
    echo "  -h       Prints this help"
    echo "  -v       Prints the version number"
    echo "  -s       Show status messages (A little bit slower - every page"
    echo "           gets dumped on its own and later recombined"
    echo -e "  -c       Don't use terminal escape sequences to move cursor \n"
    exit 0
}

print_quiet() {
    if [ $S_FLAG -eq 1 ]; then
        echo -en $1
    fi
}

move_cursor() {
    if [ $S_FLAG -eq 1 ]; then
        if [ $C_FLAG -eq 1 ]; then
            echo -ne "\n"
            return
        else
            # cursor magic
            echo -en "\033[35D"
        fi
    fi
}

clean_cursor() {
    # if the cursor 'magic' messes anything up
    if [ $S_FLAG -eq 1 ]; then
        if [ $C_FLAG -eq 0 ]; then
            tput sgr0
        fi
    fi
}

clean_temp_files() {
    rm -rf "$TEMP"/* >/dev/null 2>&1
}

clean_temp_dir() {
    rm -rf "$TEMP" >/dev/null 2>&1
}

#*****************************************************************************
# - Programm checks
#*****************************************************************************

# MacOS and BSD compatibility
which seq >/dev/null 2>&1
if [ $? -eq 0 ]; then
    SEQ=seq
  else
    SEQ=jot
fi

for i in "ddjvu djvulibre" "gs ghostscript"; do
    BINARY=$(echo $i | awk '{print $1'})
    PACKAGE=$(echo $i | awk '{print $2'})
    which $BINARY >/dev/null 2>&1
    check_print_error "$BINARY not found. Install $PACKAGE."
done


#*****************************************************************************
# - Commandline options
#*****************************************************************************

while getopts hvcs opt
do
    case "$opt" in
        v) print_version
        exit 0;;
        h) print_help;;
        c) C_FLAG=1;;
        s) S_FLAG=1;;
        \?) exit 1;;
    esac
done

shift `expr $OPTIND - 1`

if [ $C_FLAG -eq 1 ]; then
    if [ $S_FLAG -eq 0 ]; then
        echo -e "Error: $0: The option -c only makes sense with -s"
        exit 1
    fi
fi

if [ -z "$1" ]; then
    echo -e "Error: $0: No file specified for conversion"
    exit 1
fi

#*****************************************************************************
# - Main loop
#*****************************************************************************

while [ $# -gt 0 ]; do

    if [ ! -f "$1" ]; then
        echo -e "Error: $0: File '$1' not found"
        exit 1
    fi

    FILENAME=$1
    FILEBASE=`basename "$FILENAME" .djvu`
    OUTPUTFILE="${FILEBASE}.pdf"

    if [ $S_FLAG -eq 0 ]; then
        ddjvu -format=pdf "${FILENAME}" "${OUTPUTFILE}" 2> /dev/null
    else

        #*****************************************************************************
        # - Temporary Files
        #*****************************************************************************

        TEMP="${TMPDIR:=/tmp}/djvu2pdf.$$"
        mkdir $TEMP
        check_print_error "Creating temporary directory failed!"

        # Child processes should use our temporary directory
        TMPDIR=$TEMP

        PAGES=`djvudump "$FILENAME" | grep pages | awk '{print $8;}'`
        ZEROCOUNT=$(expr `echo $PAGES | wc -m` - 1)
        COUNT=1
        TENS=1

        print_quiet "Converting $FILENAME to $OUTPUTFILE\n"

        #
        # We dump every page and print the status message


        for COUNT in `$SEQ 1 $PAGES`;
        do
            if [ `expr $COUNT / $TENS` -gt 0 ]
            then
                TENS=`expr $TENS \* 10`
                ZEROCOUNT=`expr $ZEROCOUNT - 1`
                ZEROS=$(for i in `$SEQ 1 $ZEROCOUNT`; do echo -n 0; done)
            fi

            ddjvu -format=pdf -page $COUNT "$FILENAME" "$TEMP/$FILEBASE.${ZEROS}$COUNT.pdf" 2> /dev/null

            print_quiet "Page $COUNT/$PAGES dumped"
            move_cursor
        done

        clean_cursor

        #
        # The pages get combined into one big happy .pdf
        #

        print_quiet "\nDumping finished - writing $OUTPUTFILE\n"

        gs -dSAFER -dQUIET -dBATCH -sDEVICE=pdfwrite -sOutputFile="$OUTPUTFILE" -DNOPAUSE "$TEMP/$FILEBASE".*.pdf
        check_print_error "Error in creating pdf file"

        clean_temp_files
    fi

    shift
done

exit 0

Thursday, January 28, 2016

how to connect an apache cgi-script to postgreql database





How do I connect to a local postgres database without password from a script within Apache
up vote 0 down vote favorite
   

I have a Python script that should load some data into postgres when a POST request is sent to Apache Webserver. In the script a system user (dbuser) is used to connect to the database (which works fine with psql). The script however cannot connect when it is executed within Apache, returning the following error:

Peer authentication failed for user dbuser

Is there a way to allow the script to connect without providing it the user password?
python apache postgresql cgi
shareimprove this question
   
asked Jun 4 '15 at 9:48
a1an
8791329
   
add a comment
1 Answer
active oldest votes
up vote 1 down vote
   

The solution I've found uses ident authentication with user maps.

The first thing to notice is that, although an username is provided in the script, when connecting via Apache, that user is used for peer authentication (which fails, requiring a password). However, the system user requesting access to postgresql is the one running Apache (namely www-data), thus enabling us to configure an user map, allowing is to authenticate to the server as another system user (thus leveraging ident authentication). Here follows the configuration files content:

In pg_ident.conf we configure the user map:

# MAPNAME      SYSTEM-USERNAME         PG-USERNAME
web            www-data                dbuser
web            dbuser                  dbuser

In pg_hba.conf we add the map as an option to the local peer authentication:

# "local" is for Unix domain socket connections only
# TYPE  DATABASE     USER    ADDRESS     METHOD
local   all          all                 peer map=web

After reloading the server, the script can access the database as if it was executed directly the the user "dbuser", without the need for a password.
shareimprove this answer

Saturday, January 2, 2016

too many files in directory need to split into subdirectories with 100 files


#!/usr/bin/perl -w
use strict;
use File::Copy;
my $fn;
my @total= (1 .. 100);
my $dir = shift;
opendir (my $dh , $dir);

my @files = grep { $_ ne '.' && $_ ne '..' } readdir $dh;

my @letters = ("a".."z") ;

MYLOOPS:
foreach my $let1 ( @letters ) {
    foreach my $let2 (@letters)   {
       mkdir "$dir$let1$let2";

for my $count (@total) {
$fn =  pop  @files;
if ( $fn ) {
  move ("$dir/$fn", "$dir$let1$let2/$fn");
} else  {
    last MYLOOPS;
}
   
} }}

Thursday, March 20, 2014


;; this should delete the last line 00:00

(let ((beg (point)))
(forward-line 0)
(delete-region beg (point)))

;;then do this

(goto-char (point-min))

this solves that first problem
then

then use this in autohotkey

make-emacs-top.ahk

SetTitleMatchMode 1
WinActivate emacs
WinSet, AlwaysOnTop, On, emacs
WinSet, Top, ,emacs
Return

Sunday, March 16, 2014

defun get-stuff ()
(interactive)
(shell-command '"c:/user/get.ahk")
(goto-char (point-min))
(delete-region
(re-search-forward "00\:00")
(point-max))
(goto-char 1)
(message " hi there meshulum"))

(defun clean-stuff ()
(interactive)
(goto-char (point-min))
(delete-region
(re-search-forward "00\:00")
(point-max))
(goto-char 1)
(message " hi there user"))

(defun both-stuff ()
(interactive)
(progn
(get-stuff)
(clean-stuff)
))





(defun put-stuff ()
(interactive)
(kill-ring-save (point-min) (point-max))
(shell-command '"c:/ure/put.ahk"))


put.ahk
SetTtileMatchMode 1
text=%Clipboard%
WinActivate Nuance
Send ^a
Send {Del}
Sleep 100
Clipboard = %text%
WinActivate Nuance
Send ^v
Return


get.ahk
SetTitleMatchMode 1
WinWait Nuance
WinGetText, text;
Clipboard = %text%
WinActivate eamcs
Send ^y
Return


Thursday, March 13, 2014

write interactive emacs functin

global-set-key (kbd "C-c C-g")
(lambda ()
(interactive)
(shell-command "/home/user/trial.pl")))


Sunday, March 9, 2014

ideas to capture to emacs and avoid nuance


SetTitleMatchMode 1
WinWait Nuance
WinGetText, text ;
MsgBox, the text is: `n%text%
WinActivate Nuance
Send ^a
Send {Del}
Sleep 100
Clipboard = %text%
WinActivate Nuance

Send ^v
Return