V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Ewig
V2EX  ›  Python

把 html 文件转成 pdf

  •  
  •   Ewig · 2019-01-04 10:23:41 +08:00 · 4087 次点击
    这是一个创建于 1911 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import pdfkit

    pdfkit.from_url('http://www.sec.gov/Archives/edgar/data/1754581/000119312518361084/d620103df1.htm','/home/shenjianlin/out.pdf')

    这个链接是打开的是 html,现在我通过程序转成 pdf 但是现在转换过程中报错了,大家有没有更好的方法,谢谢


    Traceback (most recent call last):
    File "timeout.py", line 3, in <module>
    pdfkit.from_url('http://www.sec.gov/Archives/edgar/data/1754581/000119312518361084/d620103df1.htm','/home/shenjianlin/out.pdf')
    File "/usr/lib/python3.4/site-packages/pdfkit/api.py", line 26, in from_url
    return r.to_pdf(output_path)
    File "/usr/lib/python3.4/site-packages/pdfkit/pdfkit.py", line 153, in to_pdf
    'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr)
    OSError: wkhtmltopdf: cannot connect to X server

    You will need to run wkhtmltopdf within a "virtual" X server.
    Go to the link below for more information
    https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server
    15 条回复    2019-01-04 16:49:54 +08:00
    Kamitora
        1
    Kamitora  
       2019-01-04 10:50:09 +08:00 via iPhone   ❤️ 2
    chrome 打开,右键,打印,导出 pdf
    sethverlo
        2
    sethverlo  
       2019-01-04 10:53:09 +08:00
    wkhtmltopdf, pandoc, 还有个什么忘了,自己搜下吧,但是坑挺多的,不建议生产环境用,自己玩玩儿还是可以的
    sethverlo
        3
    sethverlo  
       2019-01-04 10:53:44 +08:00
    哦对了你这个 X server 指的是 x11
    binux
        5
    binux  
       2019-01-04 10:56:35 +08:00
    You will need to run wkhtmltopdf within a "virtual" X server.
    Go to the link below for more information
    https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server
    519718366
        7
    519718366  
       2019-01-04 10:57:44 +08:00
    chrome 插件:Full Page Screen Capture
    Aprilming
        8
    Aprilming  
       2019-01-04 11:19:56 +08:00
    你这个更新到最新的 wkhtmltopdf,好像就可以了,这个我之前用过,需要 X server,但是服务器没有 X server,需要安装其他包才能解决,上次看的时候最新的 wkhtmltopdf 貌似自带了。
    yidinghe
        9
    yidinghe  
       2019-01-04 11:22:01 +08:00   ❤️ 2
    用 Chrome 的 headless 模式直接转换,无需 xserver
    Aprilming
        10
    Aprilming  
       2019-01-04 11:23:05 +08:00
    这玩意在生产环境中,还是比较坑比的,自己玩玩就算了,主要是在服务器上转 pdf 的速度比较慢,还经常报错,还有文件名中文的问题,反正之前用的时候,坑的一批,最后为了解决慢的问题,还重构了代码,ps:在自己电脑转的时候不算慢,但是到了服务器上真的慢。主要还是 X server 的问题
    devforma
        11
    devforma  
       2019-01-04 11:47:20 +08:00
    https://yq.aliyun.com/articles/601753
    可以用阿里云函数计算搭一个服务
    nogoodren
        12
    nogoodren  
       2019-01-04 11:48:45 +08:00
    试一试虚拟打印机
    l0o0
        13
    l0o0  
       2019-01-04 13:38:39 +08:00
    如果用 python 的话,推荐你试试这个 https://github.com/Kozea/WeasyPrint,我感觉比较不错,打印出来的效果比 wkhtmltopdf 好挺多的。而且开发还挺活跃,有问题直接提 issue
    xjqaxl
        14
    xjqaxl  
       2019-01-04 14:26:52 +08:00
    phantomjs
    Ewig
        15
    Ewig  
    OP
       2019-01-04 16:49:54 +08:00
    已经解决,原因是版本太低,在外国论坛找了一个高版本的已经解决,谢谢诸位!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4146 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 05:26 · PVG 13:26 · LAX 22:26 · JFK 01:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.