0

I've been developing an API using FastAPI and pipenv to manage my virtual environment. To start my testing setup, I use the following commands on the console:

pipenv shell
uvicorn backend:app --reload

The API starts after a couple of seconds and everything is okay. However, recently I decided to install pdfkit to generate some PDF documents that need to be sent back to a webapp. However, after I did (using this this page as reference):

pipenv install pdfkit

Now, whenever I try to run the same uvicorn command to start my API, the API returns the error module pdfkit not found. I've checked that the package is properly installed and it does appear on my Pipfile and whenever I type pip freeze once my enviroment has been activated.

Here's a snippet of the API call that returning the error:

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, StreamingResponse
from scipy.optimize import leastsq
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.styles import Font
from openpyxl import Workbook
import pandas as pd
import numpy as np
import json
import os

# ==========================
# SETUP
# ==========================

app = FastAPI()

# ==========================
# CORS
# ==========================

origins = [
    "http://127.0.0.1:5500/", 
    "http://127.0.0.1:8000/", 
    "http://localhost"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],        # List of origins that are allowed to make cross-origin requests
    allow_credentials=True,     # Indicate that cookies should be supported for cross-origin requests
    allow_methods=["*"],        # List of HTTP methods that should be allowed for cross-origin requests
    allow_headers=["*"],        # List of HTTP headers that should be supported for cross-origin requests
)

# ==========================
# GET: PDF RENDERING
# ==========================

@app.get("/pdf")
def render_pdf(pdf_content: str):
    
    from jinja2 import Template, Environment, FileSystemLoader
    import pdfkit as pdf

    # Jinja2 template "generator" or "main" object (AKA Environment)
    env = Environment(loader = FileSystemLoader('PDF Rendering'), trim_blocks=True, lstrip_blocks=True)

    # Template parameters
    template_params = {
        "BASE_PATH": os.getcwd(),
        "REPORT_CONTENT": pdf_content,
    }

    # Render template
    template = env.get_template("pdf_template.j2")
    template_text = template.render(template_params)

    with open("PDF Rendering/render.html", "w") as f:
        f.write(template_text)

    # Turn rendered file to PDF
    pdf.from_file("PDF Rendering/render.html", "PDF Rendering/render.pdf")

    return {
        "message": "Success"
    }

(The Jinja section works mind you, but for some reason, the pdfkit one doesnt)

Eddysanoli
  • 481
  • 2
  • 8
  • 17
  • It's hard to tell without the source code and the exact steps you follow... Anyways, have you checked that the `pipenv` has the package correctly installed? Is it for the correct `python` version? – lsabi Jun 06 '22 at 08:22
  • Sorry, I will include a small snippet of my code to make it clearer. But yeah. I have installed pdfkit multiple times using `pipenv install pdfkit` and that package specifically returns the error: "module not found". Something similar happened with an Excel handling package, but I manage to replace it with "openpyxl". – Eddysanoli Jun 06 '22 at 14:24
  • That's really odd...could it be that the lib did not get installed? Or is it possible that you have a folder with the same name that causes troubles? – lsabi Jun 06 '22 at 21:01
  • Yeah, I wanted to check that as well, but I made sure that I dont have any folders named that way. Its just strange – Eddysanoli Jun 07 '22 at 21:43
  • And does the app run outside `pipenv` or you don't want to/can't try? – lsabi Jun 08 '22 at 20:08

0 Answers0