Odoo14_pdf下载功能实现

1.安装wkhtmltopdf

sudo apt install wkhtmltopdf

2.安装pip包

pip3 install pdfkit

3.代码实现

# -*- coding: utf-8 -*-
from odoo import http
import pdfkit


class FsnPlan(http.Controller):

    @http.route('/test/download_pdf/', auth='public')
    def test_download_pdf(self, **kw):

        html = "<h1>Hello World !!!哈哈哈</h1>"

        attachment = pdfkit.from_string(
                html,
                options={
                    'page-size': 'A4',
                    'margin-top': '0',
                    'margin-right': '0',
                    'margin-left': '0',
                    'margin-bottom': '0',
                    'zoom': '1.2',
                    'encoding': "UTF-8",
                })


        response = http.request.make_response(attachment)
        response.headers['Content-type'] = 'application/pdf'  # 指定返回的类型
        response.headers["Content-Disposition"] = "inline; filename=output.pdf"     # 文件名称
        return response

 

posted @ 2023-02-24 14:24  手可摘星辰。  阅读(83)  评论(0编辑  收藏  举报