我正在使用 pdfkit 来转换包含带有 href 属性的链接的 html 文件。
在 html 内部,href 是用相对路径编写的,例如:
<a href="folder/picture.jpg">PIC</a>
当我将其转换为 pdf 时,href 似乎会自动重写为绝对路径 (C:/Users/...
)。
为什么pdf会改变href?
最佳答案
pdfkit依赖的Wkhtmltopdf默认将相对链接转换为绝对链接。
这可以通过使用带有特殊标志的命令行工具来停止:
wkhtmltopdf --keep-relative-links src destination
或者告诉 pdfkit 应用此选项:
def convert_to_pdf(path):
try:
# run the conversion and write the result to a file
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
options = {
'--keep-relative-links': ''
}
pdfkit.from_url(path+'.htm', path+'.pdf', configuration=config, options=options)
except Exception as why:
# report the error
sys.stderr.write('Pdf Conversion Error: {}\n'.format(why))
raise
关于python - pdfkit 在转换时将 href 从相对路径更改为绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51515397/