Python pdfbox的flask RESTful服务

Python pdfbox的flask RESTful服务,python,rest,flask,pdfbox,Python,Rest,Flask,Pdfbox,我使用以下python代码调用pdfbox #!/usr/bin/env python3 import jpype import jpype.imports jpype.addClassPath(sys.argv[1]) jpype.startJVM(convertStrings=False) import org.apache.pdfbox.tools as tools tools.ExtractText.main(['-startPage', '1', sys.argv[2], sys.a

我使用以下python代码调用pdfbox

#!/usr/bin/env python3

import jpype
import jpype.imports
jpype.addClassPath(sys.argv[1])
jpype.startJVM(convertStrings=False)
import org.apache.pdfbox.tools as tools
tools.ExtractText.main(['-startPage', '1', sys.argv[2], sys.argv[3]])
但每次我想转换pdf文件时,加载jar文件会很慢。有谁能提供flask代码来创建RESTful服务,这样pdfbox只能加载一次,然后就可以从PDF中提取文本了

另外,这是一个教程,不适合解决我的问题


例如,它从_目录导入
send_,这与完整的解决方案有点遥远。我需要的是一个示例程序,它可以从REST接口获取输入并将文件保存在某个位置,然后调用java代码,然后将文件发送回。因此,需要一个显示所有三个步骤的示例。

您可以在Flask中创建一个
POST
路由,该路由将接收上传的PDF文件,使用
pdfbox
进行处理,并将您需要的任何内容返回给用户(文本内容或文本文件本身)。我没有测试这段代码,它只是一个例子,让我了解如何处理它,希望对你有所帮助

$ ./main.py pdfbox-app-2.0.20.jar in.pdf output.txt

烧瓶是一条路要走,检查下面给出的例子。烧瓶是一条路要走。Django、FastAPI、Pyramid等也是如此。。。这就是为什么要求我们查找或推荐非现场资源的问题,以及那些主要基于观点的问题,都明确地脱离了主题。OP,这个问题太广泛了。以5k的声誉,我想你知道这一点。我把问题限制在烧瓶上。请打开它。@user1424739,这仍然是离题的。我们不是来为您编写(或查找)代码的。你对烧瓶做了一些基础研究吗?有没有看过教程?找到使用Flask创建RESTful API的库?我是唯一使用服务器的人,因此不需要检查文件名。需要的是,当调用jar函数时出错,应该有一些后处理代码。此外,它不应该要求用户将文件名与文件内容一起发送,只应该发送文件内容。换句话说,
curl-v-F file='@user1424739我更新了我的答案,以更多地反映您的需求,但是,代码仍然没有经过测试,我不打算对其进行测试,首先,因为关键不是要得到完整的解决方案,这是一些人实际得到的报酬。另一件事是,我不知道您要将什么样的数据发送到Web服务器,以及您需要做什么并返回服务。使用我的代码片段,您应该有一个很好的起点,并且在(或StakcOverflow)的一点帮助下,您应该能够自己完成其余的事情。如果存在对服务器的并发访问怎么办?应该返回什么来代替
#如果需要做额外的后处理
?我使用固定文件名,因为这是一个伪代码(如我的回答和代码片段中所述),并且因为我不打算为您编写完全可操作的web服务器。你应该能够自己解决一些简单的事情,比如这些。关于“额外处理”,我不知道你的意图是什么,你提到如果调用Java失败,你需要一些“额外的后处理”,所以只要处理它并返回你需要的任何东西(可能是某种错误?…+10@errata,用于模拟伪代码,用于明确的“给我答案”类型的问题。。。。
"""
Pseudo-code with possible mistakes, not tested, just to get the idea...
"""
import gzip
from io import BytesIO

import jpype
import jpype.imports
import org.apache.pdfbox.tools as tools
from flask import Flask, make_response
from flask import request

UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

jpype.addClassPath('pdfbox-app-2.0.20.jar')
jpype.startJVM(convertStrings=False)


def allowed_file(filename):
    """ Helper function to figure out if file is a-ok"""
    return '.' in filename and \
        filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/', methods=['POST'])
def index():
    """ Route to upload and process PDF files """
    uploaded_file_name = 'upload.pdf'
    converted_file = 'output.txt'

    # Get the file from the form upload (or any other desired way)
    # If you use curl with -F flag, request will contain 'form' data
    file_data = request.form['file']

    # Save content of file to local disk (e.g. save as .pdf file)
    with open(uploaded_file_name, 'w') as f:
        f.write(file_data)

    # Call your Java thingie
    try:
        tools.ExtractText.main(['-startPage', '1', uploaded_file_name, converted_file])
    except:
        print('Error processing file.')
        # Do extra post-processing if needed

    # Serve back whatever you need here...
    response = make_response(converted_file)
    gzip_buffer = BytesIO()
    gzip_file = gzip.GzipFile(mode='wb', fileobj=gzip_buffer)

    gzip_file.write(response.get_data())
    gzip_file.close()
    response.set_data(gzip_buffer.getvalue())
    response.headers.set('Content-Encoding', 'gzip')
    response.headers.set('Content-Length', len(response.get_data()))
    response.headers.set('Content-Disposition', 'attachment', filename=converted_file)
    return response