odoo15接口调用qweb打印,将pdf旋转并下载到本地
一、将pdf旋转的通用方法
def rotate_pdf(self, pdf, angle): """ rotateClockwise(90) 这里的pdf传:bytes类型 这里的angel:角度 90 180 270 360度 getNumPages将每一页都旋转 """ writer = PdfFileWriter() reader = PdfFileReader(io.BytesIO(pdf), strict=False) for page in range(0, reader.getNumPages()): page = reader.getPage(page) page.rotateClockwise(angle) writer.addPage(page) with io.BytesIO() as _buffer: writer.write(_buffer) return _buffer.getvalue()
二、通过接口将report打印,并提供下载
@http.route('/fmcg/download/pdf', type='http', auth="public", csrf=False, cors='*') def melon_download_pdf(self, **kw): """下载图片接口:直接返回的是一个文件对象,可直接下载""" data = [] id = kw['id']
#获取打印单的 bytes文件 report = request.env.ref('sale.action_transaction_report').with_user(SUPERUSER_ID)._render_qweb_pdf(int(kw['id']))
report_datas = self.rotate_pdf(report[0],90)
data = io.BytesIO(report_datas)
return http.send_file(data, filename='transaction_order.pdf', as_attachment=True)
三、接口里面传附件方式:1、传文件对象;
2、传地址;
@http.route('/upload/image/api', type='http', methods=['POST'], auth="public", csrf=False, cors='*') def upload_store_image(self, **kw): """上传图片接口:sale.order
这里是文件对象
""" _logger.info("---------上传图片接口--kw--------------------------:%s", kw) if not kw['file']: return json.dumps({'result': 'fail', 'message': 'no image'}) base64_data = base64.b64encode(kw['file'].read()) attachment = request.env['ir.attachment'].sudo().create({ 'name': kw['file'].filename, 'datas': base64_data, 'res_model': 'sale.order', }) image = '%s/web/image/%s-%s' % (url_path, str(attachment.id), attachment.checksum) if attachment else '' return json.dumps({'code': 200, 'message': 'success', 'data':[]})
心有猛虎,细嗅蔷薇