[英]How to convert Webpage page or HTML url to pdf?
我正在嘗試將HTML頁面或HTML URL轉換為pdf,不僅可以轉換html,還可以轉換css並保存它。我很困惑我應該使用什么(weasyprint,wkhtmltopdf或python pdfkit)。 同時,我正在使用以下代碼:
def ConvertToPdf(urltoConvert=None):
import pdfkit
pdfFormatOptions= {'page-size':'Letter', 'disable-forms':'','zoom': 1}
pdfObject = None
try:
pdfkit.from_url('http://tdi.dartmouth.edu/', 'dart.pdf')
except:
Exception while converting"
pass
return pdfObject
if __name__ == "__main__":
# url ='http://tdi.dartmouth.edu/'
ConvertToPdf()
和此代碼
import weasyprint
pdf = weasyprint.HTML('http://tdi.dartmouth.edu/').write_pdf()
len(pdf)
file('dart.pdf', 'w').write(pdf)
但一切都是徒勞的,請幫忙。
您可能要嘗試使用: https : //pypi.python.org/pypi/pdfkit
它也具有保存CSS的功能
You can specify external CSS files when converting files or strings using css option.
Warning This is a workaround for this bug in wkhtmltopdf. You should try –user-style-sheet option first.
# Single CSS file
css = 'example.css'
pdfkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css = ['example.css', 'example2.css']
pdfkit.from_file('file.html', options=options, css=css)
這應該工作正常
import pdfkit
pdfkit.from_url('http://google.com', 'res.pdf')
另外,另一種解決方案可能是通過硒制作屏幕截圖,然后從這些圖像合成.pdf。 但是,這很臟。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.