I use stirling-pdf for this, ran locally with
docker run -d -p 8080:8080 -e DOCKER_ENABLE_SECURITY=false --name stirling-pdf frooodle/s-pdf:latest
Lots of features, OP you should definitely check this out
Seems cool! Thanks for sharing. Even though it's MIT licensed it's written in Java, so unfortunately I can't borrow any code, but it's nice for inspiration.
Official image seems to be "docker.stirlingpdf.com/stirlingtools/stirling-pdf:latest" BTW.
Stirling is a life saver, they have a web UI too: https://stirlingpdf.io