当前位置: 首页 > 工具软件 > PDFKit > 使用案例 >

pdfkit

益绯辞
2023-12-01

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')

 

 类似资料:

相关阅读

相关文章

相关问答