How To Create PDFs With Go, Javascript, Curl Or Other Languages
Tags: pdf, cloud, webservice
Published: 2022-01-23 11:33:21

---
title: How To Create PDFs With Go, Javascript, Curl Or Other Languages
published: true
description: a web service for creating PDF documents
tags: pdf,cloud,webservice
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2lk4k5o0laqv9fetzaj.png
---

Sometimes you need a pdf, for example for a confirmation or an invoice document, but you either don't have the possibility to install the libraries on the target system or the effort is too big.

There are a lot of great services out there, but unfortunately they didn't quite cover our use case. 

That's why we created our own [pdf service](https://www.alvine.cloud/en/products/media-services/juno/documentation/#create).

The desired page can be defined with [Juno](https://www.alvine.cloud/en/products/media-services/juno/) via a JSON file.

First we want to define a page:

```json
"page-size":{
      "unit":"mm",
      "width":291,
      "height":210
   },
```

Next, we want the pdf to use the RobotoX character set:

```json
"fonts":[
      {
         "name":"RobotoX",
         "url":"https://cdn.jsdelivr.net/npm/rubik-font@0.0.3/fonts/Rubik-Light.ttf",
         "kerning":true
      }
   ],
```

To avoid having to redefine certain content, such as the header or the footer, you can also assign a template to the PDF. 

```json
"templates":[
      {
         "url":"https://cdn.alvine.io/examples/alvine-cloud-website.pdf",
         "page":1,
         "name":"website",
         "box":"/MediaBox"
      }
   ],
```

The pages key can then be used to define the corresponding contents of the page. The individual objects are defined via the type key. There are texts, text boxes, geometric objects and templates and images. The following is an example of an image.

```json
{
 "type":"image",
 "url":"https://alvine.io/alvine.png",
 "x":5,
 "y":5,
 "width":40,
 "height":10
}
```

The JSON can then be sent to the service via the preferred way. Here we want to use `curl`.


```bash
curl --location --request POST 'https://juno.alvine.cloud/api/v1/create' \
--header 'Accept: application/json' \
--header 'Authorization: api-key EVALUATION' \
--header 'Content-Type: application/json' \
--data-raw '
{
   "author":"schukai GmbH",
   "title":"Demopaper",
   "subject":"this is a example pdf",
   "page-size":{
      "unit":"mm",
      "width":291,
      "height":210
   },
   "fonts":[
      {
         "name":"RobotoX",
         "url":"https://cdn.jsdelivr.net/npm/rubik-font@0.0.3/fonts/Rubik-Light.ttf",
         "kerning":true
      }
   ],
   "templates":[
      {
         "url":"https://cdn.alvine.io/examples/alvine-cloud-website.pdf",
         "page":1,
         "name":"website",
         "box":"/MediaBox"
      }
   ],
   "pages":[
      {
         "outline":"This is a bookmark",
         "objects":[
            {
               "type":"template",
               "name":"website",
               "x":0,
               "y":0,
               "width":148
            },
            {
               "type":"textbox",
               "x1":170,
               "y1":30,
               "x2":270,
               "y2":180,
               "border":{
                  "color":{
                     "r":255,
                     "g":0,
                     "b":0
                  }
               },
               "text":{
                  "text":"Text with Linefeed",
                  "valign":"top"
               }
            },
            {
               "type":"text",
               "text":"Lorem ipsum dolor sit amet, consetetur\nsadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.",
               "x":180,
               "y":50,
               "font":"RobotoX",
               "width":80
            },
            {
               "type":"image",
               "url":"https://alvine.io/alvine.png",
               "x":5,
               "y":5,
               "width":40,
               "height":10
            }
         ]
      }
   ]
}' > test.pdf
```

Besides the create api there is also an api for converting web pages to PDF and converting office documents.



_hope you enjoy it!_



## References

* https://www.alvine.cloud/en/products/media-services/juno/documentation/