def genPDF(self): data = util.String() data += "%PDF-1.5\n" for obj in self.objects: self.writeObj(data, obj) xrefStartPos = len(data) data += "xref\n0 %d\n" % self.objectCnt self.writeXref(data, 0, 65535, "f") for obj in self.objects: self.writeXref(data, obj.xrefPos) data += "\n" data += ("trailer\n" "<< /Size %d\n" "/Root %d 0 R\n" "/Info %d 0 R\n>>\n" % (self.objectCnt, self.catalogObj.nr, self.infoObj.nr)) data += "startxref\n%d\n%%%%EOF\n" % xrefStartPos return str(data)
def generateRTF(self): s = util.String() for page in self.pages: for p in page: s += p.generateRTF() s += "\\page\n" return str(s)
def genPage(self, pageNr): pg = self.doc.pages[pageNr] # content stream cont = util.String() self.currentFont = "" for op in pg.ops: op.pdfOp.draw(op, pageNr, cont, self) self.pageContentObjs[pageNr].data = self.genStream(str(cont))
def generate(self): #lsdjflksj = util.TimerDev("generate") doc = self.doc # fast lookup of font information self.fonts = { pml.COURIER: FontInfo("Courier"), pml.COURIER | pml.BOLD: FontInfo("Courier-Bold"), pml.COURIER | pml.ITALIC: FontInfo("Courier-Oblique"), pml.COURIER | pml.BOLD | pml.ITALIC: FontInfo("Courier-BoldOblique"), pml.HELVETICA: FontInfo("Helvetica"), pml.HELVETICA | pml.BOLD: FontInfo("Helvetica-Bold"), pml.HELVETICA | pml.ITALIC: FontInfo("Helvetica-Oblique"), pml.HELVETICA | pml.BOLD | pml.ITALIC: FontInfo("Helvetica-BoldOblique"), pml.TIMES_ROMAN: FontInfo("Times-Roman"), pml.TIMES_ROMAN | pml.BOLD: FontInfo("Times-Bold"), pml.TIMES_ROMAN | pml.ITALIC: FontInfo("Times-Italic"), pml.TIMES_ROMAN | pml.BOLD | pml.ITALIC: FontInfo("Times-BoldItalic"), } # list of PDFObjects self.objects = [] # number of fonts used self.fontCnt = 0 # PDF object count. it starts at 1 because the 'f' thingy in the # xref table is an object of some kind or something... self.objectCnt = 1 pages = len(doc.pages) self.catalogObj = self.addObj() self.infoObj = self.createInfoObj() pagesObj = self.addObj() # we only create this when needed, in genWidths self.widthsObj = None if doc.tocs: self.outlinesObj = self.addObj() # each outline is a single PDF object self.outLineObjs = [] for i in xrange(len(doc.tocs)): self.outLineObjs.append(self.addObj()) self.outlinesObj.data = ("<< /Type /Outlines\n" "/Count %d\n" "/First %d 0 R\n" "/Last %d 0 R\n" ">>" % (len(doc.tocs), self.outLineObjs[0].nr, self.outLineObjs[-1].nr)) outlinesStr = "/Outlines %d 0 R\n" % self.outlinesObj.nr if doc.showTOC: outlinesStr += "/PageMode /UseOutlines\n" else: outlinesStr = "" # each page has two PDF objects: 1) a /Page object that links to # 2) a stream object that has the actual page contents. self.pageObjs = [] self.pageContentObjs = [] for i in xrange(pages): self.pageObjs.append( self.addObj("<< /Type /Page\n" "/Parent %d 0 R\n" "/Contents %d 0 R\n" ">>" % (pagesObj.nr, self.objectCnt + 1))) self.pageContentObjs.append(self.addObj()) if doc.defPage != -1: outlinesStr += "/OpenAction [%d 0 R /XYZ null null 0]\n" % ( self.pageObjs[0].nr + doc.defPage * 2) self.catalogObj.data = ("<< /Type /Catalog\n" "/Pages %d 0 R\n" "%s" ">>" % (pagesObj.nr, outlinesStr)) for i in xrange(pages): self.genPage(i) kids = util.String() kids += "[" for obj in self.pageObjs: kids += "%d 0 R\n" % obj.nr kids += "]" fontStr = "" for fi in self.fonts.itervalues(): if fi.number != -1: fontStr += "/F%d %d 0 R " % (fi.number, fi.pdfObj.nr) pagesObj.data = ("<< /Type /Pages\n" "/Kids %s\n" "/Count %d\n" "/MediaBox [0 0 %f %f]\n" "/Resources << /Font <<\n" "%s >> >>\n" ">>" % (str(kids), pages, self.mm2points( doc.w), self.mm2points(doc.h), fontStr)) if doc.tocs: for i in xrange(len(doc.tocs)): self.genOutline(i) return self.genPDF()