def testPDFIndirectObject(self): doc = self.doc doc.Reference(pdfdoc.PDFArray([0, 1, 2, 3]), pdfdoc.PDFName('abracadabra')[1:]) self.assertEquals( pdfdoc.PDFIndirectObject('abracadabra', pdfdoc.PDFArray([3, 2, 1, 0])).format(doc), b'2 0 obj\r\n[ 3 2 1 0 ]\r\nendobj\r\n')
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 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 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 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 addObjects(self, doc): """Makes one or more PDF objects to be added to the document. The caller supplies the internal name to be used (typically F1, F2, ... in sequence). This method creates a number of Font and FontDescriptor objects. Every FontDescriptor is a (no more than) 256 character subset of the original TrueType font.""" try: state = self.state[doc] except KeyError: state = self.state[doc] = TTFont.State(self._asciiReadable) state.frozen = 1 for n, subset in enumerate(state.subsets): internalName = self.getSubsetInternalName(n, doc)[1:] baseFontName = (b''.join( (SUBSETN(n), b'+', self.face.name, self.face.subfontNameX))).decode('pdfdoc') pdfFont = pdfdoc.PDFTrueTypeFont() pdfFont.__Comment__ = 'Font %s subset %d' % (self.fontName, n) pdfFont.Name = internalName pdfFont.BaseFont = baseFontName pdfFont.FirstChar = 0 pdfFont.LastChar = len(subset) - 1 widths = list(map(self.face.getCharWidth, subset)) pdfFont.Widths = pdfdoc.PDFArray(widths) cmapStream = pdfdoc.PDFStream() cmapStream.content = makeToUnicodeCMap(baseFontName, subset) if doc.compression: cmapStream.filters = [pdfdoc.PDFZCompress] pdfFont.ToUnicode = doc.Reference(cmapStream, 'toUnicodeCMap:' + baseFontName) pdfFont.FontDescriptor = self.face.addSubsetObjects( doc, baseFontName, subset) # link it in ref = doc.Reference(pdfFont, internalName) fontDict = doc.idToObject['BasicFonts'].dict fontDict[internalName] = pdfFont del self.state[doc]
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>>')
def testPDFArray(self): self.assertEqual(pdfdoc.PDFArray([1,2,3,4]).format(self.doc),b'[ 1 2 3 4 ]')