gpt4 book ai didi

Python 将请求 header 传递给 pdfkit

转载 作者:太空宇宙 更新时间:2023-11-03 17:03:28 24 4
gpt4 key购买 nike

我正在尝试使用 python 中的 pdfkit 将 URL 转换为 pdf,如下所示。

import pdfkit
pdfkit.from_url(url, file_path)

我想知道是否有某种方法可以将带有此 URL 的自定义请求 header (例如 X-Proxy-REMOTE-USER)传递给某些内容。

最佳答案

python-pdfkit 只是 wkhtmltopdf 的包装。查看 usage section of the docs 中的第五个示例,您可以为其他选项指定第三个参数:

options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None
}

pdfkit.from_url('http://google.com', 'out.pdf', options=options)

options are specified here 并且您对 --custom-header <name> <value> 特别感兴趣。不幸的是,他们没有说明如何传递带有多个参数的选项,但由于命令行需要两者之间有一个空格,并且查看代码,他们似乎没有更改值参数,我会尝试仅传递 name value 作为选项的值,两者之间有一个空格。

options = {
'custom-header': 'X-Proxy-REMOTE-USER STEVE'
}

pdfkit.from_url('http://google.com', 'out.pdf', options=options)

关于Python 将请求 header 传递给 pdfkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34818475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com