DEV Community 👩‍💻👨‍💻

Cover image for How To Create PDFs With Go, Javascript, Curl Or Other Languages
Volker Schukai for schukai GmbH

Posted on

How To Create PDFs With Go, Javascript, Curl Or Other Languages

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.

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
   },
Enter fullscreen mode Exit fullscreen mode

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
      }
   ],
Enter fullscreen mode Exit fullscreen mode

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"
      }
   ],
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

hope you enjoy it!

References

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.