This question has been flagged
2 Replies
3316 Views

Hi 

I have a pice of code what I use to get a PDF byte string which is created when I try to generate a PDF Document from 'account_move' module. 

But the problem is that PDF file is empty. I take only metadata of the file.

```

​import base64
import datetime
import os
import threading

from dotenv import load_dotenv
from odoo.http import request
from odoo.tools.config import config
from odoo.tools import pdf
from telebot import apihelper, types

from odoo import SUPERUSER_ID, api, fields, http, models, sql_db, _

from . import bot_controller


class AccountMove(models.Model):
​_inherit = 'account.move'
 
 
​automatic_send = fields.Char(compute='auto_send_invoice')
​pdf_file = fields.Binary(string="PDF File", readonly=True)
 
​@api.constrains('state')
​def auto_send_invoice(self, ):
​if self.state == 'posted' and self.move_type == 'out_invoice' and : ​self.env['ir.actions.report']._get_report_from_name('telegram_bot_module.report_tg')

​pdf_content = report._render_qweb_pdf(report.id)
 
​invoice_name = f"invoice_{self.id}.pdf"

 
​with open('invoice_from_action.pdf', 'wb') as file:
​file.write((pdf_file[0]))rning(f"PDF content: {(report)}")
 

 

```

Avatar
Discard
Author Best Answer

[Solved] by using next code:

report = self.env['ir.actions.report'].sudo()._get_report_from_name(
'account.report_invoice_with_payments')

pdf_file = report._render_qweb_pdf(report.id, record.id)
pdf_file = pdf_file[0]
record.pdf_file = base64.b64encode(pdf_file)

Avatar
Discard
Best Answer

Hello Oleg Zubak,

I think you have passed wrong id in report. 

Please find code in comment. 

Hope it will be helpful to you.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

Please find code here :-

pdf_content = report._render_qweb_pdf(report.id)

In report._render_qweb_pdf you have passed report's report.id instead of current record id(account.move)
Here, you can see how to pass current id for any report
In pdf_content you will get complete rendered report

@api.constrains('state')
def auto_send_invoice(self, ):
if self.state == 'posted' and self.move_type == 'out_invoice':
report = self.env['ir.actions.report']
pdf_content = report._render_qweb_pdf('telegram_bot_module.report_tg', res_ids=self.id)