把 html 文件转成 pdf - V2EX
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 4744 次点击
    这是一个创建于 2477 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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     Solana     950 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 19:30 PVG 03:30 LAX 12:30 JFK 15:30
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86