#require 'Action' require 'erb' OSX.require_framework "WebKit" #OSX.require_framework "PDFKit" class TemplatePrinter < OSX::NSObject attr_accessor :pdfs,:pages,:webView def webView_didFinishLoadForFrame(sender,frame) rect = OSX::NSRect.new(0,0,72*8.5,72*11) view= @webView.mainFrame.frameView.documentView pdfData = view.dataWithPDFInsideRect(view.bounds) @pdfs << pdfData if (@pdfs.size < @pages.size) resources= OSX::NSURL.URLWithString("file://#{OSX::NSBundle.mainBundle.resourcePath.fileSystemRepresentation}/todo_a.rb") @webView.mainFrame.loadHTMLString_baseURL(@pages[@pdfs.size],resources) else @finalDoc=nil @pdfDocs=[] @pdfs.each do |pdf| pdfDoc=OSX::PDFDocument.alloc.initWithData(pdf) pdfBounds=pdfDoc.pageAtIndex(0).boundsForBox(1) @pdfDocs << pdfDoc if @finalDoc==nil @finalDoc=pdfDoc else @finalDoc.insertPage_atIndex(pdfDoc.pageAtIndex(0),@finalDoc.pageCount) end end #@finalDoc.writeToFile("/tmp/finalPrint.pdf") pdfBounds= @finalDoc.pageAtIndex(0).boundsForBox(1) rect = OSX::NSRect.new(0,0,100,100) @pdfView=OSX::PDFView.alloc.initWithFrame(rect) @pdfView.setDocument(@finalDoc) @pdfView.setScaleFactor(1.0) #@pdfView.setAutoScales(true) @pdfView.setDisplaysPageBreaks(false) @pdfView.goToFirstPage(self) printInfo=OSX::NSPrintInfo.sharedPrintInfo printInfo.setHorizontalPagination(OSX::NSFitPagination) printInfo.setVerticalPagination(OSX::NSFitPagination) printInfo.setVerticallyCentered(true) printInfo.setHorizontallyCentered(true) printInfo.setBottomMargin(0.0) printInfo.setTopMargin(0.0) printInfo.setLeftMargin(0.0) printInfo.setRightMargin(0.0) #printInfo.dictionary.setObject_forKey(0.75,OSX::NSPrintScalingFactor) @pdfView.printWithInfo_autoRotate(printInfo,false) end end def printTasksWithTemplate(tasks,template) rect = OSX::NSRect.new(0,0,100,100) @pages= template.buildPagesForTasks(tasks) @pdfs=[] @webView=OSX::WebView.alloc.initWithFrame(rect) @webView.mainFrame.frameView.setAllowsScrolling(false) @webView.mainFrame.frameView.setFrame(rect) webFrame=webView.mainFrame @webView.setFrameLoadDelegate(self) resources= OSX::NSURL.URLWithString("file://#{OSX::NSBundle.mainBundle.resourcePath.fileSystemRepresentation}/todo_a.rb") webFrame.loadHTMLString_baseURL(pages[0],resources) end def printHTML(html) rect = OSX::NSRect.new(0,0,100,100) @pages=[html] @pdfs=[] @webView=OSX::WebView.alloc.initWithFrame(rect) @webView.mainFrame.frameView.setAllowsScrolling(false) @webView.mainFrame.frameView.setFrame(rect) webFrame=webView.mainFrame @webView.setFrameLoadDelegate(self) resources= OSX::NSURL.URLWithString("file://#{OSX::NSBundle.mainBundle.resourcePath.fileSystemRepresentation}/todo_a.rb") webFrame.loadHTMLString_baseURL(pages[0],resources) end end