github.com/raphaelreyna/latte@v0.11.2-0.20220317193248-98e2fcef4eef/README.md (about)

     1  # LaTTe
     2  ![Docker Pulls](https://img.shields.io/docker/pulls/raphaelreyna/latte)
     3  
     4  Generate PDFs using LaTeX templates and JSON.
     5  
     6  [Try out the demo!](https://rphlrn.com/latte-demo)
     7  
     8  [Find LaTTe on Docker Hub](https://hub.docker.com/r/raphaelreyna/latte)
     9  
    10  ## Table of Contents
    11  * [About](#toc-about)
    12  * [Obtaining LaTTe](#toc-obtaining)
    13  * [Running & Using LaTTe](#toc-running-latte)
    14  	* [HTTP Service](#toc-http-service)
    15  		* [Environment Variables](#toc-env-vars)
    16  		* [Registering Files](#toc-registering-files)
    17  		* [Generating PDFs](#toc-service-generating-pdfs)
    18  			* [Example](#toc-example-1)
    19  	* [CLI](#toc-cli)
    20  * [Extending LaTTe](#toc-extending)
    21  * [Docker Images](#toc-docker)
    22  	* [Tags](#toc-tags)
    23  	* [Image Size](#toc-image-size)
    24  	* [Building Custom Images](#toc-building-custom-images)
    25  * [Contributing](#toc-contributing)
    26  * [Roadmap](#toc-roadmap)
    27  
    28  <a name="toc-about"></a>
    29  ## About
    30  LaTTe helps you generate professional looking PDFs by using your .tex files as templates and filling in the details with JSON.
    31  Under the hood, it uses [pdfTeX / pdfLaTeX](https://tug.org/applications/pdftex) to create a PDF from a .tex file that has been filled in using [Go's templating package](https://golang.org/pkg/text/template/).
    32  
    33  LaTTe has two modes of operation:
    34  * As a service over HTTP, while also offering some degree of templating/pre-processing, caching and persistence. [More info on running LaTTe as an HTTP service.](#toc-http-service)
    35  * As a cli tool to create PDFs using TeX/LaTeX and JSON files quickly and easily. [More info on using LaTTe's cli.](#toc-cli)
    36  
    37  <a name="toc-obtaining"></a>
    38  ## Obtaining LaTTe
    39  You can download the source code for LaTTe by running `git clone github.com/raphaelreyna/latte` in your terminal.
    40  LaTTe can then be easily compiled by running `go build ./cmd/latte`. 
    41  If you wish to build LaTTe with support for PostreSQL, simply run `go build -tags postgresql` instead. [More info on persistent storage support](#toc-extending)
    42  
    43  LaTTe is also available via several docker images; running `docker run --rm -d -p 27182:27182 raphaelreyna/latte` will leave you with a basic version of LaTTe running as a an HTTP service.
    44  [More info on Docker images](#toc-docker)
    45  
    46  <a name="toc-running-latte"></a>
    47  ## Running & Using LaTTe
    48  
    49  <a name="toc-http-service"></a>
    50  ### HTTP Service
    51  LaTTe will run as an HTTP service by default; running `latte` in your terminal will have LaTTe listening for HTTP traffic on port 27182 by default.
    52  All configurations are done through environment variables.
    53  
    54  <a name="toc-env-vars"></a>
    55  ### Environment Variables
    56  ### `PORT`
    57  The port that LaTTe will bind to. The default value is 27182.
    58  ### `LATTE_ROOT`
    59  The directory that LaTTe will use to store all of its files. The default value is the users cache directory.
    60  ### `LATTE_DB_HOST`
    61  The address where LaTTe can reach its database (assuming LaTTe was compiled with database support).
    62  ### `LATTE_DB_PORT`
    63  The the port that LaTTe will use when connecting to its database (assuming LaTTe was compiled with database support).
    64  ### `LATTE_DB_USERNAME`
    65  The username that LaTTe will use to connect to its database (assuming LaTTe was compiled with database support).
    66  ### `LATTE_DB_PASSWORD`
    67  The password that LaTTe will use to connect to its database (assuming LaTTe was compiled with database support).
    68  ### `LATTE_DB_SSL`
    69  Dictates if the database that LaTTe will use is using SSL; acceptable values are `required` and `disable` (assuming LaTTe was compiled with database support).
    70  ### `LATTE_TMPL_CACHE_SIZE`
    71  How many templates LaTTe will keep cached in memory. (defaults to 15)
    72  
    73  <a name="toc-registering-files"></a>
    74  #### Registering a file
    75  Files are registered by sending an HTTP POST request to the endpoint "/register" with a JSON body of the form:
    76  ```
    77  {
    78  	"id": "WHATEVER_NAME_YOU_WANT"
    79  	"data": "BASE_64_ENCODED_STRING"
    80  }
    81  ```
    82  
    83  <a name="toc-service-generating-pdfs"></a>
    84  #### Generating PDFs
    85  LaTTe can genarate PDF's from both registered and unregistered resources, templates and json files (which LaTTe calls 'details'). A resource is any kind of file used in compiling the .tex file into a PDF (e.g. images); a template is any valid .tex file.
    86  
    87  A registered file is one that has been stored either to LaTTe's local disk and/or to some database (currently only PostgreSQL is supported). Registered files are referenced by an ID. When generating a PDF, all references to registered files are made in the URL of the request; unregistered files are provided as base 64 encoded strings in a JSON body.
    88  
    89  LaTTe also allows for users to define how missing template keys should be handled through the "onMissingKey" field in the requests JSON body or URL. Valid options for "onMissingKey" are: "error" for when LaTTe should return with a "Bad Request" HTTP status, "zero" for when LaTTe should use the field types zero value ("" for strings, false for booleans, 0 for numerics, etc.).
    90  
    91  
    92  PDF's are generated by sending an HTTP POST request to the endpoint "/generate" with a JSON body of the form (if using unregisted files):
    93  ```
    94  {
    95  	"template": "BASE_64_ENCODED_STRING",
    96  	"resources": {
    97  		"FILE_NAME": "BASE_64_ENCODED_STRING"
    98  		},
    99  	"details": { SOME_OBJECT_DESCRIBING_YOUR_SUBSTITUTIONS },
   100  	"delimiters": { "left": "LEFT_DELIMITER", "right": "RIGHT_DELIMITER" },
   101     	"onMissingKey": "error" | "zero" | "nothing",
   102  	"compiler": "latexmk" | "pdflatex",
   103  	"count": 1 | 2 | 3 | ...
   104  }
   105  ```
   106  If you wish to also use registered files, you may reference them in the URL:
   107  ```
   108  http://localhost:27182/generate?tmpl=TEMPALATE_ID&rsc="RESOURCE_ID&rsc="SOME_OTHER_RESOURCE_ID"&dtls="DETAILS_ID"&onMissingKey="error|zero|nothing"
   109  ```
   110  If you provide both a reference to a file and include it in the JSON body, the file you sent in the body will be used.
   111  
   112  <a name="toc-example-1"></a>
   113  ##### Example: Generating a PDF from unregistered files
   114  Here we demonstrate how to generate a PDF of the Pythagorean theorem, after substituting variables a, b & c for x, y & z respectively.
   115  
   116  We create our .tex template file pythagorean_template.tex:
   117  ```
   118  \documentclass{article}
   119  \title{LaTTe Sample Document}
   120  \begin{document}
   121  \maketitle
   122  The Pythagorean Theorem: 
   123  $ #!.a!# ^ 2 + #!.b!# ^ 2 = #!.c!# ^ 2 $
   124  \end{document}
   125  ```
   126  The template `.tex` file should be a template that follows [Go's templating syntax](https://golang.org/pkg/text/template/).
   127  LaTTe by default uses `#!` and `!#` as the left and right delimeters, respectively, in the `.tex` template file; however [custom delimiters are supported](#toc-service-generating-pdfs). As required by pdfLaTeX, all files must start with the character "\".
   128  
   129  We then convert it to base 64:
   130  ```
   131  $ cat pythagorean_template.tex | base64
   132  ```
   133  which gives the output:
   134  ```
   135  XGRvY3VtZW50Y2xhc3N7YXJ0aWNsZX0KXHRpdGxle0xhVFRlIFNhbXBsZSBEb2N1bWVudH0KXGJlZ2lue2RvY3VtZW50fQpcbWFrZXRpdGxlClRoZSBQeXRoYWdvcmVhbiBUaGVvcmVtOiAKJCAjIS5hISMgXiAyICsgIyEuYiEjIF4gMiA9ICMhLmMhIyBeIDIgJApcZW5ke2RvY3VtZW50fQo=
   136  ```
   137  
   138  We then send this to LaTTe:
   139  ```
   140  $ curl \
   141  -X POST \
   142  -H "Content-Type: application/json" \
   143  -d '{"template":"XGRvY3VtZW50Y2xhc3N7YXJ0aWNsZX0KXHRpdGxle0xhVFRlIFNhbXBsZSBEb2N1bWVudH0KXGJlZ2lue2RvY3VtZW50fQpcbWFrZXRpdGxlClRoZSBQeXRoYWdvcmVhbiBUaGVvcmVtOiAKJCAjIS5hISMgXiAyICsgIyEuYiEjIF4gMiA9ICMhLmMhIyBeIDIgJApcZW5ke2RvY3VtZW50fQo=", "details": { "a": "x", "b": "y", "c": "z" } }' \
   144  --output pythagorean.pdf "http://localhost:27182/generate"
   145  ```
   146  
   147  which leaves us with the file `pythagorean.pdf` (the image below is a cropped screenshot of `pythagorean.pdf`):
   148  ![pythagorean_pdf](/../screenshots/screenshots/screenshot.png?raw=true)
   149  
   150  <a name="toc-cli"></a>
   151  ### CLI
   152  LaTTe offers a CLI to quickly and easily generate templated PDFs using the files on your computer.
   153  ```
   154  Usage: latte [ -t template_tex_file ] [ -d details_json_file ] [ path/to/resources ]
   155  
   156  Description: Generate PDFs using TeX / LaTeX templates and JSON.
   157  
   158  
   159  Flags:
   160    -t Path to .tex file to be used as the template.
   161  
   162    -d Path to .json file to be used as the details to fill in to the tamplate.
   163    
   164  Other:
   165      The final argument is optional and should be a path to resources needed for compilation.
   166      Resources are any files that are referenced in the .tex file such as image files.
   167  ```
   168  
   169  <a name="toc-extending"></a>
   170  ## Extending LaTTe
   171  ### Adding databases / persistent store drivers
   172  LaTTe can easily be extended to support using various databases and other storage solutions.
   173  To have LaTTe use your persistent storage solution of choice, simply create a struct that satisfies the `DB` interface:
   174  ```
   175  type DB interface {
   176  	// Store should be capable of storing a given []byte or contents of an io.ReadCloser
   177  	Store(ctx context.Context, uid string, i interface{}) error
   178  	// Fetch should return either a []byte, or io.ReadCloser.
   179  	// If the requested resource could not be found, error should be of type NotFoundError
   180  	Fetch(ctx context.Context, uid string) (interface{}, error)
   181  	// Ping should check if the databases is reachable.
   182    	// If it is, the return error should be nil and non-nil otherwise.
   183  	Ping(ctx context.Context) error
   184  }
   185  ```
   186  
   187  <a name="toc-docker"></a>
   188  ## Docker Images
   189  
   190  <a name="toc-tags"></a>
   191  ### Image Tags
   192  There are several LaTTe images available to serve a wide range of needs, and they all follow the same tagging convention:
   193  ```
   194  	latte:<VERSION>-[pg]-<base/full>
   195  ```
   196  where \<VERSION\> denotes the latte version, [pg] if present denotes Postgres support, and \<base/full\> denotes the presence of either [texlive-full](https://packages.ubuntu.com/eoan/texlive-full) or [texlive-base](https://packages.ubuntu.com/eoan/texlive-base).
   197  	
   198  #### Currently Supported Tags
   199  The currently supported tags for LaTTe are:
   200  ##### v0.11.0-base
   201  ##### v0.11.0-pg-base
   202  ##### latest, v0.11.0-full
   203  ##### v0.11.0-pg-full
   204  
   205  <a name="toc-image-size"></a>
   206  ### Image size
   207  LaTTe relies on [pdflatex](https://www.tug.org/applications/pdftex/) in order to actually create the PDF files.
   208  Unfortunately, this means that image sizes can be rather large (a full texlive installation is around 4GB).
   209  The build script in the `build` directory makes it easy to create custom sized images of LaTTe to fit your needs.
   210  
   211  <a name="toc-building-custom-images"></a>
   212  ### Building Custom Images
   213  LaTTe comes with a build script, build/build.sh, which makes it easy to build LaTTe images with custom Go build flags and tex packages.
   214  
   215  ```
   216  Usage: build.sh [-h] [-s] [-b build_tag] [-p latex_package] [-t image_tag]
   217  		[-d descriptor] [-H host_name] [-u user_name]
   218  
   219  Description: build.sh parametrically builds and tags Docker images for Latte.
   220               The tag used for the image follows the template bellow:
   221                   host_name/user_name/image_name:image_tag-descriptor
   222  
   223  Flags:
   224    -b Build tags to be passed to the Go compiler.
   225  
   226    -d Descriptor to be used when tagging the built image.
   227  
   228    -h Show this help text.
   229  
   230    -p LaTeX package to install, must be available in default Ubuntu repos.
   231       (default: texlive-base)
   232  
   233    -s Skip the image building stage. The generated Dockerfile will be sent to std out.
   234  
   235    -t Tag the Docker image.
   236       The image will be tagged with the current git tag if this flag is omitted.
   237       If no git tag is found, we default to using 'latest' as the image tag.
   238  
   239    -u Username to be used when tagging the built image.
   240       (default: raphaelreyna)
   241  
   242    -H Hostname to be used when tagging the built image.
   243  
   244    -y Do not ask to confirm image tab before building image.
   245  ```
   246  
   247  <a name="toc-contributing"></a>
   248  ## Contributing
   249  Contributions are welcome!
   250  
   251  <a name="toc-roadmap"></a>
   252  ## Roadmap
   253  - :heavy_check_mark: <s>Registering templates and resources.</s>
   254  - Add support for AWS S3, <s>PostrgeSQL</s>, and possibly other forms of persistent storage.
   255  - :heavy_check_mark: <s>CLI tool</s>.
   256  - Add support for building PDFs from multiple LaTeX files.
   257  - Whatever else comes up