I would like to generate invoices with a little Python program I've written. I want it to output the invoice first as an HTML file, then convert it to a PDF. Currently my code is creating the HTML doc as desired, but it is outputting a blank PDF file:
import pdfkit
import time
name = input('Enter business name: ')
date = input('Enter the date: ')
invoice = input('Enter invoice number: ')
total = 1000 # Fixed number for now just for testing purposes
f = open('invoice.html','w')
message = f"""<html>
<head>
<link rel='stylesheet' href='style.css')>
</head>
<body>
<h1>INVOICE<span id="invoice-number"> {invoice}</h1>
<h3>{date} </h3>
<h3>{name} </h3>
<h3>Total: ${total}</h3>
</body>
</html>"""
f.write(message)
time.sleep(2)
options = {
'enable-local-file-access': None
}
pdfkit.from_file('invoice.html', 'out.pdf', options = options)
When I run the code above, the HTML doc it puts out looks fine, but the PDF it creates is blank. I receive no error messages at all. The output in Terminal is:
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
However, I have another file called test.py in the same directory. This file contains the import statement, exactly the same last four lines as the first code, and nothing else:
import pdfkit
options = {
'enable-local-file-access': None
}
pdfkit.from_file('invoice.html', 'out.pdf', options = options)
If I run this file after the previous file, it correctly outputs the PDF to look just like the HTML version. Why does this code work when run in a separate file, but doesn't work when included in the original file? How can I get it to run in the same file so I don't have to execute two commands to get an invoice?