View all our articles

Protecting the generated PDF in Go with Net/HTTP

In this guide, we'll show you how you can protect your generated PDF with encryption for owner and user, and for choosing who can modify, print and/or copy the generated PDF. This guides explains you to do it using Go and the Net/HTTP library and relies on the PDFShift's API.

One of the most useful feature here is the ability to set a password for the owner and the user of the PDF. This allows you to protect your document from anyone and restrict its access to only a few people.

PDFShift allows you to do this easily by adding the protection object to your query.

Here's a sample:

package main

import (

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

    params := map[string]interface{}{
        "source": "",
        "protection": map[string]interface{}{
            "owner_password": "owner_password",
            "user_password":  "user_password",
            "no_print":       true,
            "no_modify":      true,

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

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

    // Create a new request
    req, err := http.NewRequest("POST", "", bytes.NewBuffer(jsonParams))
    if err != nil {
        fmt.Println("Error creating request:", err)

    // 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)
    defer resp.Body.Close()

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

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

    // Save the PDF document
    err = ioutil.WriteFile("result.pdf", body, 0644)
    if err != nil {
        fmt.Println("Error saving PDF document:", err)

    fmt.Println("The PDF document was generated and saved to result.pdf")

Adding the protection object to your query will tell PDFShift to generate a PDF that will be password protected.

The protection object accepts the following parameters:

  • author : The name of the author in the PDF metadata.
  • owner_password: The password set for the owner.
  • user_password: The password set for the user.
  • no_print: If set to True, the user won't be able to print the PDF. (Only the owner)
  • no_modify: If set to True, the user won't be able to modify the PDF. (Only the owner)
  • no_copy: If set to True, the user won't be able to copy the content of the PDF. (Only the owner)

IMPORTANT: It is important to note that most PDF reader don't respect the protection parameter. For instance, if you set the user_password to none (allowing anyone to view the PDF) but set the owner_password and block the modify, print and copy, most PDF reader will still allow user (and not owners) to modify, print and copy the PDF. This is a limitation of the PDF format and not PDFShift.

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