def addObjects(self, doc): """Add whatever needed to PDF file, and return a FontDescriptor reference""" from reportlab.pdfbase import pdfdoc fontFile = pdfdoc.PDFStream() fontFile.content = self._binaryData #fontFile.dictionary['Length'] = self._length fontFile.dictionary['Length1'] = self._length1 fontFile.dictionary['Length2'] = self._length2 fontFile.dictionary['Length3'] = self._length3 #fontFile.filters = [pdfdoc.PDFZCompress] fontFileRef = doc.Reference(fontFile, 'fontFile:' + self.pfbFileName) fontDescriptor = pdfdoc.PDFDictionary({ 'Type': '/FontDescriptor', 'Ascent':self.ascent, 'CapHeight':self.capHeight, 'Descent':self.descent, 'Flags': 34, 'FontBBox':pdfdoc.PDFArray(self.bbox), 'FontName':pdfdoc.PDFName(self.name), 'ItalicAngle':self.italicAngle, 'StemV':self.stemV, 'XHeight':self.xHeight, 'FontFile': fontFileRef, }) fontDescriptorRef = doc.Reference(fontDescriptor, 'fontDescriptor:' + self.name) return fontDescriptorRef
def makePDFObject(self): "Returns a PDF Object representing self" # avoid circular imports - this cannot go at module level from reportlab.pdfbase import pdfdoc D = {} baseEncodingName = self.baseEncodingName baseEnc = getEncoding(baseEncodingName) differences = self.getDifferences(baseEnc) #[None] * 256) # if no differences, we just need the base name if differences == []: return pdfdoc.PDFName(baseEncodingName) else: #make up a dictionary describing the new encoding diffArray = [] for range in differences: diffArray.append(range[0]) # numbers go 'as is' for glyphName in range[1:]: if glyphName is not None: # there is no way to 'unset' a character in the base font. diffArray.append('/' + glyphName) #print 'diffArray = %s' % diffArray D["Differences"] = pdfdoc.PDFArray(diffArray) if baseEncodingName in ('MacRomanEncoding','MacExpertEncoding','WinAnsiEncoding'): #https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf page 263 D["BaseEncoding"] = pdfdoc.PDFName(baseEncodingName) D["Type"] = pdfdoc.PDFName("Encoding") PD = pdfdoc.PDFDictionary(D) return PD
def makePDFObject(self): "Returns a PDF Object representing self" # avoid circular imports - this cannot go at module level from reportlab.pdfbase import pdfdoc D = {} baseEnc = getEncoding(self.baseEncodingName) differences = self.getDifferences(baseEnc) #[None] * 256) # if no differences, we just need the base name if differences == []: return pdfdoc.PDFName(self.baseEncodingName) else: #make up a dictionary describing the new encoding diffArray = [] for range in differences: diffArray.append(range[0]) # numbers go 'as is' for glyphName in range[1:]: if glyphName is not None: # there is no way to 'unset' a character in the base font. diffArray.append('/' + glyphName) #print 'diffArray = %s' % diffArray D["Differences"] = pdfdoc.PDFArray(diffArray) D["BaseEncoding"] = pdfdoc.PDFName(self.baseEncodingName) D["Type"] = pdfdoc.PDFName("Encoding") PD = pdfdoc.PDFDictionary(D) return PD
def addSubsetObjects(self, doc, fontname, subset): """Generate a TrueType font subset and add it to the PDF document. Returns a PDFReference to the new FontDescriptor object.""" fontFile = pdfdoc.PDFStream() fontFile.content = self.makeSubset(subset) fontFile.dictionary['Length1'] = len(fontFile.content) if doc.compression: fontFile.filters = [pdfdoc.PDFZCompress] fontFileRef = doc.Reference(fontFile, 'fontFile:%s(%s)' % (self.filename, fontname)) flags = self.flags & ~ FF_NONSYMBOLIC flags = flags | FF_SYMBOLIC fontDescriptor = pdfdoc.PDFDictionary({ 'Type': '/FontDescriptor', 'Ascent': self.ascent, 'CapHeight': self.capHeight, 'Descent': self.descent, 'Flags': flags, 'FontBBox': pdfdoc.PDFArray(self.bbox), 'FontName': pdfdoc.PDFName(fontname), 'ItalicAngle': self.italicAngle, 'StemV': self.stemV, 'FontFile2': fontFileRef, }) return doc.Reference(fontDescriptor, 'fontDescriptor:' + fontname)
def format(self, document): """Allow it to be used within pdfdoc framework. This only defines how it is stored, not how it is drawn later.""" dict = pdfdoc.PDFDictionary() dict['Type'] = '/XObject' dict['Subtype'] = '/Image' dict['Width'] = self.width dict['Height'] = self.height dict['BitsPerComponent'] = 8 dict['ColorSpace'] = pdfdoc.PDFName(self.colorSpace) content = string.join(self.imageData[3:-1], '\n') + '\n' strm = pdfdoc.PDFStream(dictionary=dict, content=content) return strm.format(document)
def structToPDF(structure): "Converts deeply nested structure to PDFdoc dictionary/array objects" if isinstance(structure, dict): newDict = {} for k, v in structure.items(): newDict[k] = structToPDF(v) return pdfdoc.PDFDictionary(newDict) elif isSeq(structure): newList = [] for elem in structure: newList.append(structToPDF(elem)) return pdfdoc.PDFArray(newList) else: return structure
def testPDFDictionary(self): self.assertEqual(pdfdoc.PDFDictionary(dict(A=pdfdoc.PDFArray([1,2,3,4]))).format(self.doc),b'<<\n/A [ 1 2 3 4 ]\n>>')