HTTP API for Weasyprint to generate PDF and PNG out of HTML (using Flask)
342
HTTP API for Weasyprint to generate PDF and PNG out of HTML (using Flask).
Start the Weasyprint Docker container in a terminal and expose the service on a http://localhost:5000:
$ docker run -p 5000:5000 dimw/weasyprint
For PDF generation run a following curl command, pass HTML a s part of JSON request (html attribute), and define a desired output file (wasyprint-test.pdf):
$ curl \
-X POST http://localhost:5000/render \
-H "Content-Type: application/json" \
-d '{"html": "<html><body><h1>Hi!</h1></body></html>"}' \
--output weasyprint-test.pdf
To switch to PNG generation the optional outputFormat attribute passed must be "png":
$ curl \
-X POST http://localhost:5000/render \
-H "Content-Type: application/json" \
-d '{"html": "<html><body><h1>Hi!</h1></body></html>", "outputFormat": "png"}' \
--output weasyprint-test.png
Hint: Please refrain of exposing the built-in Flask development server in the container directly to the public and read how to "Run with a Production Server" in the Flask documentation.
The endpoint might be used for a simple health check of the application.
Request:
GET /healthcheck
Response:
OK
This endpoint is used for generation of PDF or PNG out of HTML.
Request:
POST /render
Content-Type: application/json
{
"html": "<html>...</html>",
"outputFormat": "pdf"
}
Response:
<<Binary content (PDF or PNG)>>
For local development and testing of the Weasyprint server Docker Compose can be used. The service can be started in the development (debug) mode and exposed on http://localhost:5000 by running the following command:
$ docker-compose up
The debug mode enables live reload of the endpoints defined in ./app/server.py.
The following command can be used to execute the tests:
$ docker-compose run weasyprint /app/run-tests.sh
The content of this repository is under the MIT licence.
Content type
Image
Digest
Size
172.7 MB
Last updated
over 6 years ago
docker pull dimw/weasyprint