V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
askask
V2EX  ›  Django

django xhtml2pdf 如何写入 CSS 和图片

  •  
  •   askask · 2016-04-19 16:54:22 +08:00 · 3393 次点击
    这是一个创建于 2901 天前的主题,其中的信息可能已经有所发展或是发生改变。
    from xhtml2pdf import pisa


    html = '<html><body><p><h1>To PDF </h1>or <h2>not to PDF</h2><p></body></html>'

    outputFilename = 'test.pdf'


    def convertHtmlToPdf(html, outputFilename):
    resultFile = open(outputFilename, "w+b")
    pisaStatus = pisa.CreatePDF(html,dest=resultFile)
    resultFile.close()
    return pisaStatus.err

    if __name__=='__main__':
    pisa.showLogging()
    convertHtmlToPdf(html, outputFilename)



    求助。如果 HTML 当中有 CSS 和图片如何写入到 PDF 了?
    4 条回复    2016-04-26 08:44:17 +08:00
    askask
        1
    askask  
    OP
       2016-04-19 17:13:29 +08:00
    http://qa.helplib.com/471954 找到一篇文章 看不太名单 myview 和模板是怎么搭配的
    yybeta
        2
    yybeta  
       2016-04-19 17:25:59 +08:00
    用 pdfcrowd 实现过,感觉比 xhtml2pdf 排版等问题要少得多, http://pdfcrowd.com/
    很容易实现,唯一的坏处是免费配额只有 100 次好像。
    askask
        3
    askask  
    OP
       2016-04-19 17:42:48 +08:00
    @yybeta 问题是要钱哟~
    askask
        4
    askask  
    OP
       2016-04-26 08:44:17 +08:00
    已解决。

    直接采用 python-pdfkit ,亲测好用。

    分享下链接:https://github.com/JazzCore/python-pdfkit

    直接按照说明安装即可。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5240 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:17 · PVG 17:17 · LAX 02:17 · JFK 05:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.