zoukankan      html  css  js  c++  java
  • python pdfkit html转pdf响应式轮子 django例

    pip install pdfkit 

    本例用django做的请求,换成对应框架即可

    此方法可将html页面转成pdf下载

     1 #!/usr/bin/env python
     2 # coding:utf-8
     3 
     4 import pdfkit
     5 import random
     6 import datetime
     7 from django.shortcuts import HttpResponse
     8 
     9 
    10 #size:'A6','A4' etc...
    11 def get_pdf(url, size):
    12     options = {'page-size':size, 'margin-top':'0','margin-right':'0', 'margin-bottom':'0', 'margin-left':'0'}
    13     output = '/tmp/x.pdf-%d' % random.randint(1000, 9999)
    14 
    15     pdfkit.from_url(str(url), output, options=options)
    16     response = HttpResponse(content_type='application/pdf')
    17 
    18     now = datetime.datetime.now()
    19     pdfname = now.strftime('%Y%m%d-%H%M%S')
    20     response['Content-Disposition'] = 'attachment; filename="'+pdfname+'.pdf"'
    21 
    22     with file(output) as fp:
    23         data = fp.read()
    24 
    25     response.write(data)
    26     return response
  • 相关阅读:
    Apollo与ROS
    QT windeployqt
    自定义QGraphicsItem
    ROS与C++
    aptitude与apt-get
    解决tcp粘包问题
    网络中两台主机通信
    I/O多路复用之select、poll、epoll
    Nginx命令行控制
    C++11
  • 原文地址:https://www.cnblogs.com/cutesnow/p/7503978.html
Copyright © 2011-2022 走看看