Skip to content

audunru/html2pdf

Repository files navigation

html2pdf

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.

Running

docker

docker run -d -p 3000:3000 --name html2pdf --security-opt seccomp=seccomp_profile.json audunru/html2pdf

docker compose

services:
  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.json

Turning HTML into a PDF

This will produce a PDF with the text "Hello world" in it.

curl

curl http://localhost:3000/pdf --output hello-world.pdf \
  -F "file=@-" <<< "<html><body><p>Hello world3</p></body></html>"

JavaScript

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,
});

Development

Build Docker image

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •