Search code examples
controllerodooqwebodoo-16

How to make custom QWeb report render on public URL in Odoo


In odoo we can access inbuilt object via token url like this:

http://localhost:8069/my/invoices/8336?access_token=96c52b2d-3604-4817-82be-4392aa8d051e&report_type=pdf

If I have designed custom qweb report PDF like this

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
    <template id="vendor_bill_report">
        <t t-call="web.html_container">
            <t t-foreach="docs" t-as="o">
                <!-- Removing the default Odoo header/footer by avoiding t-call to web.external_layout -->
                <html lang="en">
                </html>
            </t>
        </t>
    </template>

    <record id="paperformat_sensorglobal_vendor_bill" model="report.paperformat">
        <field name="name">Sensorglobal Vendor Bill</field>
        <field name="default" eval="False"/>
        <field name="format">custom</field>
        <field name="page_height">266</field>
        <field name="page_width">210</field>
        <field name="margin_top">0</field>
        <field name="margin_bottom">0</field>
        <field name="margin_left">0</field>
        <field name="margin_right">0</field>
        <field name="orientation">Portrait</field>
        <field name="dpi">100</field>
    </record>
    
    <!-- Report Action -->
    <record id="report_account_move_action" model="ir.actions.report">
        <field name="name">SG Vendor Bill</field>
        <field name="model">account.move</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">module_name.vendor_bill_report</field>
        <field name="report_file">module_name.vendor_bill_report</field>
        <field name="print_report_name">'V_%s_%s' % (object.name, object.create_date.strftime("%b_%y").upper())</field>
        <field name="binding_model_id" ref="account.model_account_move" />
        <field name="binding_type">report</field>
        <field name="paperformat_id" ref="module_name.paperformat_sensorglobal_vendor_bill"/>
    </record>
</odoo>

If I want to access this PDF on custom url like this: http://localhost:8069/my/custom_vendor_bills/8337?access_token=e5751590-8027-4f2c-b5ab-a0d62f675721&report_type=pdf

How can i achieve this, it tried adding some controller code, but it did not work:

from odoo import http
from odoo.http import request

class CustomVendorBillReport(http.Controller):

@http.route(['/my/custom_vendor_bills/<int:move_id>'], type='http', auth="public", website=True)
def custom_vendor_bill_report(self, move_id, access_token=None, **kw):
    try:
        # Get the account.move object based on the move_id and access token
        move = request.env['account.move'].sudo().browse(move_id)
        print("move", move)

        if not move or not move.check_access_rights('read', raise_exception=False):
            print("test")
            return request.redirect('/my')  # Redirect to a default page if unauthorized

        # Generate the report
        pdf = request.env.ref('ir.actions.report').sudo()._render_qweb_html('module_name.vendor_bill_report', [move_id])[0]
        pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]

        return request.make_response(pdf, headers=pdfhttpheaders)

    except Exception as e:
        return request.not_found()

can anyone help me here to render custom qweb report on public url. I am using odoo version 16 TIA


Solution

  • class CustomVendorBillReport(http.Controller):
    
        @http.route(['/my/vendor_bills/<int:move_id>'], type='http', auth="public", website=True)
        def custom_vendor_bill_report(self, move_id, access_token=None, **kw):
            try:
                # Get the account.move object based on the move_id and access token
                move = request.env['account.move'].sudo().browse(move_id)
                print("move", move)
    
                if not move or not move.check_access_rights('read', raise_exception=False):
                    print("test")
                    return request.redirect('/my')  # Redirect to a default page if unauthorized
    
                pdf, _ = request.env['ir.actions.report'].sudo()._render_qweb_pdf('sg_util.vendor_bill_report', [move_id])
                pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
                return request.make_response(pdf, headers=pdfhttpheaders)
    
            except Exception as e:
                return request.not_found()
    

    this code finally worked!