View all our articles

Save your PDF online and get back a URL in Go with Net/HTTP

In this guide, we'll show you how you can generate a PDF document from HTML and get back an URL instead of the raw PDF. This allows you to share the link directly to your users or redirect them to that page.

To do so, we are going to pass the filename parameter to the request. It expects a string that will be the name of the file once it is saved on our servers.

package main

import (
    "bytes"
    "encoding/base64"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    // You can get an API key at https://pdfshift.io
    apiKey := "sk_xxxxxxxxxxxx"

    params := map[string]interface{}{
        "source": "https://www.example.com",
        "filename": "example.pdf"
    }

    // Marshal the parameters into JSON
    jsonParams, err := json.Marshal(params)
    if err != nil {
        fmt.Println("Error marshaling JSON:", err)
        return
    }

    // Create a new HTTP client
    client := &http.Client{}

    // Create a new request
    req, err := http.NewRequest("POST", "https://api.pdfshift.io/v3/convert/pdf", bytes.NewBuffer(jsonParams))
    if err != nil {
        fmt.Println("Error creating request:", err)
        return
    }

    // Set request headers
    req.Header.Set("Content-Type", "application/json")

    // Set basic authentication header
    auth := "api:" + apiKey
    req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))

    // Perform the request
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error performing request:", err)
        return
    }
    defer resp.Body.Close()

    // Read response body
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        return
    }

    // Check response status code
    if resp.StatusCode >= 400 {
        fmt.Printf("Request failed with status code %d: %s\n", resp.StatusCode, string(body))
        return
    }

    fmt.Println("The PDF document was generated and is available online via the url in the response")
}

In the above example, we used the filename parameter to specify the name of the file once it is saved on our servers. The response will convert the document but return a JSON response instead of the raw PDF.

The body of the response will be similar to this:

{
    "success": true,
    "url": "https://pdfshift.s3.amazonaws.com/d/2/2024-03/c9c2d3e536ff42d892f06fdda6bb1ff7/example.pdf",
    "filesize": 34980,
    "duration": 1237,
    "response": {
        "status-code": 200,
        "content-length": 0,
        "requests": 0,
        "duration": 1085.6739225387573
    },
    "executed": "2024-03-06T10:05:51.516413",
    "pdf_pages": 1
}

NOTE: Using the filename does save your document on our S3 storage for 2 days. After two days, the document is automatically deleted.

NOTE: Since we store your documents on our server, this command is not recommended if you want to be HIPAA compliant.

For further details on the filename property and its usage, please refer to our dedicated documentation.

We hope this guide was helpful. If you have any questions or noticed any issues on the code above,
feel free to drop us a line.