Create PDFs from HTML using an Express server and Playwright running in a Docker container.
The audunru/html2pdf Docker image will give you a web server that accepts POST requests to the /pdf endpoint. The submitted HTML will be returned as a PDF.
Note that any resources referred to in the HTML (JavaScript, CSS, images) will need to be reachable from where the container is running.
docker run -d -p 3000:3000 --name html2pdf --security-opt seccomp=seccomp_profile.json audunru/html2pdfservices:
html2pdf:
image: audunru/html2pdf
container_name: html2pdf
restart: always
ports:
- "3000:3000"
environment:
ALLOW_ORIGIN: "https://domain.com"
HEADERS: "{'Strict-Transport-Security':'max-age=63072000; includeSubDomains; preload'}"
JAVASCRIPT_ENABLED: true
WAIT_UNTIL: "domcontentloaded"
PDF_OPTIONS: "{'landscape':true}"
PORT: "3000"
PAYLOAD_LIMIT: "100000"
security_opt:
- seccomp=seccomp_profile.jsonThis will produce a PDF with the text "Hello world" in it.
curl http://localhost:3000/pdf --output hello-world.pdf \
-F "file=@-" <<< "<html><body><p>Hello world3</p></body></html>"const formData = new FormData();
const html = "<html><body><p>Hello world</p></body></html>";
const blob = new Blob([html], { type: "text/html" });
formData.append("file", blob);
const response = await fetch("http://localhost:3000/pdf", {
method: "POST",
body: formData,
});You can build the Docker image locally and run it like so:
docker build -t html2pdf .
docker stop html2pdf
docker rm html2pdf
docker run -d -p 3000:3000 --name html2pdf --security-opt seccomp=seccomp_profile.json html2pdf