8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

使用 libreoffice 将 docx 文件转换为 pdf 时显示错误

André Lehto 2月前

49 0

我以 html 格式上传 docx 文件,然后我想在 ubuntu 服务器中将此文件转换为 pdf,但它显示错误,例如 CalledProcessError at /add-documentCommand '['libreoffice', '--headless', '--conver...

我以 html 格式上传 docx 文件,然后我想在 ubuntu 服务器中将此文件转换为 pdf,但它显示错误,如 CalledProcessError at /add-documentCommand '['libreoffice', '--headless', '--convert-to', 'pdf', '/var/www/html/dev/media/documents/Lavender_Bay_Boatshed_Website_Testing_61gWm6r.docx', '--outdir', '/var/www/html/dev/media/documents/Lavender Bay Boatshed Website Testing.pdf']' 返回非零退出状态 77。

@login_required
def add_document(request):
    title = "Add document"
    if request.method == 'POST':
        file = request.FILES.get('file')
        check = request.POST.get('check')
        
        subject = request.POST.get('subject')
        message = request.POST.get('message')
        print(request.POST)

        names = []
        emails = []
        for key in request.POST:
            
            if key.startswith('email_'):
                emails.append(request.POST[key])
            elif key.startswith('name_'):
                names.append(request.POST[key])
        print(emails)
        print(names)
        recipients = []
        list_of_dicts = [{'email': email, 'name': name} for email, name in zip(emails, names)]
        if file:
            if (file.name.endswith('.jpg')) or (file.name.endswith('.png')):
                obj = Documents(document=file,name=file.name,user=request.user)
                obj.save()
                image = Image.open(obj.document.path)
                iml = image.convert('RGB')
                folder = r'C:\Users\admin\Desktop\projects\Document-sign\media\documents'
                path = os.path.join(folder, file.name.split('.')[0] + '.pdf')
                os.remove(obj.document.path)
                iml.save(path)
                obj.document = f"documents/{file.name.split('.')[0]}.pdf"
                obj.save()
                os.remove(obj.document.path)
                if check:
                    request.session['check'] = False
                    return redirect('sign_document')
                else:
                    recipients.append({'emails':list_of_dicts,'subject':subject,'message':message})
                    request.session['recipients'] = recipients
                    request.session['check'] = True
                    return redirect('make_envelope')
                
            elif file.name.endswith('.docx'):
                obj = Documents(document=file, name=file.name, user=request.user)
                obj.save()

                # Define paths
                path = os.path.join(settings.MEDIA_ROOT, 'documents')
                input_path = obj.document.path
                

                # Run LibreOffice conversion
                try:
                    result = subprocess.run(
                        ['libreoffice', '--headless', '--convert-to', 'pdf', input_path, '--outdir', path],
                        check=True,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE
                    )
                    print(result.stdout)
                    print(result.stderr)
                except subprocess.CalledProcessError as e:
                    print("Error occurred:", e.stderr)
                    raise

                # Remove the original DOCX file
                os.remove(input_path)

                # Update the document path to the converted PDF
                obj.document = f"documents/{file.name.split('.')[0]}.pdf"
                obj.save()
                
                if check:
                    request.session['check'] = False
                    return redirect('sign_document')
                else:
                    recipients.append({'emails':list_of_dicts,'subject':subject,'message':message})
                    request.session['recipients'] = recipients
                    request.session['check'] = True
                    return redirect('make_envelope')
            else:
                obj = Documents(document=file,name=file.name,user=request.user)
                obj.save()
                if check:
                    
                    request.session['check'] = False
                    return redirect('sign_document')
                else:
                    recipients.append({'emails':list_of_dicts,'subject':subject,'message':message})
                    print(recipients)
                    request.session['recipients'] = recipients
                    request.session['check'] = True
                    return redirect('make_envelope')   
            
    context = {'title':title}
    return render(request, 'myapp/add_document.html',context=context)

帖子版权声明 1、本帖标题:使用 libreoffice 将 docx 文件转换为 pdf 时显示错误
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由André Lehto在本站《django》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 我将 '--convert-to'、'pdf' 更改为 '--convert-to' 'pdf:writer_pdf_Export' 并再次运行,但浏览器中显示 'bufsize 必须是整数' 错误

  • 我们可能需要一些其他信息。请尝试从以下内容开始:

    您要转换的文件的 obj.document.path 路径 ( os.path.isfile(obj.document.path) 调用 libreoffice 之前添加一个即可。

    2-确保您的环境中安装了 libreoffice: libreoffice --version

    3- 尝试使用 --convert-to pdf:writer_pdf_Export 不仅仅是 --convert-to pdf

    4-确保输出目录存在

    5- 确保 libreoffice 没有打开 GUI 实例

  • 谷歌搜索“libreoffice headless 77”我得到的印象是退出代码 77 通常表示权限问题,例如

返回
作者最近主题: