|
1 | 1 | # Typst HTTP API |
2 | 2 |
|
| 3 | +***Compile typst documents with a simple HTTP request.*** |
| 4 | + |
3 | 5 | <!-- This sentence is from the typst repo --> |
4 | 6 | > [Typst](https://github.com/typst/typst) is a new markup-based typesetting system |
5 | 7 | > that is designed to be as powerful as LaTeX while being much easier to learn and use. |
6 | 8 | I recommend that you check it out if you don't know it yet. |
7 | 9 |
|
8 | 10 | This project is a web server that allows users to compile typst markup remotely by a simple API call. |
9 | | -This webserver is provided in the form of a docker container. |
10 | | -*For now there is no official image on any registry.* |
| 11 | +This webserver is provided in the form of a docker container [`ghcr.io/slashformotion/typst-http-api` see available tags](https://github.com/slashformotion/typst-http-api/pkgs/container/typst-http-api). |
11 | 12 |
|
12 | 13 | I want to bring some elements to your attention: |
13 | 14 |
|
14 | | -- Please be aware that while the container runs, |
15 | | - I do not consider this project production-ready, more work is needed. |
16 | 15 | - All contributions are welcome of course welcome. |
17 | 16 | - Currently, there is no way to compile a file that loads external resources (images or other `.typ` files for example). |
18 | 17 |
|
19 | | -Current version: v0.1.0 |
| 18 | +## HTTP interface |
20 | 19 |
|
21 | | -## Build and run |
| 20 | +This service expose two endpoints: |
22 | 21 |
|
23 | | -Build the docker image |
| 22 | +- `POST /` : send the typst content directly to the endpoint (no Content-Type header required) and a streaming reponse will be return with the raw pdf bytes. You can find the corresponding curl command in the section [How does it work ?](#how-does-it-work-). |
| 23 | + |
| 24 | + If your document is not valid and an error happen at the compilation step, you will get a code 422 and a json response with the raw error[^1]. |
| 25 | + |
| 26 | +- `GET /metrics` : a traditional prometheus metrics endpoint (includes python gc data, http requests info and others). |
| 27 | + |
| 28 | +## How does it work ? |
| 29 | + |
| 30 | +Run the container: |
24 | 31 |
|
25 | 32 | ```shell |
26 | | -docker build . -t typst_image |
27 | | -# This command build an image using the Dockerfile at the root of the project, |
28 | | -# then tag it with "typst_image" |
| 33 | +docker run -p 8000:8000 ghcr.io/slashformotion/typst-http-api |
29 | 34 | ``` |
30 | 35 |
|
31 | | -Create a container: |
| 36 | +Send a valid Typst file (here `test.typ`) to the api and output the file to `result.pdf`: |
32 | 37 |
|
33 | 38 | ```shell |
34 | | -docker run -p 8000:8000 typst_image |
35 | | -# This command creates a docker container based on the image created at the last step |
| 39 | +curl -H "Content-Type:text/plain" -X POST --data-binary @test.typ http://localhost:8000 --output result.pdf |
36 | 40 | ``` |
37 | 41 |
|
38 | | -Send `test.typ` to the api and output the file to `result.pdf`: |
| 42 | +### With docker-compose |
39 | 43 |
|
40 | | -```shell |
41 | | -curl -H "Content-Type:text/plain" --data-binary @test.typ http://localhost:8000 --output result.pdf |
| 44 | +```yml |
| 45 | +version: "3.8" |
| 46 | + |
| 47 | +services: |
| 48 | + typst-builder: |
| 49 | + image: ghcr.io/slashformotion/typst-http-api:v0.3.0 |
| 50 | + ports: |
| 51 | + - "8000:8000" |
42 | 52 | ``` |
43 | 53 |
|
44 | | -Or more simply use [httpie](https://httpie.io/cli): |
| 54 | +## Build the docker image locally |
| 55 | +
|
| 56 | +Build the docker image |
45 | 57 |
|
46 | 58 | ```shell |
47 | | -cat test.typ | http POST http://localhost:8000 > result.pdf |
| 59 | +docker build . -t typst_image |
| 60 | +# This command build an image using the Dockerfile at the root of the project, |
| 61 | +# then tag it with "typst_image" |
48 | 62 | ``` |
49 | 63 |
|
50 | | -- If the compilation succeeds, you will get a response with an HTTP code `200`. |
51 | | -The body of the response will contain the pdf document. |
52 | | -- On an invalid input you will get a JSON containing the error returned by the compiler with an HTTP code `422 Unprocessable Content`. |
| 64 | +Create a container: |
| 65 | + |
| 66 | +```shell |
| 67 | +docker run -p 8000:8000 typst_image |
| 68 | +# This command creates a docker container based on the image created at the last step |
| 69 | +``` |
53 | 70 |
|
54 | | - ```json |
55 | | - { |
56 | | - "error": "compile error: 16:21 expected length, found string" |
57 | | - } |
58 | | - ``` |
| 71 | +[^1]: error example: `{"reason":"compilation error", "content": "raw error log here"}` |
0 commit comments