xref: /plan9/sys/lib/lp/process/pdfgs (revision ea005eaf856f7225007c246955582d1bfae7be47)
1#!/bin/rc
2# pdfgs pdffile [gsdev] - generate PS from PDF
3#
4# we don't go through postscript, because to
5# get to postscript, we send the pdf through gs!
6# much easier to just go directly.
7
8switch($#*) {
9case 2
10	GSDEVICE=$2
11case 1
12	GSDEVICE=`{echo $LPCLASS | sed 's/(.*\+)?gs!([^+]*)(\+.*)?/\2/'}
13case *
14	echo 'usage: pdfgs pdffile [gsdev]' >[1=2]
15	exit usage
16}
17
18GSTMPFILE=/tmp/pdf^$pid
19GSOPT=('-sDEVICE='$GSDEVICE '-sOutputFile='^$GSTMPFILE -dSAFER -dNOPAUSE \
20	-dQUIET -dBATCH -dNOPAUSE)
21
22# ps level 1 is extremely verbose and tends to run our
23# printers out of memory when printing images.
24if(~ $GSDEVICE pswrite && ~ $LPCLASS *post2*)
25	GSOPT=($GSOPT '-dLanguageLevel=2')
26if not if(~ $GSDEVICE pswrite)
27	GSOPT=($GSOPT '-dLanguageLevel=1')
28
29if(~ $OLIST '')
30	gs $GSOPT $1
31if not {
32	PGLIST=`{echo $OLIST | sed 's/-o//;s/,/ /g;s/	/ /g' | tr -cd '0-9 -'}
33	GSPGLIST=()
34	for(i in $PGLIST){
35		switch($i){
36		case -*
37			GSPGLIST=($GSPGLIST `{seq 1 `{echo $i|tr -d '-'}})
38		case *-
39			# BUG assume 100 >= number of pages
40			GSPGLIST=($GSPGLIST `{seq `{echo $i|tr -d '-'} 100})
41		case *-*
42			GSPGLIST=($GSPGLIST `{seq `{echo $i|tr '-' ' '}})
43		case *
44			GSPGLIST=($GSPGLIST $i)
45		}
46	}
47	GSPGLIST=$"GSPGLIST
48	echo '
49		/Page null def
50		/Page# 0 def
51		/PDFSave null def
52		/DSCPageCount 0 def
53		/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage} def
54		GS_PDF_ProcSet begin
55		pdfdict begin
56		('^$1^') (r) file pdfopen begin
57		/npage pdfpagecount def
58		['^$GSPGLIST^']
59		{
60			dup dup
61				1 ge exch npage le and
62				{ DoPDFPage }
63				{ pop }
64			ifelse
65		} forall
66	' | gs $GSOPT - >/dev/null >[2=1]
67}
68
69cat $GSTMPFILE
70rm -f $GSTMPFILE
71exit ''
72