pdfkit

pdfkit參考: http://blog.sina.com.cn/s/blog_725d6c650102x2k4.html

https://www.cnblogs.com/niejinmei/p/8157680.html

https://www.cnblogs.com/xingzhui/p/7887212.html

遇到的問題: https://blog.csdn.net/u012561176/article/details/83655247

項目的應用:

# 服務器版
def viewpdf(request):
    # Create a URL of our project and go to the template route
    aim = request.GET['aim']
    beginDate = request.GET.get('beginDate')
    endDate = request.GET.get('endDate')
    options = {
        'page-size': 'A4',
    }
    url = request.GET.get('url','')+"&aim="+aim
    if beginDate and endDate:
        url += "&beginDate=" + beginDate + "&endDate=" + endDate
    projectUrl = request.get_host() + url
    pdf = pdfkit.from_url(projectUrl, False,options=options)
    # Generate download
    response = StreamingHttpResponse(pdf)
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="shigongriji.pdf"'
    return response

# 測試
def viewpdf_test(request):
    # Create a URL of our project and go to the template route
    aim = request.GET['aim']
    beginDate = request.GET.get('beginDate')
    endDate = request.GET.get('endDate')

    options = {
        'page-size': 'A4',
    }
    url = request.GET.get('url','')+"&aim="+aim
    if beginDate and endDate:
        url += "&beginDate="+beginDate + "&endDate="+endDate
    projectUrl = request.get_host() + url
    path_wk = r'C:\wkhtmltox\bin\wkhtmltopdf.exe'  # 安裝位置
    config = pdfkit.configuration(wkhtmltopdf=path_wk)
    pdf = pdfkit.from_url(projectUrl, False, configuration=config, options=options)

    # Generate download
    response = StreamingHttpResponse(pdf)
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="shigongriji.pdf"'
    return response


def shigongriji(request):
    response_data={}
    allBody=[]
    cid=request.GET['id']
    aim=request.GET['aim']
    aBody=getABody(cid)
    if aBody != None:
        allBody.append(aBody)
    response_data["length"]=len(allBody)
    response_data["data"]=allBody
    if len(allBody)>0:
        if aim == "preview" :
            return render_to_response('projects/shigongriji.html',response_data)
        else :
            return render_to_response('projects/pdfmodel.html',response_data)
    else :
        return render_to_response('projects/nodata.html')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章