How to Convert a URL to a PDF Document using Go

Cloudmersive
2 min readNov 21, 2022

At a high level, entering a URL string into an internet browser will retrieve a specific set of online resources forming a web page. That’s not the only way to render a URL, however; with our URL to PDF API, you can use that same URL string to generate a PDF with the fully rendered web page instead. Making this conversion will provide you with a mistake-free platform to try experimental edits on your web page, and it’ll allow you to save a static version/view of a web page to compare with future iterations (not unlike taking a URL screenshot).

The URL To PDF Conversion API accepts an input request format containing the URL string and other (optional) details as well. These options include a field to accommodate extra loading wait time, a field to specify the inclusion of background graphics, and a field to specify the scale factor (please refer to the example JSON request model below):

{
"Url": "string",
"ExtraLoadingWait": 0,
"IncludeBackgroundGraphics": true,
"ScaleFactor": 0
}

One great advantage of this API is that you can use it completely free — you’ll just need to register a free account on our website (this will yield a free-tier API key with a limit of 800 API calls per month) and follow brief instructions below to call the API using complementary, ready-to-run Go code examples.

To call this API, simply include the below code examples in your file, and then you can go about entering your API key string & URL request parameters into their respective headers:

package main

import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)

func main() {

url := "https://api.cloudmersive.com/convert/web/url/to/pdf"
method := "POST"

payload := strings.NewReader("Url=%3Cstring%3E&ExtraLoadingWait=%3Cinteger%3E&IncludeBackgroundGraphics=%3Cboolean%3E&ScaleFactor=%3Cinteger%3E")

client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)

if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")

res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

With that, you’re all done — no more code required. Simple & easy!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.