View all our articles

Save your PDF to your Amazon S3 Bucket in Go with Net/HTTP

In this guide, we'll show you how you can generate a PDF document from HTML with PDFShift's API and save it to your own Amazon S3 bucket. This allows you to generate document and automate actions once the document has been created on your bucket.

First, you'll need to set up permissions in your bucket to allow PDFShift to write in it.

We crafted a very simple policy that only does what is required and nothing more. You can copy/paste it and apply it on your bucket, but don't forget to change the name of your bucket:

{
    "Version": "2012-10-17",
    "Statement": [{
        "Sid": "Allows PDFShift.io to write the generated PDF in this bucket.",
        "Effect": "Allow",
        "Principal": {
            "AWS": ["arn:aws:iam::804461045055:user/pdfshift"]
        },
        "Action": ["s3:PutObject"],
        "Resource": "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*"
    }]
}

Once this is done, you can send a request to PDFShift by adding the s3_destination parameter. It takes the full s3 bucket as a URL. Once the conversion will be done, PDFShift will

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",
        "s3_destination": "s3://DOC-EXAMPLE-BUCKET/test/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 saved to your S3 Bucket")
}

Once the conversion is done, PDFShift will save the content to your given s3_destination path and will return a response with a JSON body, such as:

{
    "success": true,
    "url": "https://DOC-EXAMPLE-BUCKET.s3.amazonaws.com/test/example.pdf",
    "filesize": 34980,
    "duration": 1423,
    "response": {
        "status-code": 200,
        "content-length": 0,
        "requests": 0,
        "duration": 1263.254446029663
    },
    "executed": "2024-03-06T10:13:33.154960",
    "pdf_pages": 1
}

In the event you haven't setup the permissions properly, PDFShift will fail with the following JSON body error:

{
    "success": false,
    "error": "The S3 destination you provided can not be accessed.",
    "code": 400
}

This features is really interesting as it allows you to not have to write the logic once the file was generated if your intention is to backup the file (such as an invoice, a report or saving a document).

You can couple it with the webhook parameter to increase the speed of processing. By doing so, PDFShift will treat your conversion request asynchronously and save the document in the given path. The request made to PDFShift will be immediate.

For further details on the s3_destination 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.