def print_pdf(self, cr, uid, ids, context): attachment_obj = self.pool.get('ir.attachment') output = PdfFileWriter() page_cnt = 0 order = self.browse(cr, uid, ids[0], context=context) lines = self.browse(cr, uid, ids, context=context) for line in lines: if line.drawing_file_name and line.drawing_file_name.lower().endswith('.pdf'): file_ids = attachment_obj.search( cr, uid, [('name', '=', 'drawing_file'), ('res_id', '=', line.id), ('res_model', '=', 'drawing.order.line')]) if file_ids: attach_file = attachment_obj.file_get(cr, uid, file_ids[0],context=context) input = PdfFileReader(attach_file) for page in input.pages: output.addPage(page) page_cnt += 1 if page_cnt > 0: #+++ HoangTK - 12/10/2015: Use system temp file and controller, fix osv_memory error when pdf file large file_name = "Drawing" if context.get('order_name'): file_name = '%s-%s'%(file_name, self._format_file_name(context.get('order_name'))) temp_drawing_pdf_file = tempfile.NamedTemporaryFile(delete=False) temp_drawing_pdf_file_name = temp_drawing_pdf_file.name temp_drawing_pdf_file.close() outputStream = file(temp_drawing_pdf_file_name, "wb") output.write(outputStream) outputStream.close() return { 'type' : 'ir.actions.act_url', 'url': '/web/export/drawing_order_print_pdf?file_name=%s&file_data=%s'%(file_name, temp_drawing_pdf_file_name), 'target': 'self', } # full_path_temp = attachment_obj.full_path(cr, uid, 'temp') # # file_name = self._format_file_name(order.name) # file_name = "Drawing" # if context.get('order_name'): # file_name = '%s-%s'%(file_name, self._format_file_name(context.get('order_name'))) # full_file_name = '%s/%s.pdf'%(full_path_temp, file_name,) # outputStream = file(full_file_name, "wb") # output.write(outputStream) # outputStream.close() # filedata = open(full_file_name,'rb').read().encode('base64') # os.remove(full_file_name) # return self.pool.get('file.down').download_data(cr, uid, "%s.pdf"%(file_name,), filedata, context) #--- HoangTK - 12/10/2015: Use system temp file and controller, fix osv_memory error when pdf file large else: raise osv.except_osv(_("Error!"),'No PDF files were found!') return False
def print_pdf(self, cr, uid, ids, context): assert len(ids) == 1, 'This option should only be used for a single CNC work order at a time' attachment_obj = self.pool.get('ir.attachment') output = PdfFileWriter() page_cnt = 0 order = self.browse(cr, uid, ids[0], context=context) for line in order.wo_cnc_lines: if line.drawing_file_name and line.drawing_file_name.lower().endswith('.pdf'): file_ids = attachment_obj.search( cr, uid, [('name', '=', 'drawing_file'), ('res_id', '=', line.id), ('res_model', '=', 'work.order.cnc.line')]) if file_ids: attach_file = attachment_obj.file_get(cr, uid, file_ids[0],context=context) input = PdfFileReader(attach_file) for page in input.pages: output.addPage(page) page_cnt += 1 if page_cnt > 0: full_path_temp = attachment_obj.full_path(cr, uid, 'temp') #+++ HoangTK - 01/15/2016 : Remove special character from file name #file_name = self._format_file_name(order.name) file_name = self._format_file_name(order.name) #--- HoangTK - 01/15/2016 : Remove special character from file name full_file_name = '%s/%s.pdf'%(full_path_temp, file_name,) outputStream = file(full_file_name, "wb") output.write(outputStream) outputStream.close() filedata = open(full_file_name,'rb').read().encode('base64') os.remove(full_file_name) return self.pool.get('file.down').download_data(cr, uid, "%s.pdf"%(file_name,), filedata, context) return False
def print_pdf(self, cr, uid, ids, context): attachment_obj = self.pool.get('ir.attachment') output = PdfFileWriter() page_cnt = 0 order = self.browse(cr, uid, ids[0], context=context) lines = self.browse(cr, uid, ids, context=context) for line in lines: if line.drawing_file_name and line.drawing_file_name.lower().endswith('.pdf'): file_ids = attachment_obj.search( cr, uid, [('name', '=', 'drawing_file'), ('res_id', '=', line.id), ('res_model', '=', 'drawing.order.line')]) if file_ids: attach_file = attachment_obj.file_get(cr, uid, file_ids[0],context=context) input = PdfFileReader(attach_file) for page in input.pages: output.addPage(page) page_cnt += 1 if page_cnt > 0: full_path_temp = attachment_obj.full_path(cr, uid, 'temp') # file_name = self._format_file_name(order.name) file_name = "Drawing" if context.get('order_name'): file_name = '%s-%s'%(file_name, self._format_file_name(context.get('order_name'))) full_file_name = '%s/%s.pdf'%(full_path_temp, file_name,) outputStream = file(full_file_name, "wb") output.write(outputStream) outputStream.close() filedata = open(full_file_name,'rb').read().encode('base64') os.remove(full_file_name) return self.pool.get('file.down').download_data(cr, uid, "%s.pdf"%(file_name,), filedata, context) else: raise osv.except_osv(_("Error!"),'No PDF files were found!') return False
def create_source_pdf(self, cr, uid, ids, data, report_xml, context=None): if not context: context={} pool = pooler.get_pool(cr.dbname) attach = report_xml.attachment aeroo_ooo = aeroo_ooo_test(cr) # Detect report_aeroo_ooo module context['aeroo_ooo'] = aeroo_ooo print_id = context.get('print_id', False) aeroo_print = self.active_prints[print_id] # Aeroo print object if attach or aeroo_ooo and report_xml.process_sep: objs = self.getObjects_mod(cr, uid, ids, report_xml.report_type, context) deferred = context.get('deferred_process') results = [] for obj in objs: aeroo_print.start_time = time.time() if deferred: deferred.progress_update() aname = attach and eval(attach, {'object':obj, 'time':time}) or False result = False if report_xml.attachment_use and aname and context.get('attachment_use', True): #aids = pool.get('ir.attachment').search(cr, uid, [('datas_fname','=',aname+'.pdf'),('res_model','=',self.table),('res_id','=',obj.id)]) #if aids: # brow_rec = pool.get('ir.attachment').browse(cr, uid, aids[0]) # if not brow_rec.datas: # continue # d = base64.decodestring(brow_rec.datas) # results.append((d,'pdf')) # continue cr.execute("SELECT id, datas_fname FROM ir_attachment WHERE datas_fname ilike %s and res_model=%s and res_id=%s LIMIT 1", (aname+'.%',self.table,obj.id)) search_res = cr.dictfetchone() if search_res: brow_rec = pool.get('ir.attachment').browse(cr, uid, search_res['id']) if not brow_rec.datas: continue d = base64.decodestring(brow_rec.datas) extension = search_res['datas_fname'].split('.')[1] results.append((d,extension)) continue result = self.create_single_pdf(cr, uid, [obj.id], data, report_xml, context) if not result: return False try: if attach and aname: name = aname+'.'+result[1] datas = base64.encodestring(result[0]) ctx = dict(context) ctx.pop('default_type', None) pool.get('ir.attachment').create(cr, uid, { 'name': aname, 'datas': datas, 'datas_fname': name, 'res_model': self.table, 'res_id': obj.id, 'type': 'binary' }, context=ctx ) cr.commit() except Exception,e: tb_s = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) logger.log(logging.ERROR, str(e)) results.append(result) if results and len(results)==1: return results[0] if results: not_pdf = filter(lambda r: r[1]!='pdf', results) if not_pdf: raise osv.except_osv(_('Error!'), _('Unsupported combination of formats!')) #if results[0][1]=='pdf': output = PdfFileWriter() for r in results: reader = PdfFileReader(StringIO(r[0])) for page in range(reader.getNumPages()): output.addPage(reader.getPage(page)) s = StringIO() output.write(s) return s.getvalue(), results[0][1]
def create_source_pdf(self, cr, uid, ids, data, report_xml, context=None): if not context: context={} pool = pooler.get_pool(cr.dbname) attach = report_xml.attachment aeroo_ooo = _aeroo_ooo_test(cr) # Detect report_aeroo_ooo module context['aeroo_ooo'] = aeroo_ooo print_id = context.get('print_id', False) aeroo_print = self.active_prints[print_id] # Aeroo print object if attach or aeroo_ooo and report_xml.process_sep: objs = self.getObjects_mod(cr, uid, ids, report_xml.report_type, context) deferred = context.get('deferred_process') results = [] for obj in objs: aeroo_print.start_time = time.time() if deferred: deferred.progress_update() aname = attach and eval(attach, {'object':obj, 'time':time}) or False result = False if report_xml.attachment_use and aname and context.get('attachment_use', True): #aids = pool.get('ir.attachment').search(cr, uid, [('datas_fname','=',aname+'.pdf'),('res_model','=',self.table),('res_id','=',obj.id)]) #if aids: # brow_rec = pool.get('ir.attachment').browse(cr, uid, aids[0]) # if not brow_rec.datas: # continue # d = base64.decodestring(brow_rec.datas) # results.append((d,'pdf')) # continue cr.execute("SELECT id, datas_fname FROM ir_attachment WHERE datas_fname ilike %s and res_model=%s and res_id=%s LIMIT 1", (aname+'.%',self.table,obj.id)) search_res = cr.dictfetchone() if search_res: brow_rec = pool.get('ir.attachment').browse(cr, uid, search_res['id']) if not brow_rec.datas: continue d = base64.decodestring(brow_rec.datas) extension = search_res['datas_fname'].split('.')[1] results.append((d,extension)) continue result = self.create_single_pdf(cr, uid, [obj.id], data, report_xml, context) if not result: return False try: if attach and aname: name = aname+'.'+result[1] datas = base64.encodestring(result[0]) ctx = dict(context) ctx.pop('default_type', None) vals = { 'name': aname, 'datas': datas, 'file_size': len(datas), 'datas_fname': name, 'res_model': self.table, 'res_id': obj.id, 'type': 'binary', } attachment_vals = self._get_attachment_create_vals( cr, uid, report_xml, vals, context=ctx) pool.get('ir.attachment').create(cr, uid, attachment_vals, context=ctx) cr.commit() except Exception,e: tb_s = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) self._logger.error(str(e)) results.append(result) if results and len(results)==1: return results[0] if results: not_pdf = filter(lambda r: r[1]!='pdf', results) if not_pdf: raise orm.except_orm(_('Error!'), _('Unsupported combination of formats!')) #if results[0][1]=='pdf': output = PdfFileWriter() for r in results: reader = PdfFileReader(StringIO(r[0])) for page in range(reader.getNumPages()): output.addPage(reader.getPage(page)) s = StringIO() output.write(s) return s.getvalue(), results[0][1]