def writepages(self, outname, pagelist, side2flag): side = 'front' imposedpage = 'left' outPDF = Document(Rectangle(self.papersize[0], self.papersize[1])) outstream = open(outname, 'w') output = PdfWriter.getInstance(outPDF, outstream) outPDF.open() pagedata = output.getDirectContent() #print "in book:writepages" for i in range(len(pagelist)): if i % 2 == 0: outPDF.newPage() outPDF.add(Chunk.NEWLINE) pagenumber = pagelist[i] ## scaling code here if pagenumber == 'b': #print 'blank' pass else: if i % 2 == 0: imposedpage = 'left' if side2flag or ((i + 2) % 4 == 0 and self.duplex): side = 'back' else: side = 'front' else: imposedpage = 'right' #print 'adding page' baseline, leftstart, scale1, scale2 = self.calculateposition( pagenumber, imposedpage, side) page = output.getImportedPage(self.currentdoc, pagenumber) pagedata.addTemplate(page, -0, scale1, scale2, 0, baseline, leftstart) #print i,pagenumber,side,imposedpage outPDF.close() outstream.close()
def save(self, parent, format): chooser = JFileChooser() #filter = FileNameExtensionFilter( # format + " files", # array([format, format.lower()], String)) #chooser.setFileFilter(filter); returnVal = chooser.showSaveDialog(parent) if returnVal == JFileChooser.APPROVE_OPTION: fileName = chooser.getSelectedFile().getPath() if not fileName.upper().endswith('.' + format): fileName += '.' + format.lower() file = File(fileName) else: return if format == 'PNG': ChartUtilities.saveChartAsPNG(file, self.chart, self.exportX, self.exportY) elif format == 'SVG': domImpl = GenericDOMImplementation.getDOMImplementation() doc = domImpl.createDocument(None, "svg", None) svgGen = SVGGraphics2D(doc) svgGen.getGeneratorContext().setPrecision(6) self.chart.draw( svgGen, Rectangle2D.Double(0, 0, self.exportX, self.exportY), None) out = OutputStreamWriter(FileOutputStream(file), "UTF-8") svgGen.stream(out, True) #True is for useCSS out.close() elif format == 'PDF': mapper = DefaultFontMapper() pageSize = Rectangle(self.exportX, self.exportY) doc = TextDocument(pageSize, 50, 50, 50, 50) out = BufferedOutputStream(FileOutputStream(file)) writer = PdfWriter.getInstance(doc, out) doc.open() cb = writer.getDirectContent() tp = cb.createTemplate(self.exportX, self.exportY) g2 = tp.createGraphics(self.exportX, self.exportY, mapper) r2D = Rectangle2D.Double(0, 0, self.exportX, self.exportY) self.chart.draw(g2, r2D) g2.dispose() cb.addTemplate(tp, 0, 0) doc.close()
def save(self, parent, format): chooser = JFileChooser() #filter = FileNameExtensionFilter( # format + " files", # array([format, format.lower()], String)) #chooser.setFileFilter(filter); returnVal = chooser.showSaveDialog(parent) if returnVal == JFileChooser.APPROVE_OPTION: fileName = chooser.getSelectedFile().getPath() if not fileName.upper().endswith('.' + format): fileName += '.' + format.lower() file = File(fileName) else: return if format == 'PNG': ChartUtilities.saveChartAsPNG(file, self.chart, self.exportX, self.exportY) elif format == 'SVG': domImpl = GenericDOMImplementation.getDOMImplementation() doc = domImpl.createDocument(None, "svg", None) svgGen = SVGGraphics2D(doc) svgGen.getGeneratorContext().setPrecision(6) self.chart.draw(svgGen, Rectangle2D.Double(0, 0, self.exportX, self.exportY), None) out = OutputStreamWriter(FileOutputStream(file), "UTF-8") svgGen.stream(out, True) #True is for useCSS out.close() elif format == 'PDF': mapper = DefaultFontMapper() pageSize = Rectangle(self.exportX, self.exportY) doc = TextDocument(pageSize, 50, 50, 50, 50) out = BufferedOutputStream(FileOutputStream(file)) writer = PdfWriter.getInstance(doc, out) doc.open() cb = writer.getDirectContent() tp = cb.createTemplate(self.exportX, self.exportY) g2 = tp.createGraphics(self.exportX, self.exportY, mapper) r2D = Rectangle2D.Double(0, 0, self.exportX, self.exportY) self.chart.draw(g2, r2D) g2.dispose() cb.addTemplate(tp, 0, 0) doc.close()
for file in sys.argv[1:len(sys.argv) - 1]: reader = PdfReader(file) reader.consolidateNamedDestinations() bookmarks = SimpleBookmark.getBookmark(reader) if bookmarks != None: if page_offset != 0: SimpleBookmark.shiftPageNumbers(bookmarks, \ page_offset, None) all_bookmarks.add(bookmarks) page_count = reader.getNumberOfPages() page_offset += page_offset if copy == None: document = Document(reader.getPageSizeWithRotation(1)) output = FileOutputStream(sys.argv[len(sys.argv) - 1]) copy = PdfCopy(document, output) document.open() print "Adding", page_count, "pages from", file for k in range(0, page_count): copy.addPage(copy.getImportedPage(reader, k + 1)) if reader.getAcroForm() != None: copy.copyAcroForm(reader) if not all_bookmarks.isEmpty(): copy.setOutlines(all_bookmarks)
def create(self): path = saveFileDialog("Save report") if path != None: output = path[0] + ".pdf" else: output = os.path.join( os.path.dirname(__file__), "reports", "report.pdf") doc = Document() PdfWriter.getInstance(doc, FileOutputStream( output) ) doc.open() image = Image.getInstance( os.path.join( os.path.dirname(__file__), "reports", "image.png") ) image.setAlignment( image.ALIGN_CENTER ) doc.add( image) doc.add( Paragraph( "\n" ) ) doc.add( LineSeparator() ) title_base_font = BaseFont.createFont( BaseFont.TIMES_BOLDITALIC, BaseFont.WINANSI, BaseFont.EMBEDDED) title_font = Font( title_base_font, 15) title = Paragraph( "TEST REPORT", title_font) title.setAlignment( title.ALIGN_CENTER ) doc.add( title) doc.add( Paragraph( "\n" ) ) doc.add( LineSeparator() ) base_font_feedback = BaseFont.createFont( BaseFont.TIMES_ITALIC, BaseFont.WINANSI, BaseFont.EMBEDDED) font_feedback = Font( base_font_feedback, 10) doc.add( Paragraph( "Test's name: " + self.getTest().getName(), font_feedback) ) doc.add( Paragraph( "Test's description: " + self.getTest().getDescription(), font_feedback) ) doc.add( Paragraph( "\n" ) ) doc.add( Paragraph( "Student " + self.getTest().getUser().getName() + " submitted this test on " + time.strftime('%d/%m/%Y') + " at " + time.strftime('%H:%M') + ".", font_feedback) ) base_font = BaseFont.createFont( BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED) font = Font( base_font, 10) if self.getTest().getUser().getRetries() == 0: doc.add( Paragraph( "This is student's first attempt.", font_feedback) ) else: doc.add( Paragraph( "This isn't student's first attempt. " + "Previous attempts:" + str(self.getTest().getUser().getRetries()) + ".", font_feedback) ) doc.add( Paragraph( "\n", font) ) doc.add( LineSeparator() ) points = 0 final_score = 0 for question in self.getTest().getQuestions(): if question.points != None: points += question.points doc.add( Paragraph( "\n", font) ) doc.add( Paragraph( str( question.number) + ") " + question.question, font) ) if question.type == 1: doc.add( Paragraph( "- " + str(question.number) + ".1) " + question.option1, font) ) doc.add( Paragraph( "- " + str(question.number) + ".2) " + question.option2, font) ) doc.add( Paragraph ( "\n", font) ) if question.answerCode != None: doc.add( Paragraph( "Answer: " + str(question.number) + "." + str( question.answerCode), font_feedback) ) else: doc.add( Paragraph( "Answer:", font_feedback) ) if question.getUserAnswerCode() != None: doc.add( Paragraph( "Student's answer: " + str(question.number) + "." + str( question.getUserAnswerCode() ), font_feedback) ) else: doc.add( Paragraph( "Student's answer:", font_feedback) ) if question.answerIsCorrect(): doc.add( Paragraph( "Score: " + str(question.points), font_feedback) ) final_score += question.points else: doc.add( Paragraph( "Score: " + str(0), font_feedback) ) elif question.type == 2: doc.add( Paragraph( "- " + str(question.number) + ".1) " + question.option1, font) ) doc.add( Paragraph( "- " + str(question.number) + ".2) " + question.option2, font) ) doc.add( Paragraph( "- " + str(question.number) + ".3) " + question.option3, font) ) doc.add( Paragraph ( "\n", font) ) if question.answerCode != None: doc.add( Paragraph( "Answer: " + str(question.number) + "." + str( question.answerCode), font_feedback) ) else: doc.add( Paragraph( "Answer:", font_feedback) ) if question.getUserAnswerCode() != None: doc.add( Paragraph( "Student's answer: " + str(question.number) + "." + str( question.getUserAnswerCode() ), font_feedback) ) else: doc.add( Paragraph( "Student's answer:", font_feedback) ) if question.answerIsCorrect(): doc.add( Paragraph( "Score: " + str(question.points), font_feedback) ) final_score += question.points else: doc.add( Paragraph( "Score: " + str(0), font_feedback) ) elif question.type == 3: doc.add( Paragraph( "\n", font) ) if question.answerText != None: doc.add( Paragraph( "Answer: " + question.answerText, font_feedback) ) else: doc.add( Paragraph( "Answer:", font_feedback) ) if question.getUserAnswerText() != None: doc.add( Paragraph( "Student's answer: " + question.getUserAnswerText(), font_feedback) ) else: doc.add( Paragraph( "Student's answer:", font_feedback) ) if question.answerIsCorrect(): doc.add( Paragraph( "Score: " + str(question.points), font_feedback) ) final_score += question.points else: doc.add( Paragraph( "Score: " + str(0), font_feedback) ) doc.add( Paragraph( "\n", font) ) doc.add( LineSeparator() ) doc.add( Paragraph( "The student got a score of " + str(final_score) + " out of " + str(points) + ".", font_feedback) ) doc.add( Paragraph( "\n", font) ) doc.add( LineSeparator() ) doc.close()