Changeset 1547
- Timestamp:
- 06/18/04 16:00:16 (20 years ago)
- Location:
- pykota/trunk
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/NEWS
r1546 r1547 24 24 - 1.19alpha23 : 25 25 26 - Smart PDL analyzer now recognizes PDF too. 27 26 28 - All tracebacks now include PyKota's version number. 27 29 -
pykota/trunk/pykota/pdlanalyzer.py
r1544 r1547 22 22 # 23 23 # $Log$ 24 # Revision 1.6 2004/06/18 14:00:16 jalet 25 # Added PDF support in smart PDL analyzer (through GhostScript for now) 26 # 24 27 # Revision 1.5 2004/06/18 10:09:05 jalet 25 28 # Resets file pointer to start of file in all cases … … 41 44 42 45 import sys 46 import os 43 47 import struct 44 48 import tempfile 49 import popen2 45 50 46 51 KILOBYTE = 1024 … … 71 76 pagecount += 1 72 77 return pagecount 78 79 class PDFAnalyzer : 80 def __init__(self, infile) : 81 """Initialize PDF Analyzer.""" 82 self.infile = infile 83 84 def getJobSize(self) : 85 """Counts pages in a PDF document. TODO : don't use GhostScript in the future.""" 86 MEGABYTE = 1024*1024 87 child = popen2.Popen4("gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pswrite -sOutputFile=- -c save pop -f - 2>/dev/null") 88 try : 89 data = self.infile.read(MEGABYTE) 90 while data : 91 child.tochild.write(data) 92 data = self.infile.read(MEGABYTE) 93 child.tochild.flush() 94 child.tochild.close() 95 except (IOError, OSError), msg : 96 raise PDLAnalyzerError, "Unable to convert PDF input to PS with GhostScript : %s" % msg 97 98 psanalyzer = PostScriptAnalyzer(child.fromchild) 99 pagecount = psanalyzer.getJobSize() 100 child.fromchild.close() 101 try : 102 retcode = child.wait() 103 except OSError, msg : 104 self.filter.logger.log_message(_("Problem while waiting for PDF to PS converter (GhostScript pid %s) to exit : %s") % (child.pid, msg)) 105 else : 106 if os.WIFEXITED(retcode) : 107 status = os.WEXITSTATUS(retcode) 108 else : 109 status = retcode 110 if status : 111 raise PDLAnalyzerError, "PDF to PS converter (GhostScript pid %s) exit code is %s" % (child.pid, repr(status)) 112 return pagecount 73 113 74 114 class PCLAnalyzer : … … 430 470 return 0 431 471 472 def isPDF(self, data) : 473 """Returns 1 if data is PDF, else 0.""" 474 if data.startswith("%PDF-") or \ 475 data.startswith("\033%-12345X%PDF-") or \ 476 ((data[:128].find("\033%-12345X") != -1) and (data.upper().find("LANGUAGE=PDF") != -1)) or \ 477 (data.find("%PDF-") != -1) : 478 return 1 479 else : 480 return 0 481 432 482 def isPCL(self, data) : 433 483 """Returns 1 if data is PCL, else 0.""" … … 466 516 elif self.isPCL(firstblock) : 467 517 return PCLAnalyzer 518 elif self.isPDF(firstblock) : 519 return PDFAnalyzer 468 520 else : 469 521 raise PDLAnalyzerError, "Analysis of first data block failed."