Download PDF file using pdfkit and FastAPI

Returning FileResponse is solved my problem. Thanks to @Paul H and @clmno Below codes are working example of returning pdf file to download with FastApi. from typing import Optional from fastapi import FastAPI from starlette.responses import FileResponse import pdfkit app = FastAPI() config = pdfkit.configuration(wkhtmltopdf=r”C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe”) @app.get(“/”) def read_root(): pdfkit.from_url(“https://nakhal.expo.com.tr/nakhal/preview”,”file.pdf”, configuration=config) return FileResponse( “file.pdf”, media_type=”application/pdf”, … Read more

Can’t create pdf using python PDFKIT Error : ” No wkhtmltopdf executable found:”

The following should work without needing to modify the windows environment variables: import pdfkit path_wkhtmltopdf = r’C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe’ config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) pdfkit.from_url(“http://google.com”, “out.pdf”, configuration=config) Assuming the path is correct of course (e.g. in my case it is r’C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe’).

Create PDF with multiple pages

Output Fixed using CTFramesetterCreateFrame and CFAttributedStringGetLength class PDFCreator { lazy var pageWidth : CGFloat = { return 8.5 * 72.0 }() lazy var pageHeight : CGFloat = { return 11 * 72.0 }() lazy var pageRect : CGRect = { CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight) }() lazy var marginPoint : CGPoint = … Read more