英文:
Python Weasyprint to Google Bucket
问题
I am using Google Functions in order to generate PDFs.
我正在使用Google Functions来生成PDF文件。
I want to store the PDFs in a Google Bucket.
我想将这些PDF文件存储在Google Bucket中。
I know that I can store PDFs as a file using the following code:
我知道我可以使用以下代码将PDF文件存储为文件:
Write PDF to HTML
将PDF写入HTML
pdf = "
Hi!
"
HTML to PDF at local disk
将HTML转换为本地磁盘上的PDF
document = weasyprint.HTML(string=pdf, encoding='UTF-8')
document.write_pdf("Hello.pdf")
However, I want to store it in a Google Bucket, so I have tried the following code:
然而,我想将它存储在Google Bucket中,所以我尝试了以下代码:
Write PDF to HTML
将PDF写入HTML
pdf = "
Hi!
"
HTML to PDF in Google Bucket
将HTML转换为存储在Google Bucket中的PDF
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.
这将一个PDF文件存储在我的Google Bucket中,但它是无效的。
英文:
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.
答案1
得分: 1
你正在尝试将document
对象的字符串表示写入文件,但这不是PDF二进制数据,你可以将其转换为二进制然后直接写入Google Cloud存储。
from google.cloud import storage
import weasyprint
pdf = "<html><title>Hello</title><body><p>Hi!</p></body></html>"
document = weasyprint.HTML(string=pdf, encoding='UTF-8')
pdf_bytes = document.write_pdf()
client = storage.Client()
bucket = client.get_bucket("monthly-customer-reports")
blob = bucket.blob("Hello.pdf")
blob.upload_from_string(pdf_bytes, content_type='application/pdf')
英文:
You are trying to write the string representation of the document
object to the file, but this is not a PDF binary data, what you could do is convert to convert to binary then write it directly to Google Cloud storage.
from google.cloud import storage
import weasyprint
pdf = "<html><title>Hello</title><body><p>Hi!</p></body></html>"
document = weasyprint.HTML(string=pdf, encoding='UTF-8')
pdf_bytes = document.write_pdf()
client = storage.Client()
bucket = client.get_bucket("monthly-customer-reports")
blob = bucket.blob("Hello.pdf")
blob.upload_from_string(pdf_bytes, content_type='application/pdf')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论