With fastapi, I created this endpoint
@app.post("/html_pdf")
def html_pdf(html: str = Form(), css: str = Form(""), orientation: str = Form("")):
rutaTMP = "/tmp/"
suffix = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
path_to_wkhtmltopdf = r'/usr/bin/wkhtmltopdf'
rute_and_filename = "{}{}-{}.pdf".format(rutaTMP,
str(uuid.uuid4()), suffix)
cssRute = "{}css{}-{}.css".format(rutaTMP, str(uuid.uuid4()), suffix)
config = pdfkit.configuration(wkhtmltopdf=path_to_wkhtmltopdf)
f = open(cssRute, "a")
f.write(css)
f.close()
options = {'encoding': 'UTF-8'}
if orientation:
options['orientation'] = orientation
pdfkit.from_string(html,
output_path=rute_and_filename,
configuration=config,
css=cssRute,
options=options)
try:
with open(rute_and_filename, "rb") as pdf_file:
encoded_string = str(base64.b64encode(pdf_file.read()))
encoded_string = encoded_string.split("'")[1]
remove(cssRute)
remove(rute_and_filename)
return encoded_string
except:
return None
It receives the html, the css and the orientation, then returns the encoded string for a PDF file. The html contain the definition of a table. Now I need to repeat the header of the table in every page. Also, I need to add only to the first page, another custom html (who contain another table with some extra info). The document must look as follows: First page:
- Information
- Header of the table with the names of columns
- The table
Next pages:
- Header of the table with the names of columns
- The rest of the table
I tried passing the cover option to from_string, but I got only errors.