convert pdf code to use sqlalchemy