0

I have a project that need to handle Upload File that using FastApi Python, and i plan to use pymupdf

but i failed after i try from solution How to use pymupdf to read a pdf after uploading that via st.file_uploader()?

here is my code

@app.post("/api/readpdf/", tags=["Read PDF"])
async def readpdf(files: UploadFile, response: Response, db: Session = Depends(get_db)):
    
    res = await readpdf.read_pdf(files, db)
    
    re = {
            "httpstatus": status.HTTP_404_NOT_FOUND, 
            "message": "Berhasil", 
            "results" : res,
            "status": "BAD REQUEST", 
            "timestamp": datetime.now()
        }
    response.status_code = 404
    r = jsonable_encoder(re)
    return r

and

from fastapi import UploadFile
from sqlalchemy.orm import Session
import fitz
from pdfrw import PdfReader, PdfWriter
from io import BytesIO
import fitz

async def read_pdf(file: UploadFile,  db: Session):
    
    
    if file is not None:
        doc = fitz.open(stream= await file.read(), filetype="pdf" )
        print(doc)
        doc.close()
        
    ev={"ok" : "OK"}
    return ev

but the doc is always empty enter image description here

Am I missing something?? I would be glad for any help.

Hansen
  • 650
  • 1
  • 11
  • 32

0 Answers0