pythonjinja2weasyprint

create a pdf file with Jinja without creating the html file


I'm creating a pdf file using an html template and weasyprint to convert the html file to a pdf file

The code works perfectly, but also create an html file like the pdf file

Is there a way to avoid creating the html file but only the pdf file??

this is my code

class Crud_db:
    def __init__(self, database = 'database.db'):
        self.database = database

    def connect(self):
        self.connection = sqlite3.connect(self.database)
        self.cursor = self.connection.cursor()
        # print('connect seccesfully')

    def execute(self, query):
        self.query = query
        self.cursor.execute(self.query)

    def close(self): 
        self.connection.commit()
        self.connection.close()

    def save_the_last_bill_to_html_pdf(self):
        env = Environment(loader=FileSystemLoader('templates'))
        # 3. Load the template from the Environment
        template = env.get_template('th_bill.html')

        # retrive the last general bill and details bill
        self.connect()
        query_general_bill = ''' SELECT general_bill.id, general_bill.client_name , general_bill.total , general_bill.total_margin , general_bill.number_of_products , general_bill.date_g ,general_bill.time_g , users.username user_id 
        FROM general_bill join users 
        on general_bill.user_id = users.id
        WHERE general_bill.id=(SELECT MAX(id) FROM general_bill) '''
        self.cursor.execute(query_general_bill)
        result_general_bill = self.cursor.fetchall()
        

        #  retrive the detail bill 

        query_details_bill = ''' SELECT product.product_name products, details_bill.number_of_products, details_bill.prix, details_bill.margin, details_bill.date, details_bill.time 
        FROM details_bill join product
        on product.id = products
        WHERE details_bill.general_bill_id = (SELECT MAX(id) FROM general_bill)'''
        self.cursor.execute(query_details_bill)
        result_detail_bill = self.cursor.fetchall() 
        # print(result_detail_bill)

        html = template.render(result_general = result_general_bill,
                            result_detail_bill = result_detail_bill)

        with open('html_report_jinja.html', 'w') as f:
            f.write(html)

        css = CSS(string='''
            @page {size: A4; margin: 1cm;} 
            th, td {border: 1px solid black;}
            ''')
        HTML('html_report_jinja.html').write_pdf('weasyprint_pdf_report.pdf', stylesheets=[css])


Solution

  • Thanks to a @K J idea I just add a delete functionality at the end of the function

    And the code will be like this

    def save_the_last_bill_to_html_pdf(self):
            env = Environment(loader=FileSystemLoader('templates'))
            # 3. Load the template from the Environment
            template = env.get_template('th_bill.html')
    
            # retrive the last general bill and details bill
            self.connect()
            query_general_bill = ''' SELECT general_bill.id, general_bill.client_name , general_bill.total , general_bill.total_margin , general_bill.number_of_products , general_bill.date_g ,general_bill.time_g , users.username user_id 
            FROM general_bill join users 
            on general_bill.user_id = users.id
            WHERE general_bill.id=(SELECT MAX(id) FROM general_bill) '''
            self.cursor.execute(query_general_bill)
            result_general_bill = self.cursor.fetchall()
            # print(result_general_bill)
    
            #  retrive the detail bill 
    
            query_details_bill = ''' SELECT product.product_name products, details_bill.number_of_products, details_bill.prix, details_bill.margin, details_bill.date, details_bill.time 
            FROM details_bill join product
            on product.id = products
            WHERE details_bill.general_bill_id = (SELECT MAX(id) FROM general_bill)'''
            self.cursor.execute(query_details_bill)
            result_detail_bill = self.cursor.fetchall() 
            # print(result_detail_bill)
    
            html = template.render(result_general = result_general_bill,
                                result_detail_bill = result_detail_bill)
    
            with open('html_report_jinja.html', 'w') as f:
                f.write(html)
    
            css = CSS(string='''
                @page {size: A4; margin: 1cm;} 
                th, td {border: 1px solid black;}
                ''')
            HTML('html_report_jinja.html').write_pdf('weasyprint_pdf_report.pdf', stylesheets=[css])
            print('pdf file and html file created successfully')
    
            if os.path.exists("html_report_jinja.html"):
                os.remove("html_report_jinja.html")
                print("The file has been deleted successfully")
            else:
                print("The file does not exist!")