I am using Google Functions in order to generate PDFs.
I want to store the PDFs in a Google Bucket.
I know that I can store PDFs as a file using the following code:
# Write PDF to HTML
pdf = "<html><title>Hello</title><body><p>Hi!</p></body></html>"
# HTML to PDF at local disk
document = weasyprint.HTML(string=pdf, encoding='UTF-8')
document.write_pdf(f"Hello.pdf")
However I want to store it in a Google Bucket, so I have tried the following code :
# Write PDF to HTML
pdf = "<html><title>Hello</title><body><p>Hi!</p></body></html>"
# HTML to PDF in Google Bucket
document = weasyprint.HTML(string=pdf, encoding='UTF-8')
client = storage.Client()
bucket = client.get_bucket("monthly-customer-reports")
blob = bucket.blob("Hello.pdf")
with blob.open("w") as f:
f.write(str(document))
This stored a PDF in my Google Bucket but it was invalid.