home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / fixpdf.zip / fixpdf.cmd next >
OS/2 REXX Batch file  |  2002-03-10  |  3KB  |  150 lines

  1. /*fixpdf - get rid of colorspace problem */
  2. /*jtdidit - jt@jt-mj.net 20 Feb - 10 Mar 2002 */
  3.  
  4.  
  5. call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  6. call SysLoadFuncs
  7.  
  8. tempout='$$$.PDF' 
  9. iccb='/ICCBased '
  10. lev='0.3'
  11. prog='fixpdf'
  12. dayte='10 Mar 2002'
  13.  
  14. say prog lev dayte 
  15.                 
  16. fix. = ''                           
  17. fix.1="/DeviceGray"
  18. fix.3="/DeviceRGB"
  19. fix.4="/DeviceCMYK"
  20.                             
  21. parse arg infile outfile .
  22. if infile='' then do
  23.    say 'Usage is fixpdf infile [outfile]'
  24.    say 'If outfile absent, fix is written back to infile'
  25.    say 'Fixes colorspace problem for Acroread 3'
  26.    exit
  27.    end
  28. infilq=infile
  29.  
  30.    
  31. if left(infile,1)='"' then do
  32.    parse arg instring
  33.    q=pos('"',instring,2)
  34.    infile=left(instring,q-1)
  35.    infilq=left(instring,q)
  36.    infile=substr(infile,2)
  37.    instring=strip(substr(instring,q+1))
  38.    if left(instring,1)='"' then do
  39.        q=pos('"',instring,2)
  40.        outfile=left(instring,q-1)
  41.        outfile=substr(outfile,2)
  42.        end
  43.    else outfile=word(instring,1)
  44.    end
  45.    
  46.    
  47.                    
  48. if outfile='' then outfile=tempout 
  49.  
  50.  
  51. ofe=stream(outfile,'C','QUERY EXISTS')
  52. if ofe >'' & outfile <> tempout then do
  53.   say 'output file' outfile 'exists'
  54.   say 'enter to replace it, anything else to quit'
  55.   pull stuff
  56.   if stuff <>'' then exit
  57.   end
  58.  
  59. call sysfiledelete outfile
  60.  
  61.                          
  62.        
  63. ife=stream(infile,'C','QUERY EXISTS')
  64. if ife='' then do
  65.    say 'input file' infile 'not there.'
  66.    say 'Filenames with spaces must be enclosed in double quotes:"this stuff.pdf"'
  67.    exit 
  68.    end
  69. insize=chars(infile)   
  70. say 'size of' infile 'is' insize 'bytes'  
  71.   
  72. signal on notready
  73.  
  74.  
  75. filx=charin(infile,1,insize)
  76. call charout infile
  77.       
  78.  
  79.  
  80. j=1  /* index to stem variables */     
  81.  
  82. icc.0=0
  83. i=1
  84.  
  85. do forever
  86.      slash=pos(iccb,filx,i)
  87.      if slash=0 then leave
  88.      fixpoint=pos('[',filx,slash-8)
  89.      icc.j=fixpoint
  90.      k=pos(']',filx,slash+10)
  91.      i=k
  92.      len.j=k-fixpoint+1
  93.      stuff=substr(filx,fixpoint,len.j)
  94.      obj.j=stuff
  95.      icc.0=j
  96.      j=j+1
  97.   end
  98.   
  99. call charout infile  /* close it */
  100. if icc.0=0 then do
  101.    say 'nothing to do'
  102.    call sysfiledelete outfile
  103.    exit
  104.    end
  105.  
  106. say 'done first pass -' icc.0 ' instance[s] to be fixed'
  107.  
  108. do j=1 to icc.0
  109.    z=pos('/',obj)
  110.    obj=substr(obj.j,z+14,30)
  111.    z=pos('R',obj)
  112.    obj=left(obj,z-1)||'obj' 
  113.    k=pos(obj,filx,1)
  114.    n=pos('/N',filx,k)
  115.    nx=substr(filx,n+3,1)
  116.    if fix.nx='' then do
  117.       say "bad colorspace N" nx "- can't continue"
  118.       signal quit
  119.       end   
  120.     fix=substr(fix.nx,1,len.j)
  121.     filx=left(filx,icc.j-1)||fix||substr(filx,icc.j+len.j)
  122.     say 'Replaced with' fix
  123.    end
  124.    
  125. call charout outfile,filx,1
  126. call charout outfile   
  127.  
  128. if outfile=tempout then do
  129.     z=pos('.PDF',translate(infilq))
  130.     if z>0 then bakfil=left(infilq,z-1)||'.bak'||substr(infilq,z+4)
  131.        else bakfil='pdf.bak'
  132.     '@copy' infilq bakfil '>NUL'
  133.     '@copy' outfile infilq '>NUL'
  134.     call sysfiledelete outfile
  135.     say 'Old input file saved as' bakfil
  136.     end
  137.  
  138. exit
  139.  
  140. quit:
  141.  
  142. call charout infile  /* close it */
  143. call sysfiledelete outfile
  144. exit
  145.  
  146. notready:
  147.  
  148. say 'notready condition at char pos' i
  149. call charout infile
  150. call charout outfile