def FormFontsDictionary(): from reportlab.pdfbase.pdfdoc import PDFDictionary fontsdictionary = PDFDictionary() fontsdictionary.__RefOnly__ = 1 for fullname, shortname in FORMFONTNAMES.items(): fontsdictionary[shortname] = FormFont(fullname, shortname) fontsdictionary["ZaDb"] = PDFPattern(ZaDbPattern) return fontsdictionary
def FormFontsDictionary(): from reportlab.pdfbase.pdfdoc import PDFDictionary fontsdictionary = PDFDictionary() fontsdictionary.__RefOnly__ = 1 for (fullname, shortname) in list(FORMFONTNAMES.items()): fontsdictionary[shortname] = FormFont(fullname, shortname) fontsdictionary["ZaDb"] = ZADB return fontsdictionary
def buttonStreamDictionary(): "everything except the length for the button appearance streams" result = PDFDictionary() result["SubType"] = "/Form" result["BBox"] = "[0 0 16.77036 14.90698]" font = PDFDictionary() font["ZaDb"] = ZADB resources = PDFDictionary() resources["ProcSet"] = "[ /PDF /Text ]" resources["Font"] = font result["Resources"] = resources return result
def buttonStreamDictionary(width=16.7704, height=14.907): "everything except the length for the button appearance streams" result = PDFDictionary() result["SubType"] = "/Form" result["BBox"] = "[0 0 %(width)s %(height)s]" % vars() font = PDFDictionary() font["ZaDb"] = PDFPattern(ZaDbPattern) resources = PDFDictionary() resources["ProcSet"] = "[ /PDF /Text ]" resources["Font"] = font result["Resources"] = resources return result
def format(self, document): d = {"Title": PDFString(self.title), "Author": PDFString(self.author), "CreationDate": PDFDate(invariant=self.invariant, dateFormatter=self._dateFormatter), "Producer": PDFString(self.producer)} if self.pdfxversion: d["GTS_PDFXVersion"] = PDFString(self.pdfxversion) d["Creator"] = PDFString(self.creator) d["Subject"] = PDFString(self.subject) d["Keywords"] = PDFString(self.keywords) pd = PDFDictionary(d) return pd.format(document)
def format(self, document): D = {} D["Title"] = PDFString(self.title) D["Author"] = PDFString(self.author) D["CreationDate"] = PDFDate(invariant=self.invariant, dateFormatter=self._dateFormatter) D["Producer"] = PDFString(self.producer) if self.pdfxversion: D["GTS_PDFXVersion"] = PDFString(self.pdfxversion) D["Creator"] = PDFString(self.creator) D["Subject"] = PDFString(self.subject) D["Keywords"] = PDFString(self.keywords) PD = PDFDictionary(D) return PD.format(document)
def format(self, document): # use a dummy document to bypass encryption from reportlab.pdfbase.pdfdoc import DummyDoc, PDFDictionary, PDFString, PDFName dummy = DummyDoc() dict = {"Filter": PDFName("Standard"), "O": hexText(self.O), #PDFString(self.O), "U": hexText(self.U), #PDFString(self.U), "P": self.P} if self.revision == 3: dict['Length'] = 128 dict['R'] = 3 dict['V'] = 2 else: dict['R'] = 2 dict['V'] = 1 pdfdict = PDFDictionary(dict) return pdfdict.format(dummy)
def bsPDF(borderWidth, borderStyle, dashLen): d = dict(W=borderWidth, S=PDFName(_bsStyles[borderStyle])) if borderStyle == 'dashed': if not dashLen: dashLen = [3] elif not isinstance(dashLen, (list, tuple)): dashLen = [dashLen] d['D'] = PDFArray(dashLen) return PDFDictionary(d)
def format(self, doc): d = dict(Fields=PDFArray([self.getRef(f) for f in self.fields]), ) if self.fonts: F = [self.fontRef(f) for f in self.fonts] d['DA'] = PDFString('/%s 0 Tf 0 g' % list(self.fonts.keys())[0]) d['DR'] = PDFFromString( '<< /Encoding\n<<\n/RLAFencoding\n%s\n>>\n%s\n>>' % (self.encRefStr, '\n'.join(F))) r = PDFDictionary(d).format(doc) return r
def _canvasMaker(self, *args, **kw): canv = canvas.Canvas(*args, **kw) catalog = canv._doc.Catalog try: vp = catalog.ViewerPreferences except AttributeError: from reportlab.pdfbase.pdfdoc import PDFDictionary vp = catalog.ViewerPreferences = PDFDictionary() vp['Duplex'] = 'Simplex' return canv
def format(self, document): # use a dummy document to bypass encryption from reportlab.pdfbase.pdfdoc import DummyDoc, PDFDictionary, PDFString, PDFName dummy = DummyDoc() dict = { "Filter": PDFName("Standard"), "O": hexText(self.O), #PDFString(self.O), "U": hexText(self.U), #PDFString(self.U), "P": self.P } if self.revision == 5: dict['Length'] = 256 dict['R'] = 5 dict['V'] = 5 dict['O'] = hexText(self.O) dict['U'] = hexText(self.U) dict['OE'] = hexText(self.OE) dict['UE'] = hexText(self.UE) dict['Perms'] = hexText(self.Perms) dict['StrF'] = PDFName("StdCF") dict['StmF'] = PDFName("StdCF") stdcf = { "Length": 32, "AuthEvent": PDFName("DocOpen"), "CFM": PDFName("AESV3") } cf = {"StdCF": PDFDictionary(stdcf)} dict['CF'] = PDFDictionary(cf) elif self.revision == 3: dict['Length'] = 128 dict['R'] = 3 dict['V'] = 2 else: dict['R'] = 2 dict['V'] = 1 pdfdict = PDFDictionary(dict) return pdfdict.format(dummy)
def save(self): data = PDFStream(dictionary=PDFDictionary({ 'Type': PDFName('Data'), 'Subtype': PDFName('XML') }), content=self.xml, filters=None) self._doc.Reference(data) if 'Data' not in self._doc.Catalog.__NoDefault__: self._doc.Catalog.__NoDefault__.append('Data') self._doc.Catalog.__setattr__('Data', data) Canvas.save(self)
def format(self, doc): d = dict(Fields=PDFArray([self.getRef(f) for f in self.fields]), ) if self.sigFlags: d['SigFlags'] = self.sigFlags if self.fonts: FK = list(sorted(self.fonts.keys())) F = [self.fontRef(f) for f in FK] d['DA'] = PDFString('/%s 0 Tf 0 g' % FK[0]) d['DR'] = PDFFromString( '<< /Encoding\n<<\n/RLAFencoding\n%s\n>>\n%s\n>>' % (self.encRefStr, '\n'.join(F))) d.update(self.extras) r = PDFDictionary(d).format(doc) return r
def format(self,doc): kids = self.kids d = len(kids) if d<2: raise ValueError('RadioGroup:%s has %d < 2 RadioBoxes' % (self.T,d)) d = dict( Ff=self.Ff, Kids = PDFArray([k for k in self.kids]), FT = PDFName('Btn'), T = PDFString(self.T), #DA = PDFString('0 g'), ) if self.V: d['V'] = PDFName(self.V) if self.TU: d['TU'] =PDFString(self.TU) r = PDFDictionary(d).format(doc) return r
def makeStream(self, width, height, stream, **D): D['Matrix'] = PDFArray([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]) D['BBox'] = PDFArray([0, 0, width, height]) D['Subtype'] = PDFName('Form') D['Type'] = PDFName('XObject') D['FormType'] = 1 s = PDFStream( PDFDictionary(D), stream, filters=[PDFStreamFilterZCompress()] if self.canv._doc.compression else None, ) #compute a lookup string s._af_refstr = stream + '\n'.join( ('%s=%r' % (k, _pdfObjToStr(v)) for k, v in sorted(D.items()))) return s
def _textfield( self, value='', fillColor=None, borderColor=None, textColor=None, borderWidth=1, borderStyle='solid', width=120, height=36, x=0, y=0, tooltip=None, name=None, annotationFlags='print', fieldFlags='', forceBorder=False, relative=False, maxlen=100, fontName=None, fontSize=None, wkind=None, options=None, dashLen=3, ): rFontName, iFontName = self.makeFont(fontName) if fontSize is None: fontSize = 12 textColor, borderColor, fillColor = self.stdColors( textColor, borderColor, fillColor) canv = self.canv if relative: x, y = self.canv.absolutePosition(x, y) doc = canv._doc rFontName = '<</%s %s>>' % (iFontName, rFontName) Ff = makeFlags(fieldFlags, fieldFlagValues) if wkind != 'textfield': #options must be a list of pairs (label value) #value must be a list of the values FT = 'Ch' if wkind == 'choice': Ff |= fieldFlagValues['combo'] #just in case V = [] Opt = [] AP = [] I = [] TF = [] if not isinstance(options, (list, tuple)): raise TypeError('%s options=%r is wrong type' % (wkind, options)) for v in options: if isStr(v): Opt.append(PDFString(v)) l = v elif isinstance(v, (list, tuple)): if len(v) == 1: v = l = v[0] else: l, v = v Opt.append(PDFArray([PDFString(v), PDFString(l)])) else: raise TypeError('%s option %r is wrong type' % (wkind, v)) AP.append(v) TF.append(l) Opt = PDFArray(Opt) if value: if not isinstance(value, (list, tuple)): value = [value] for v in value: if v not in AP: if v not in TF: raise ValueError( '%s value %r is not in option\nvalues %r\nor labels %r' % (wkind, v, AP, TF)) else: v = AP[TF.index(v)] I.append(AP.index(v)) V.append(PDFString(v)) I.sort() if not (Ff & fieldFlagValues['multiSelect']) or len(value) == 1: if wkind == 'choice': value = TF[I[0]] else: value = value[:1] V = V[:1] V = V[0] if len(V) == 1 else PDFArray(V) lbextras = dict(labels=TF, I=I, wkind=wkind) else: V = PDFString(value) else: I = Opt = [] lbextras = {} FT = 'Tx' if not isStr(value): raise TypeError('textfield value=%r is wrong type' % value) V = PDFString(value) AP = {} for key in 'N': tC, bC, fC = self.varyColors(key, textColor, borderColor, fillColor) ap = self.txAP(key, value, iFontName, rFontName, fontSize, fillColor=fC, borderColor=bC, textColor=tC, borderWidth=borderWidth, borderStyle=borderStyle, width=width, height=height, dashLen=dashLen, **lbextras) if ap._af_refstr in self._refMap: ref = self._refMap[ap._af_refstr] else: ref = self.getRef(ap) self._refMap[ap._af_refstr] = ref AP[key] = ref TF = dict( FT=PDFName(FT), P=doc.thisPageRef(), V=V, #AS = PDFName(value), DV=V, Rect=PDFArray((x, y, x + width, y + height)), AP=PDFDictionary(AP), Subtype=PDFName('Widget'), Type=PDFName('Annot'), F=makeFlags(annotationFlags, annotationFlagValues), Ff=Ff, #H=PDFName('N'), DA=PDFString( '/%s %d Tf %s' % (iFontName, fontSize, self.streamFillColor(textColor))), ) if Opt: TF['Opt'] = Opt if I: TF['I'] = PDFArray(I) if maxlen: TF['MaxLen'] = maxlen if tooltip: TF['TU'] = PDFString(tooltip) if not name: name = 'AFF%03d' % len(self.fields) TF['T'] = PDFString(name) MK = dict(BG=PDFArray(self.colorTuple(fillColor)), ) # Acrobat seems to draw a thin border when BS is defined, so only # include this if there actually is a border to draw if borderWidth: TF['BS'] = bsPDF(borderWidth, borderStyle, dashLen) MK['BC'] = PDFArray(self.colorTuple(borderColor)) TF['MK'] = PDFDictionary(MK) TF = PDFDictionary(TF) self.canv._addAnnotation(TF) self.fields.append(self.getRef(TF)) self.checkForceBorder(x, y, width, height, forceBorder, 'square', borderStyle, borderWidth, borderColor, fillColor)
def radio( self, value=None, selected=False, buttonStyle='circle', shape='circle', fillColor=None, borderColor=None, textColor=None, borderWidth=1, borderStyle='solid', size=20, x=0, y=0, tooltip=None, name=None, annotationFlags='print', fieldFlags='noToggleToOff required radio', forceBorder=False, relative=False, dashLen=3, ): if name not in self._radios: group = RadioGroup(name, tooltip=tooltip, fieldFlags=fieldFlags) group._ref = self.getRef(group) self._radios[name] = group self.fields.append(group._ref) else: group = self._radios[name] fieldFlags = makeFlags(fieldFlags, fieldFlagValues) if fieldFlags != group.Ff: raise ValueError('radio.%s.%s created with different flags' % (name, value)) if not value: raise ValueError('bad value %r for radio.%s' % (value, name)) initialValue = value if selected else 'Off' textColor, borderColor, fillColor = self.stdColors( textColor, borderColor, fillColor) if initialValue == value: if group.V is not None: if group.V != value: raise ValueError( 'radio.%s.%s sets initial value conflicting with %s' % (name, value, group.V)) else: group.V = value canv = self.canv if relative: x, y = self.canv.absolutePosition(x, y) doc = canv._doc AP = {} for key in 'NDR': APV = {} tC, bC, fC = self.varyColors(key, textColor, borderColor, fillColor) for v in (value, 'Off'): ap = self.checkboxAP( key, 'Yes' if v == value else 'Off', buttonStyle=buttonStyle, shape=shape, fillColor=fC, borderColor=bC, textColor=tC, borderWidth=borderWidth, borderStyle=borderStyle, size=size, dashLen=dashLen, ) if ap._af_refstr in self._refMap: ref = self._refMap[ap._af_refstr] else: ref = self.getRef(ap) self._refMap[ap._af_refstr] = ref APV[v] = ref AP[key] = PDFDictionary(APV) del APV RB = dict( FT=PDFName('Btn'), P=doc.thisPageRef(), AS=PDFName(initialValue), #DV = PDFName(initialValue), Rect=PDFArray((x, y, x + size, y + size)), AP=PDFDictionary(AP), Subtype=PDFName('Widget'), Type=PDFName('Annot'), F=makeFlags(annotationFlags, annotationFlagValues), Parent=group._ref, #DA = PDFString('1 g '+(self.streamFillColor(fillColor) if fillColor else '-0.25 0.75 -0.25 rg')) H=PDFName('N'), ) #RB['T'] = PDFString(name) MK = dict( CA='(%s)' % ZDSyms[buttonStyle], BC=PDFArray(self.colorTuple(borderColor)), BG=PDFArray(self.colorTuple(fillColor)), ) if borderWidth: RB['BS'] = bsPDF(borderWidth, borderStyle, dashLen) RB['MK'] = PDFDictionary(MK) RB = PDFDictionary(RB) self.canv._addAnnotation(RB) group.kids.append(self.getRef(RB)) self.checkForceBorder(x, y, size, size, forceBorder, shape, borderStyle, borderWidth, borderColor, fillColor)
def checkbox( self, checked=False, buttonStyle='check', shape='square', fillColor=None, borderColor=None, textColor=None, borderWidth=1, borderStyle='solid', size=20, x=0, y=0, tooltip=None, name=None, annotationFlags='print', fieldFlags='required', forceBorder=False, relative=False, dashLen=3, ): initialValue = 'Yes' if checked else 'Off' textColor, borderColor, fillColor = self.stdColors( textColor, borderColor, fillColor) canv = self.canv if relative: x, y = self.canv.absolutePosition(x, y) doc = canv._doc AP = {} for key in 'NDR': APV = {} tC, bC, fC = self.varyColors(key, textColor, borderColor, fillColor) for value in ('Yes', 'Off'): ap = self.checkboxAP( key, value, buttonStyle=buttonStyle, shape=shape, fillColor=fC, borderColor=bC, textColor=tC, borderWidth=borderWidth, borderStyle=borderStyle, size=size, dashLen=dashLen, ) if ap._af_refstr in self._refMap: ref = self._refMap[ap._af_refstr] else: ref = self.getRef(ap) self._refMap[ap._af_refstr] = ref APV[value] = ref AP[key] = PDFDictionary(APV) del APV CB = dict( FT=PDFName('Btn'), P=doc.thisPageRef(), V=PDFName(initialValue), AS=PDFName(initialValue), #DV = PDFName(initialValue), Rect=PDFArray((x, y, x + size, y + size)), AP=PDFDictionary(AP), Subtype=PDFName('Widget'), Type=PDFName('Annot'), F=makeFlags(annotationFlags, annotationFlagValues), Ff=makeFlags(fieldFlags, fieldFlagValues), H=PDFName('N'), ) if tooltip: CB['TU'] = PDFString(tooltip) if not name: name = 'AFF%03d' % len(self.fields) if borderWidth: CB['BS'] = bsPDF(borderWidth, borderStyle, dashLen) CB['T'] = PDFString(name) MK = dict( CA='(%s)' % ZDSyms[buttonStyle], BC=PDFArray(self.colorTuple(borderColor)), BG=PDFArray(self.colorTuple(fillColor)), ) CB['MK'] = PDFDictionary(MK) CB = PDFDictionary(CB) self.canv._addAnnotation(CB) self.fields.append(self.getRef(CB)) self.checkForceBorder(x, y, size, size, forceBorder, shape, borderStyle, borderWidth, borderColor, fillColor)