我以 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)
使用 libreoffice 将 docx 文件转换为 pdf 时显示错误
下载声明:
本站所有软件和资料均为软件作者提供或网友推荐发布而来,仅供学习和研究使用,不得用于任何商业用途。如本站不慎侵犯你的版权请联系我,我将及时处理,并撤下相关内容!