How to Generate Pdf Report from Python Code. Odoo 16

I have a custom wizard with fields : "file_name"(Char), "model_id"(active model), "content_report"(html) . I added a print button in this wizard . When I click on this button I want to generate reporte with name = "file_name" and content of report = "content_report" , without using a report template. Only with python code?

How can I do it? Any help please?

Thanks.

1 Answer

You can use _run_wkhtmltopdf function used to print the qweb reports.

Example:

def render_html(self):
    self.ensure_one()
    
    action_report = self.env['ir.actions.report']

    pdf_content = action_report._run_wkhtmltopdf(
        [self.content_report],
        report_ref=None,
        header=header,
        footer=footer,
        landscape=False,
        specific_paperformat_args=specific_paperformat_args,
        set_viewport_size=False,
    )

    self.update({'file_content': base64.b64encode(pdf_content)})

Your HTML should be enclosed in main tag

6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest