Create PDF files with Go, Javascript, Curl or other languages

PDF creation as a service

Create PDF files with Go, Javascript, Curl or other languages

If you want to send a confirmation or an invoice to a customer in PDF format, you can use various tools. But if you don't want or are not allowed to install anything yourself, you can use a web service.

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

That's why we created our own pdf service.

The desired page can be defined with Juno via a JSON file.

First we want to define a page

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

Font, Font, Font

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

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

Templates

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

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

Objects

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.

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

Full example

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

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