将需要身份验证的网页转换为pdf

g52tjvyc  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(207)

我正试图以pdf格式归档来自需要身份验证的内部服务的一组内容。我已经能够使用pdfkit从url生成pdf:

pdfkit.from_url(url="https://www.google.ca", output_path="google.pdf")

我还能够通过请求模块的身份验证获取请求页面的内容:

cookies = { 
    # snip
}

headers = {
    # snip 
}

data = requests.get("https://site", headers=headers, cookies=cookies, verify=False)

htmlcontent = data.content.decode("utf-8")

pdfkit.from_string(htmlcontent, "out.pdf")
``` `htmlcontent` 包含正确的html,但pdfkit抱怨(我认为)它无法访问css文件、图像等:

OSError: wkhtmltopdf reported an error:
Loading pages (1/6)
Warning: Blocked access to file
Warning: Blocked access to file
Warning: Blocked access to file
Warning: Blocked access to file
Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown

etc...

通过pdfkit,它似乎不支持为url请求提供会话信息的方法
有没有一种方法(或示例)可以在这种情况下生成pdf?
提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题