On Wed, 2010-06-02 at 14:27 -0400, David Burleigh wrote:
> Has anyone written a script to automate building booklets with 
> Multivalent? 

Will pdftk do instead of multivalent?

#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <sys/stat.h>

#define LINESCREEN 110

void CreateScript(FILE *fp, const char *pdfname, const char *buffer1,
const int totalpages, int low, int high);

void start_dos_colors(void);
void doscolor(const int color);
void header(void);
char* left(char *string, const int length);
char* right(char *string, const int length);
char* mid(char*string,const int start, int length); // the start number
starts from string position number 0
int monthtonumber(const char *);
bool YesOrNo(void);

int main(void)
        {
        FILE *fp;
        int counter,totalpages;
        char buffer1[500], temp[500];
        char impose[]="/opt/bargainhunter/barghntr/impose .scr";
        bool evenodd=false;

        initscr();
        noecho();
        cbreak();
        keypad(stdscr,TRUE);
        start_color();

        start_dos_colors();

        header();

        // print compile date and time on the main menu
        strcpy(buffer1,__DATE__);
        left(buffer1,3);
        counter=monthtonumber(buffer1);
        mvprintw(23,62,"Version %02d",counter);
        strcpy(buffer1,__DATE__);
        mid(buffer1,4,2);
        printw("%02d",atoi(buffer1));
        strcpy(buffer1,__DATE__);
        right(buffer1,2);
        printw("%02d",atoi(buffer1));
        strcpy(buffer1,__TIME__);
        left(buffer1,2);
        printw("%02d",atoi(buffer1));
        strcpy(buffer1,__TIME__);
        mid(buffer1,3,2);
        printw("%s",buffer1);
        // end of print compile date and time on main menu

        mvaddstr(7,23,"1. Impose Parkland Bargain Hunter");
        mvaddstr(9,23,"2. Impose Regina Bargain Hunter");
        mvaddstr(11,23,"3. Impose Saskatoon Bargain Hunter");
        mvaddstr(13,23,"4. RIP 11x17 Layout");
        mvaddstr(15,23,"5. Impose 8.5x11 Layout");
        mvaddstr(17,23,"6. Quit");
        refresh();

        counter=0;
        flushinp();
        while (counter < 49 || counter > 54)
                counter=getch();
        strcpy(buffer1,"/opt/bargainhunter/scribus/impose/");
        switch(counter)
                {
                case 49: // Impose Parkland Bargain Hunter
                        strcat(buffer1,"parkland/");
                        impose[34]='p';
                        break;
                case 50: // Impose Regina Bargain Hunter
                        strcat(buffer1,"regina/");
                        impose[34]='r';
                        break;
                case 51: // Impose Saskatoon Bargain Hunter
                        strcat(buffer1,"saskatoon/");
                        impose[34]='s';
                        break;
                case 52: // RIP 11x17 Layout
                        header();
                        mvaddstr(11,23,"RIP 11x17 Layout -- Proceed (Y/N)");
                        refresh();
                        if (!YesOrNo())
                                {
                                endwin();
                                return 0;
                                }
                        endwin();
                        printf("RIP 11x17 Layout\n");
                        printf("----------------\n\n");
                        printf("Counting pages . . .\n");
                        system("cat /sda/impose/11x17/paper.ps | grep 
\\%\\%Page\\: | wc -l
> /opt/bargainhunter/barghntr/pagecnt.tmp");
                        fp=fopen("/opt/bargainhunter/barghntr/pagecnt.tmp","r");
                        fgets(temp,4,fp);
                        fclose(fp);
                        remove("/opt/bargainhunter/barghntr/pagecnt.tmp");
                        totalpages=atoi(temp);
                        printf("Writing RIP...\n");
                        
fp=fopen("/opt/bargainhunter/barghntr/impose11.scr","w");
                        fprintf(fp,"#!/bin/bash\n");
                        fprintf(fp,"cd /sda/impose/11x17\n");
                        fprintf(fp,"ps2pdf -dPDFSETTINGS=/printer 
-sPAPERSIZE=ledger paper.ps
imposed.pdf\n");
                        fprintf(fp,"pdftk imposed.pdf cat ");
                        for (counter=1; counter < totalpages+1; counter++)
                                {
                                if (evenodd)
                                        fprintf(fp,"E ");
                                else
                                        fprintf(fp,"W ");
                                evenodd=!evenodd;
                                }
                        fprintf(fp,"output 11x17.pdf\n");
                        fprintf(fp,"rm imposed.pdf\n");
                        fprintf(fp,"gs -dNOPAUSE -sDEVICE=psmono -r1200 
-sPAPERSIZE=11x17
-dCOLORSCREEN -dDITHERPPI=%d -sOUTPUTFILE=temp.ps 11x17.pdf -c quit
\n",LINESCREEN);
                        fprintf(fp,"rm 11x17.pdf\n");
                        fprintf(fp,"ps2pdf temp.ps temp.pdf\n");
                        fprintf(fp,"pdftk temp.pdf cat ");
                        evenodd=false;
                        for (counter=1; counter < totalpages+1; counter++)
                                {
                                fprintf(fp,"%d",counter);
                                if (evenodd)
                                        fprintf(fp,"E ");
                                else
                                        fprintf(fp,"W ");
                                evenodd=!evenodd;
                                }
                        fprintf(fp,"output 11x17.pdf\n");
                        fprintf(fp,"rm temp.ps temp.pdf\n");
                        // PDF OPTIMIZATION STARTS HERE
                        fprintf(fp,"pdfopt 11x17.pdf opt.pdf\n");
                        fprintf(fp,"rm 11x17.pdf\n");
                        fprintf(fp,"mv opt.pdf 11x17.pdf\n");
                        // PDF OPTIMIZATION ENDS HERE
                        fprintf(fp,"chmod 666 *\n");
                        fclose(fp);
                        
chmod("/opt/bargainhunter/barghntr/impose11.scr",S_IRWXU |
S_IRWXG); // -rwxrwx---
                        printf("Starting 11x17 RIP.  This may take a while . . 
.\n");
                        system("ssh jack 
/opt/bargainhunter/barghntr/impose11.scr");
                        remove("/opt/bargainhunter/barghntr/impose11.scr");
                        header();
                        mvaddstr(11,14,"11x17 RIP completed.  Press any key to 
end
program.");
                        refresh();
                        flushinp();
                        getch();
                        endwin();
                        return 0;
                case 53: // Impose 8.5x11 Layout
                        strcat(buffer1,"8.5x11/");
                        impose[34]='m';
                        break;
                case 54:
                        endwin();
                        return 0;
                }
        header();
        mvprintw(6,15,"%spaper.ps",buffer1);
        mvaddstr(7,23,"Print Options: Print Separations");
        mvaddstr(11,4,"Are you sure you want to start imposing the ");
        doscolor(14);
        switch(counter)
                {
                case 49:
                        addstr("Parkland");
                        break;
                case 50:
                        addstr("Regina");
                        break;
                case 51:
                        addstr("Saskatoon");
                        break;
                case 53:
                        addstr("Miscellaneous");
                }
        doscolor(8);
        addstr(" paper? (Y/N)");
        refresh();
        if (!YesOrNo())
                {
                endwin();
                return 0;
                }
        endwin();
        printf("\nImposing ");
        switch(counter)
                {
                case 49:
                        printf("Parkland");
                        break;
                case 50:
                        printf("Regina");
                        break;
                case 51:
                        printf("Saskatoon");
                        break;
                case 53:
                        printf("Miscellaneous");
                }
        printf(" paper\n");
        printf("-----------------------\n");
        printf("Counting pages . . .\n");
        sprintf(temp,"cat %spaper.ps | grep \%\%Page\\: | wc -l
> /opt/bargainhunter/barghntr/pagecnt.tmp",buffer1);
        system(temp);
        fp=fopen("/opt/bargainhunter/barghntr/pagecnt.tmp","r");
        fgets(temp,4,fp);
        fclose(fp);
        remove("/opt/bargainhunter/barghntr/pagecnt.tmp");
        totalpages=atoi(temp);
        printf("Writing RIP...\n");
        fp=fopen(impose,"w");
        fprintf(fp,"#!/bin/bash\n");
        fprintf(fp,"cd %s\n",buffer1);
        CreateScript(fp,"black",buffer1,totalpages,1,totalpages-3);
        CreateScript(fp,"cyan",buffer1,totalpages,2,totalpages-2);
        CreateScript(fp,"magenta",buffer1,totalpages,3,totalpages-1);
        CreateScript(fp,"yellow",buffer1,totalpages,4,totalpages);
        fclose(fp);
        chmod(impose,S_IRWXU | S_IRWXG); // -rwxrwx---
        printf("Starting ");
        switch(counter)
                {
                case 49:
                        printf("PARKLAND");
                        break;
                case 50:
                        printf("REGINA");
                        break;
                case 51:
                        printf("SASKATOON");
                        break;
                case 53:
                        printf("MISCELLANEOUS");
                }
        printf(" BARGAIN HUNTER job.  This may take a while...");
        strcpy(temp,"ssh jack ");
        strcat(temp,impose);
        system(temp);
        remove(impose);
        header();
        doscolor(14);
        switch(counter)
                {
                case 49:
                        mvaddstr(11,13,"PARKLAND");
                        break;
                case 50:
                        mvaddstr(11,14,"REGINA");
                        break;
                case 51:
                        mvaddstr(11,13,"SASKATOON");
                        break;
                case 53:
                        mvaddstr(11,10,"MISCELLANEOUS");
                }
        doscolor(8);
        addstr(" RIP completed.  Press any key to end program.");
        refresh();
        flushinp();
        getch();
        endwin();
        return 0;
        }

void CreateScript(FILE *fp, const char *pdfname, const char *buffer1,
const int totalpages, int low, int high)
        {
        int page=1,counter;
        bool evenodd=false;
        fprintf(fp,"psselect -p");
        while (low <= high)
                {
                if (evenodd)
                        fprintf(fp,"%d,%d",low,high);
                else
                        fprintf(fp,"%d,%d",high,low);
                page++;
                low+=4;
                high-=4;
                if (low <=high)
                        fprintf(fp,",");
                evenodd=!evenodd;
                }
        fprintf(fp," %spaper.ps ordered.ps\n",buffer1);
        fprintf(fp,"psnup -pledger -Pletter -nup 2 ordered.ps imposed.ps\n");
        fprintf(fp,"ps2pdf -dPDFSETTINGS=/printer -sPAPERSIZE=ledger imposed.ps
imposed.pdf\n");
        fprintf(fp,"pdftk imposed.pdf cat ");
        evenodd=false;
        for (counter=1; counter < totalpages/8+1; counter++)
                {
                fprintf(fp,"%d",counter);
                if (evenodd)
                        fprintf(fp,"E ");
                else
                        fprintf(fp,"W ");
                evenodd=!evenodd;
                }
        fprintf(fp,"output %s.pdf\n",pdfname);
        fprintf(fp,"rm imposed.pdf imposed.ps ordered.ps\n");
        fprintf(fp,"gs -dNOPAUSE -sDEVICE=psmono -r1200 -sPAPERSIZE=11x17
-dCOLORSCREEN -dDITHERPPI=%d -sOUTPUTFILE=temp.ps %s.pdf -c quit
\n",LINESCREEN,pdfname);
        fprintf(fp,"rm %s.pdf\n",pdfname);
        fprintf(fp,"ps2pdf temp.ps %s.pdf\n",pdfname);  
        fprintf(fp,"rm temp.ps\n");
        //***** PDF OPTIMIZE ROUTINE STARTS HERE
        fprintf(fp,"pdfopt %s.pdf opt.pdf\n",pdfname);
        fprintf(fp,"rm %s.pdf\n",pdfname);
        fprintf(fp,"mv opt.pdf %s.pdf\n",pdfname);
        //***** PDF OPTIMIZE ROUTINE ENDS HERE
        fprintf(fp,"chmod 666 *\n");
        return;
        }

void start_dos_colors(void)
        {
        init_pair(1,0,6); // black
        init_pair(2,3,6); // blue
        init_pair(3,2,6); // green
        init_pair(4,1,6); // red
        init_pair(5,5,6); // magenta
        init_pair(6,3,6); // brown
        init_pair(7,7,6); // white
        return;
        }

void doscolor(const int color)
        //these are all on a cyan background
        {
        switch (color)
                {
                case 0:
                        attrset(COLOR_PAIR(1)); //black
                        break;
                case 1:
                        attrset(COLOR_PAIR(2)); // blue
                        break;
                case 2:
                        attrset(COLOR_PAIR(3)); //green
                        break;
                case 3:
                        // cyan -- not used on this cyan background
                        break;
                case 4:
                        attrset(COLOR_PAIR(4)); //red
                        break;
                case 5:
                        attrset(COLOR_PAIR(5)); //magenta
                        break;
                case 6:
                        attrset(COLOR_PAIR(6)); //brown
                        break;
                case 7:
                        attrset(COLOR_PAIR(7)); //white
                        break;
                case 8:
                        attrset(COLOR_PAIR(1)|A_BOLD); //gray
                        break;
                case 9:
                        attrset(COLOR_PAIR(2)|A_BOLD); //light blue
                        break;
                case 10:
                        attrset(COLOR_PAIR(3)|A_BOLD); //light green
                        break;
                case 11:
                        // light cyan -- not used on this cyan background
                        break;
                case 12:
                        attrset(COLOR_PAIR(4)|A_BOLD); // light red
                        break;
                case 13:
                        attrset(COLOR_PAIR(5)|A_BOLD); // light magenta
                        break;
                case 14:
                        attrset(COLOR_PAIR(6)|A_BOLD); // yellow
                        break;
                case 15:
                        attrset(COLOR_PAIR(7)|A_BOLD); // high intensity white
                        break;
                }
        return;
        }

char* left(char *string, const int length)
        {
        if (strlen(string) > length)
                string[length]='\0';
        return string;
        }

char* right(char *string, const int length)
        {
        if (strlen(string) > length)
                memmove(string, &string[strlen(string)-length],length+1);
        return string;
        }

char* mid(char*string,const int start, int length) // the start number
starts from string position number 0
        {
        int counter1, counter2;
        if (start > strlen(string))
                {
                string[0]='\0';
                return string;
                }
        if (strlen(string) < start+length)
                length = strlen(string) - start;
        for (counter1=0, counter2=start; counter1 <length; counter1++, counter2
++)
                string[counter1] = string[counter2];
        string[counter1]='\0';
        return string;
        }

int monthtonumber(const char *month)
        {
        if (strcmp(month,"Jan")==0)
                return 1;
        if (strcmp(month,"Feb")==0)
                return 2;
        if (strcmp(month,"Mar")==0)
                return 3;
        if (strcmp(month,"Apr")==0)
                return 4;
        if (strcmp(month,"May")==0)
                return 5;
        if (strcmp(month,"Jun")==0)
                return 6;
        if (strcmp(month,"Jul")==0)
                return 7;
        if (strcmp(month,"Aug")==0)
                return 8;
        if (strcmp(month,"Sep")==0)
                return 9;
        if (strcmp(month,"Oct")==0)
                return 10;
        if (strcmp(month,"Nov")==0)
                return 11;
        if (strcmp(month,"Dec")==0)
                return 12;
        return 0; // we should never get here if we have a valid month  
        }

bool YesOrNo(void)
        {
        char input;
        flushinp();
        while (input != 'Y' && input != 'N')
                input = toupper(getch());
        if (input == 'Y')
                return true;
        else
                return false;
        }

void header(void)
        {
        clear();
        bkgd(COLOR_PAIR(1));
        doscolor(4);
        mvaddstr(1,12,"P A R K L A N D   B A R G A I N   H U N T E R   I N
C .");
        mvaddstr(2,27,"Imposition Script & RIP");
        doscolor(8);
        curs_set(0);
        }


-- 
MELVILLE THEATRE ~ Melville Sask ~ http://www.melvilletheatre.com



Reply via email to