创建pdf

时间:2017-01-23 09:34:43

标签: python django rest

我有一个脚本可以生成PDF文件,然后将其发送到另一个应用程序(LogicalDOC)以保存它。

我遇到了payload行的问题。文件名应该是我的文件名变量,但不考虑。

如果我写filename.pdf,我会在LogicalDOC中找到带有良好内容的filename.pdf。

但是文件名必须为每个新的BirthCertificate自动更改。

如何将路径作为文件名传递?

这是我的剧本:

@login_required
def BirthCertificate_PDF(request, id) :

    birthcertificate = get_object_or_404(BirthCertificate, pk=id)

    data = {"birthcertificate" : birthcertificate}

    template = get_template('BC_raw.html')
    html  = template.render(Context(data))


    filename_directory = str(BirthCertificate.objects.get(pk=id).lastname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).firstname.encode('utf-8')) + "_" + str(BirthCertificate.objects.get(pk=id).birthday)
    filename = 'Acte_Naissance_' + filename_directory + '.pdf'
    path = '/Users/valentinjungbluth/Desktop/Django/Individus/' + filename

    file = open(path, "w+b")
    pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')

    file.seek(0)
    pdf = file.read()
    if pdf :

        payload = '{ "language":"fr","fileName":filename,"folderId":3309569 }'
        upfile = path
        files = { 
        'document': (None, payload, 'application/json'),
        'content': (os.path.basename(upfile), open(upfile, 'rb'), 'application/octet-stream')
        } 
        url = 'http://localhost:8080/services/rest/document/create'
        headers = {'Content-Type': 'multipart/form-data'}
        r = requests.post(url, files=files, headers=headers, auth=('admin', 'admin'))

        context = {"birthcertificate":birthcertificate,
                   "path":path}

        return render(request, 'BC_PDF.html', context)
    file.close()

    return HttpResponse(pdf, 'application/pdf')

如果我写道:

  • "FileName":"test.pdf"我在logicalDoc中获取了test.pdf文件(见图片)
  • "FileName":"filename"我在logicalDoc中获取格式未知的文件名文件(见图片)

我想将filename variable作为文档名称

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用字符串格式。请按照:

Caused by: java.lang.NoClassDefFoundError: com_atlassian_clover/TestNameSniffer