Python Weasyprint to Google Bucket

huangapple go评论63阅读模式
英文:

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 = "Hello

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 = "Hello

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 = &quot;&lt;html&gt;&lt;title&gt;Hello&lt;/title&gt;&lt;body&gt;&lt;p&gt;Hi!&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;&quot;

document = weasyprint.HTML(string=pdf, encoding=&#39;UTF-8&#39;)
pdf_bytes = document.write_pdf()

client = storage.Client()
bucket = client.get_bucket(&quot;monthly-customer-reports&quot;)
blob = bucket.blob(&quot;Hello.pdf&quot;)
blob.upload_from_string(pdf_bytes, content_type=&#39;application/pdf&#39;)

huangapple
  • 本文由 发表于 2023年4月13日 17:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定