master
1import config
2import reportlab.pdfbase.pdfdoc as pdfdoc
3from reportlab.pdfbase.pdfdoc import _getTimeStamp, md5, bytestr, DummyDoc, PDFString, pdfdocEnc
4import reportlab.pdfgen.canvas as canvas
5from reportlab.pdfgen.canvas import Canvas
6
7class PDFInfo(pdfdoc.PDFInfo):
8 creator = config.APP_NAME
9 producer = config.APP_NAME
10 title = "SURVEY TITLE"
11 author = "SURVEY AUTHOR"
12 subject = "SURVEY SUBJECT"
13pdfdoc.PDFInfo = PDFInfo
14
15class PDFFile(pdfdoc.PDFFile):
16 ### just accumulates strings: keeps track of current offset
17 def __init__(self,pdfVersion=pdfdoc.PDF_VERSION_DEFAULT):
18 self.strings = []
19 self.write = self.strings.append
20 self.offset = 0
21 ### chapter 5
22 # Following Ken Lunde's advice and the PDF spec, this includes
23 # some high-order bytes. I chose the characters for Tokyo
24 # in Shift-JIS encoding, as these cannot be mistaken for
25 # any other encoding, and we'll be able to tell if something
26 # has run our PDF files through a dodgy Unicode conversion.
27 self.add((pdfdocEnc("%%PDF-%s.%s" % pdfVersion) +
28 b'\r\n%\223\214\213\236 Survey PDF\r\n'
29 ))
30pdfdoc.PDFFile = PDFFile
31
32class PDFDocument(pdfdoc.PDFDocument):
33 def __init__(self, *args, **kwargs):
34 super().__init__(*args, **kwargs)
35 sig = self.signature = md5()
36 sig.update(b"a survey document")
37 if not self.invariant:
38 cat = _getTimeStamp()
39 else:
40 cat = 946684800.0
41 cat = ascii(cat)
42 sig.update(bytestr(cat)) # initialize with timestamp digest
43 #self.info = PDFInfo()
44 #self.info.invariant = self.invariant
45
46 def ID(self):
47 "A unique fingerprint for the file (unless in invariant mode)"
48 if self._ID:
49 return self._ID
50 digest = self.signature.digest()
51 doc = DummyDoc()
52 IDs = PDFString(digest,enc='raw').format(doc)
53 self._ID = (b'\r\n % PDF Survey -- digest\r\n ['
54 +IDs+b' '+IDs+b']\r\n')
55 return self._ID
56canvas.pdfdoc.PDFDocument = PDFDocument